| 1 |
/** |
| 2 |
* Copyright (c) 2012-2017, jcabi.com |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: 1) Redistributions of source code must retain the above |
| 8 |
* copyright notice, this list of conditions and the following |
| 9 |
* disclaimer. 2) Redistributions in binary form must reproduce the above |
| 10 |
* copyright notice, this list of conditions and the following |
| 11 |
* disclaimer in the documentation and/or other materials provided |
| 12 |
* with the distribution. 3) Neither the name of the jcabi.com nor |
| 13 |
* the names of its contributors may be used to endorse or promote |
| 14 |
* products derived from this software without specific prior written |
| 15 |
* permission. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT |
| 19 |
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 20 |
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 21 |
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 22 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 28 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
package com.jcabi.aspects; |
| 31 |
|
| 32 |
import java.lang.annotation.Documented; |
| 33 |
import java.lang.annotation.ElementType; |
| 34 |
import java.lang.annotation.Retention; |
| 35 |
import java.lang.annotation.RetentionPolicy; |
| 36 |
import java.lang.annotation.Target; |
| 37 |
import java.util.concurrent.TimeUnit; |
| 38 |
|
| 39 |
/** |
| 40 |
* Schedules the method to run with fixed delay, automatically. |
| 41 |
* |
| 42 |
* <p>For example, you want a method to do something every minute: |
| 43 |
* |
| 44 |
* <pre> @ScheduleWithFixedDelay(delay = 1, unit = TimeUnit.MINUTES) |
| 45 |
* public class Bucket implements Runnable, Closeable { |
| 46 |
* @Override |
| 47 |
* void run() { |
| 48 |
* // do some routine job |
| 49 |
* } |
| 50 |
* @Override |
| 51 |
* void close() { |
| 52 |
* // close operations |
| 53 |
* } |
| 54 |
* }</pre> |
| 55 |
* |
| 56 |
* <p>Execution will be started as soon as you make an instance of the class, |
| 57 |
* and will be stopped when you call {@code close()}: |
| 58 |
* |
| 59 |
* <pre> Bucket bucket = new Bucket(); |
| 60 |
* // some time later |
| 61 |
* bucket.close();</pre> |
| 62 |
* |
| 63 |
* <p>In order to be executed the class should implement either |
| 64 |
* {@link Runnable} or {@link java.util.concurrent.Callable}. In order to |
| 65 |
* be closed and stopped, your the class should implement |
| 66 |
* {@link java.io.Closeable} and its {@code close()} |
| 67 |
* method should be explicitly called |
| 68 |
* at the moment you want it to stop. |
| 69 |
* |
| 70 |
* <p><b>NOTE:</b> It should be pointed out that in order to ensure that there |
| 71 |
* are no duplicate executions, you can only schedule an execution once between |
| 72 |
* all equal objects (i.e. instances that are equal as per |
| 73 |
* {@link Object#equals(Object)})). Invoking the same method multiple times, |
| 74 |
* without stopping it first, will result in an {@link IllegalStateException} |
| 75 |
* being thrown. |
| 76 |
* |
| 77 |
* @author Yegor Bugayenko ([email protected]) |
| 78 |
* @version $Id: 0f0f9614675d072044ea863a4d5f93538845b101 $ |
| 79 |
* @since 0.7.16 |
| 80 |
* @see <a href="http://aspects.jcabi.com">http://aspects.jcabi.com/</a> |
| 81 |
*/ |
| 82 |
@Documented |
| 83 |
@Retention(RetentionPolicy.RUNTIME) |
| 84 |
@Target(ElementType.TYPE) |
| 85 |
public @interface ScheduleWithFixedDelay { |
| 86 |
|
| 87 |
/** |
| 88 |
* Delay, in time units. |
| 89 |
*/ |
| 90 |
int delay() default 1; |
| 91 |
|
| 92 |
/** |
| 93 |
* Time units of delay. |
| 94 |
*/ |
| 95 |
TimeUnit unit() default TimeUnit.MINUTES; |
| 96 |
|
| 97 |
/** |
| 98 |
* How long to wait for the task to finish after shutdown in await units. |
| 99 |
*/ |
| 100 |
int await() default 1; |
| 101 |
|
| 102 |
/** |
| 103 |
* Time units of await time. |
| 104 |
*/ |
| 105 |
TimeUnit awaitUnit() default TimeUnit.MINUTES; |
| 106 |
|
| 107 |
/** |
| 108 |
* How many times to do a forceful shutdown after await time. |
| 109 |
* Each forceful shutdown attempt will be followed by a 1 second wait to |
| 110 |
* allow the threads to finish. |
| 111 |
*/ |
| 112 |
int shutdownAttempts() default 1; |
| 113 |
|
| 114 |
/** |
| 115 |
* Total number of fixed threads. |
| 116 |
*/ |
| 117 |
int threads() default 1; |
| 118 |
|
| 119 |
/** |
| 120 |
* Be less verbose. |
| 121 |
*/ |
| 122 |
boolean verbose() default true; |
| 123 |
} |