be marked with the @TimeOut annotation or by implementing the javax.ejb
One important snippet of code that you've not yet seen is how to construct a Sched-
uleExpression . ScheduleExpression , as mentioned earlier, supports method
chaining, thus reducing the number of lines of code necessary to specify a time. To con-
struct an expression for sending out a flyer on Valentine's day right before lunch, the fol-
lowing code snippet would construct the appropriate ScheduleExpression :
ScheduleExpression se = new ScheduleExpression();
se.month(2).dayOfMonth(14).year(2012).hour(11).minute(30); // 2/14/2012 @ 11:30
Using programmatic timers is thus relatively easy. You have the Timer Service injected into
the bean, annotate a method in the bean with the javax.ejb.TimeOut , and schedule
the method using one of the methods on the javax.ejb.TimerService object. This
couldn't be any easier!
7.3.3. Using EJB programmatic timers effectively
Programmatic timers enable operations to be scheduled programmatically at runtime.
They're great for user-driven operations like the example of scheduling a flyer. These are
operations where you can't predict when something will be scheduled. Another example
in ActionBazaar would be the conclusion of bidding on an item. A programmatic timer
could be used to close out bidding and mail the winning bidder notification of their tri-
umph. When bidding is first opened, a new timer is created that will fire when the bidding
is scheduled to conclude. Programmatic timers thus are versatile, and, unlike declarative
timers, they're fluid and can be created and changed at runtime without editing a configur-
ation file or recompiling. In contrast, declarative timers should be used for operations that
are known in development and perform an application maintenance task such as purging
inactive accounts on a monthly basis.
When using programmatic timers, it's important to consider how the timers are being per-
sisted by the application container. Redeploying an application may wipe existing timers.
This would probably be an unintended side effect if you were merely deploying a patch
to fix a bug in production. Therefore, it's important to understand how timer persistence
is impacted by more than just server restarts/failures. What happens when an application
is redeployed or migrated to another server? Application server developers have already