2.12.7 Implementing an application using the Timer Service
(1) Implementation with API
Implement the following contents:
- Specifying the timeout method
Specify the timeout method to be called back with either of the following methods:
- Specify the timeout annotation in the method used as the timeout method.
- Implement the TimedObject interface in the Enterprise Bean. In such a case, the ejbTimeout method defined in the TimedObject interface will become the timeout method.
- Acquiring the javax.ejb.TimerService objects
Acquire the TimerService object using DI, the getTimerService method of the EJBContext interface, or the lookup method of JNDI.
- Generating the EJB timer
Invoke any of the following methods of the TimerService object and implement the code for generating the timer.
- createTimer()
- createSingleActionTimer()
- createIntervalTimer()
- createCalenderTimer()
- Canceling the EJB timer (Timer)
When you must execute the cancellation process, implement the code for canceling the timer. Acquire the timer from javax.ejb.TimerService and javax.ejb.TimerHandle.
The examples of implementing this processing and the precautions during the implementation are as follows:
(a) Example of implementation when DI is used (specify the timeout method with the Timeout annotation)
The following is an example of implementation when an annotation. In this example, the timeout method (myTimeout) is specified with the Timeout annotation (@Timeout).
@Stateless public class TimerSessionBean{
@Resource TimerService timerService;
public void createMyTimer(long intervalDuration){
Timer timer = timerService.createTimer
(intervalDuration, "MyTimer");
}
@Timeout public void myTimeout(Timer timer) {
System.out.println("TimerSessionBean: myTimeout ");
}
public void cancelTimers(){
Collection<Timer> timers = timerService.getTimers();
for(Timer timer: timers) {
timer.cancel();
}
}
} |
(b) Example of implementation when EJBContext is used (implement the TimedObject interface)
The following is an example of acquiring the TimerService object by using SessionContext that is a subclass of EJBContext. In this example, the TimedObject interface is implemented to execute the processing.
public class TimerSessionBean implements SessionBean, TimedObject{
private SessionContext context;
public void createMyTimer(long intervalDuration) {
System.out.println("TimerSessionBean: start createTimer ");
TimerService timerService = context.getTimerService();
Timer timer = timerService.createTimer
(intervalDuration, "MyTimer");
}
public void ejbTimeout(Timer timer) {
System.out.println("TimerSessionBean: ejbTimeout ");
}
public void setSessionContext(SessionContext sc) {
context = sc;
}
} |
(c) Example of implementation when lookup is used (Implement the TimedObject interface)
The following is an example of acquiring the TimerService object using the JNDI. In this example, the TimedObject interface is implemented to execute the processing.
public class TimerSessionBean implements SessionBean, TimedObject{
private SessionContext context;
public void createMyTimer(long intervalDuration) {
System.out.println("TimerSessionBean: start createTimer ");
InitialContext context = new InitialContext();
TimerService timerService =
(TimerService)context.lookup("java:comp/TimerService");
Timer timer = timerService.createTimer
(intervalDuration, "MyTimer");
}
public void ejbTimeout(Timer timer){
System.out.println("TimerSessionBean: ejbTimeout ");
}
} |
(2) Implementation with an annotation
You can automatically generate a timer using the @Schedule annotation.
Examples are as follows:
(a) Example of @Schedule annotation specification
An example of the @Schedule annotation is as follows. In this example, one timer is generated.
// In this example, generateMonthlyAccountStatements method is set
// to be executed at 1:00 on first day of every month with @Schedule
// annotation.
@Schedule(hour="1", dayOfMonth="1",info="AccountStatementTimer")
public void generateMonthlyAccountStatements() { ... } |
Specify any character string in the info attribute of the @Schedule annotation. You can acquire the specified character string with the getInfo method of the related Timer object.
(b) Example of @Schedules annotation specification
An example of the @Schedules annotation specification is as follows. In this example, multiple timers are generated.
// In this example, sendLunchNotification method is set to be executed
// at 12:00 from Monday to Thursday and at 11:00 on Friday with
// @Schedules annotation.
@Schedules (
{ @Schedule(hour="12", dayOfWeek="Mon-Thu"),
@Schedule(hour="11", dayOfWeek="Fri")
}
)
public void sendLunchNotification() { ... } |
You can invoke multiple timers from one callback method with the @Schedules annotation.
(3) Method for specifying a schedule in calendar format
Use values in the coding of a calendar format as follows:
The values that you can specify are based on the standard specifications.
- Specifying a value
Specify a value in seconds or months, as shown below:
Example
second = "10"
month = "Sep"
- Wild card
Specify the wild card as shown below:
Example
second = "*"
dayOfWeek = "*"
- Specifying multiple values
You can specify multiple values by separating the values with ",".
Example
second = "10,20,30"
dayOfWeek = "Mon,Wed,Fri"
minute = "0-10,30,40"
- Specifying the range
You can specify a value for a fixed period using "-".
Example
second="1-10"
dayOfWeek = "Sat-Mon"
The following example shows the range from the 27th day of a month to the 3rd day of the next month. First "-" indicates a time interval between the start time and the end time.
Example
dayOfMonth = "27-3"
This is an example of specifying days. However, you can also specify seconds, minutes, or hours in a similar way.
- Incremental specification
Specify the incremental value with "/". The process is executed every time the value specified as the incremental value increases.
In the following example, the process is executed every 5 minutes.
Example:
minute = "*/5"
This example is same as in the case of specifying "0,5,10,15,20,25,30,35,40,45,50,55".
In the following example, the process is executed every 10 seconds after 30 seconds of each minute.
Example:
second = "30/10"
This example is same as in the case of specifying "second = "30,40,50, ...".
- Note
- If an argument of the createCalendarTimer method is invalid, the IllegalArgumentException exception is thrown and the message KDJE43209-E is output.