Java Reference
In-Depth Information
and the value after the slash specifies the interval. Increments can be used only with
second , minute , and hour attributes. The increment executes until it hits the next
unit—if the increment is set on the second, the increment will terminate once the next
minute is reached. Ditto for minute and hour. Wildcards may be used when specifying the
starting position but not following the slash. Let's look at a simple example:
@Schedule(second="30/10", minute="*/20", hour="*/6")
In this example, the timer will fire every 10 seconds, after a delay of 30 seconds, every 20
minutes, within every sixth hour. Thus, starting the timer at midnight would result in it fir-
ing at 06:20:40, 6:40:30, 12:20:40, 12:40:30, and so on. None of the other attributes were
specified, because they don't support increments.
7.3. Using programmatic timers
In the previous section you were introduced to declarative timers using the @Schedule
and @Schedules annotations. Declarative timers are useful when the task to be sched-
uled and its scheduling parameters are known. Declarative cron timers are useful for
tasks such as log rotation, batch processing of records off-hours, and other well-known
schedulable business processes. Ad hoc timers, on the other hand, aren't known in advance
and are created dynamically at runtime as required. Using the ActionBazaar newsletter ex-
ample, hardcoding holiday newsletter broadcasts into the application isn't flexible. Because
ActionBazaar is used in different countries, additional holiday broadcasts will be required.
Obviously recompiling the application for additional holidays would be excessive. A su-
perior approach is to use ad hoc timers and let a marketing administrator schedule newslet-
ter broadcasts at runtime.
Ad hoc timers are thus created programmatically at runtime. Functionally they're every bit
as powerful as the declarative timers. The only difference is that ad hoc timers are cre-
ated programmatically at runtime and each bean is limited to one time-out because only
one method can be annotated with @Timeout . As you'll see, there's a variety of different
methods to choose from when creating a programmatic timer.
 
Search WWH ::




Custom Search