uCosminexus Application Server, EJB Container Functionality Guide

[Contents][Glossary][Index][Back][Next]

2.12.7 Implementing an application using the Timer Service

This subsection describes the details of implementation with an API and implementation with an annotation as implementation contents of an application using the Timer Service. This subsection also describes the method of specifying a schedule in the calendar format.

Organization of this subsection
(1) Implementation with API
(2) Implementation with an annotation
(3) Method for specifying a schedule in calendar format

(1) Implementation with API

Implement the following contents:

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.