Java Reference
In-Depth Information
In the preceding code snippet, the executeTask method is invoked at 23:59 Central European
time regardless of the time zone of the server on which it is deployed. A call to the method getInfo
returns the text Generates nightly report .
You can coni gure more complex timers using @Schedules (note the pluralization) with multiple
timer expressions.
@Schedules({
@Schedule(dayOfMonth = "1"),
@Schedule(dayOfWeek = "Mon,Tue,Wed,Thu,Fri", hour = "8")
})
public void executeTask() {
System.out.println("Task performed");
}
This timer i res on the i rst of every month and every work day at 08:00. Listing 10‐1 shows a
complete example of an automatic timer.
LISTING 10‐1: The simplest implementation of an automatic timer
package com.devchronicles.timer;
import javax.ejb.Schedule;
import javax.ejb.Schedules;
public class PeriodicTimer {
@Schedules({
@Schedule(dayOfMonth = "1"),
@Schedule(dayOfWeek = "Mon,Tue,Wed,Thu,Fri", hour = "8")
})
public void executeTask() {
System.out.println("Task performed");
}
}
One drawback of the automatic timer is that its schedule is set at deployment time and
cannot be changed while the application is executing. Fortunately, there is a solution to
this situation in the form of the programmatic timer, which you can set at any moment during
run time.
Programmatic Timers
Programmatic timers are created at run time by invoking one of the create methods of the
TimerService interface. Here is a simple example:
public void setTimer(){
timerService.createTimer(30000, "New timer");
}
 
Search WWH ::




Custom Search