Java Reference
In-Depth Information
7.2.1. @Schedule annotation
Placing the
@Schedule
annotation on the method to be used as the time-out creates a de-
clarative timer. The annotation can only be used in singleton beans, stateless session beans,
and message-driven beans. The following code shows the definition of the annotation. If no
attributes are specified, the timer will fire at midnight every day. The
info
attribute can
be used to provide descriptive text that can be retrieved at runtime—it has no effect on the
execution of the timer:
@Target(value=METHOD)
@Retention(value=RUNTIME)
public @interface Schedule {
String dayOfMonth() default "*";
String dayOfWeek() default "*";
String hour() default "0";
String info() default "";
String minute() default "0";
String month() default "*";
boolean persistent() default true;
String second() default "0";
String timezone() default "";
String year() default "*";
}
The method on which the
@Schedule
annotation can be placed has the same require-
ments as the
@Timeout
annotation. The method can't return a value and arguments are
limited to the
javax.ejb.Timer
object discussed earlier. Thus, the following time-outs
are legal method prototypes:
void <METHOD>()
void <METHOD>(Timer timer)
With declarative cron timers, multiple timers can be registered for a single method. This
is accomplished via the
@Schedules
annotation. This annotation takes an array of
@Schedule
annotations. Remember that a timer isn't associated with a specific instance
so it doesn't matter if two or more timers are registered for the same method—both fire
at the same time and each will operate on a completely different instance. When a timer
fires, a new instance will be pulled from the pool; there isn't a one-to-one correspondence
between a timer and a bean.