Java Reference
In-Depth Information
7.3.1. Understanding programmatic timers
Ad hoc timers are created via the
javax.ejb.TimerService
. The
TimerService
can either be injected via the
@Resource
annotation or acquired from the
javax.ejb
.SessionContext
. Once acquired,
TimerService
has numerous
createTimer
variations that simplify timer creation. Two types of timers can be created with the
TimerService
: interval timers and calendar timers. Calendar timers are also known as
cron-based timers and were covered earlier. Interval timers made their appearance in EJB
2.1 and are also supported.
getTimers()
method, all other methods are responsible for creating timers. Each meth-
od returns a
javax.ejb.Timer
object. The
Timer
object can be used for retrieving
basic information about the timer, such as whether it's persistent, the time remaining until
it fires, and a method for cancelling the timer. The
getHandle()
method returns a
javax.ejb.TimerHandle
, which supports serialization. The
javax.ejb.Timer
object can't be serialized and thus shouldn't be stored in a member variable of a stateful
session bean, JPA entity, and so on. The
javax.ejb.TimerHandle
provides a method
enabling the current
Timer
instance to be returned.
Listing 7.3.
TimerService
interface