Java Reference
In-Depth Information
The methods for creating a calendar-based timer accept a
javax.ejb.Schedule-
Expression
and optionally a
javax.ejb.TimerConfig
object. The
Sched-
uleExpression
specifies when the timer should fire and has methods for configuring
dates, times, and so on. The second block of methods creates the interval-based timers,
which are timers that execute after a fixed period of time and possibly repeatedly. The third
block creates single-action timers, which are timers that fire only once after a speci-
fied time period. Many of the methods optionally accept an
info
object that can be null
if you don't wish to use it. The only requirement on the object passed in is that it must
implement the
java.io.Serializable
interface; otherwise everything else is up to
you. The
info
object is a container for you to pass information to the timer when it ex-
ecutes—whatever information you choose.
Not mentioned in the discussion is the
getTimers()
method, which returns a collection
of all outstanding timers. You can iterate over this list, cancel outstanding timers, and find
out when the next timer will execute. One thing to keep in mind is that the timers may
execute by the time you retrieve them from the
TimerService
—be careful not to intro-
duce race conditions by depending on your ability to reach in and cancel timers before they
execute.
Listing 7.3
referenced two additional classes:
javax.ejb.ScheduleExpression
and
javax.ejb.TimerConfig
. The
ScheduleExpression
class is used to spe-
cify when the timer should fire. It has a no-argument constructor and methods for setting
the day of the week, month, year, hour, second, and so on. Each method returns the same
ScheduleExpression
instance enabling chaining so that the schedule can be defined
on a single line. The
TimerConfig
object is a container for additional timer configura-
tion information. It encapsulates the
info
object to be passed back to a time-out method