TABLE 10-2: Examples of the Four Timer Creation Methods
createIntervalTimer(new Date(), 10000,
This creates a timer that i res at the given date
and then every ten seconds thereafter.
This creates a timer that i res after one second.
createTimer(30000, "Created new
This creates a timer that i res after 30 seconds.
This creates a timer that i res every ten
All methods apart from the createCalendarTimer method can take as the i rst parameter either
duration in milliseconds or a date. This sets up the point at which the timer is triggered. Here is an
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy 'at' HH:mm");
Date date = formatter.parse("26/01/2015 at 17:56");
timerService.createSingleActionTimer(date, new TimerConfig());
In this code snippet, the “timeout” method is triggered at 17:56 on January 26, 2015.
If a scheduled timer is required, you can use the createCalendarTimer method. This method takes
a ScheduleExpression object in which a schedule is set using the values described in the “Timer
Expression” section that follows.
ScheduleExpression expression = new ScheduleExpression();
In this code snippet, the schedule is set to trigger every ten seconds of every minute of every hour.
All the creation methods return a Timer object that represents the timer. This object has the method
getHandle , which returns a serializable handle to the timer. The handle object can be persisted in a
database or memory.
Later you can retrieve the handle object and return a reference to the timer by invoking the
getTimer method. With this object in hand, you can retrieve useful information about the timer.
It's easy to obtain information about the behavior of the timer. You can retrieve details regarding
the timer's schedule by calling the method getSchedule . This returns a ScheduleExpression object
that has a getter method for each attribute. For example, getMinute() returns the value set for the
minute attribute. The getNextTimeout method gets the point when the timer i res next, whereas the
method getTimeRemaining returns the milliseconds before the timer expires.
The isCalendarTimer method returns true if the timer was set by constructing a
ScheduleExpression object. You must call it before the getSchedule method to determine if