Schedulers & Timers
EJBs have had support for programmatic timers since version 2.1. This was made possible by use of a combination of javax.ejb.TimerService
interface along with the @Timeout
annotation (which will be covered later). The @Schedule
and @Schedules
annotations were added in EJB 3.1. Let's look at how these annotations have made working with timers much more easier.
@Schedule
@Schedule
This annotation is used to designate a method as a target for a timed call back i.e. the EJB container invokes this method as per configuration/metadata provided in this annotation.
Please note that, in the above example, the
@Schedule
annotation is not configured with the schedule frequency. In such a scenario, the EJB container invokes (by default) the annotated method on a daily basis (at midnight)
@Schedule
exposes a powerful cron-like syntax which provides a number of permutations and combinations are available as far the scheduling configurations are concerned. The syntax is so rich that it is not possible to enumerate all the possible combinations. Here are a few selective examples
@Schedules
@Schedules
This annotation is to be used when you want to impose/declare multiple timers scheduling options to single method. All it does it accept multiple (an array) of @Schedule
annotations
This is useful if you do not want to declare different methods for different timer configurations (although it's perfectly legal to do so)
Timer persistence
Timers are persistent by default i.e. their state is persisted in case the application terminates. One can toggle this using the persistent property in the
@Schedule
annotation
@Timeout
@Timeout
This annotation is useful when you want to adopt a programmatic way of working with timers rather than configuring them in declarative fashion using the annotations mentioned above. @Timeout
is used a marker for the EJB container to be able to determine the trigger point which can be invoked when the timer expires. The method annotated with @Timeout
contains the actual logic which you want to execute as part of your scheduled operation
If you compare this with @Schedule
, the major difference you would notice is that @Timeout
does not allow declarative configuration via annotation elements
Since this annotation does not allow timer/scheduler configuration, it is used in conjunction with the javax.ejb.TimerService
API which helps you create timers on the fly. It's helper method let you specify the timers in terms of duration, exact trigger date etc. as well as ad-hoc schedules (similar to what @Schedule
offers) using the javax.ejb.ScheduleExpression
class
What's next ??
Next, we'll look at EJBs in their Web Service avatar !
Last updated