Hitachi

uCosminexus Application Server Compatibility Guide


21.3.5 Developing applications by using TimerManager

This section describes development of applications by using TimerManager.

The following table describes the usage status of components, which configure the application, when using TimerManager.

Table 21‒12:  Usage status of components, which configure the application, when using TimerManager

Component

Usage status

EJB client

N

Resource adapter

N

JavaBeans resources

N

Servlet/JSP#

Y

EJB

Stateless Session Bean

EJB2.1 or earlier versions

CMT

Y

BMT

Y

EJB3.0

N

Stateful Session Bean

N

Entity Bean

N

Message-driven Bean

N

Legend:

Y: Can be used

N: Cannot be used

#

You can use the components also with the servlet listener or the filter.

The flow of developing an application by using TimerManager is as follows:

  1. Defining the properties of EJBs or servlets, which are the schedule sources

  2. Implementing the processing to be executed in the listener of TimerManager

  3. Creating EJBs or servlets, which are the schedule sources

  4. Compiling J2EE applications which use TimerManager

Details of each task are as follows:

Organization of this subsection

(1) Defining the properties of EJBs or servlets, which are the schedule sources

Define properties of EJBs or servlets, which use TimerManager, in the DD. You cannot implement the definition for using TimerManager, in annotation.

The following table describes the properties, which you must define to use TimerManager.

Table 21‒13: Properties, which you must define to use TimerManager

Tag name

Explanation

<Root-tag>

--

<description>

Set optionally.

<res-ref-name>

Specify the JNDI ENC name (name to be used for the JNDI lookup).

<res-type>

Set the following value.

commonj.timers.TimerManager

<res-auth>

The set value is ignored.

<res-sharing-scope>

Set Unshareable. However, even if you set Shareable, the same operation as for Unshareable is executed (new TimerManager is created whenever you perform lookup).

<mapped-name>

The set value is ignored.

<injection-target>

The set value is ignored.

<linked-to>

The set value is ignored.

The definition example of web.xml when you use TimerManager in servlet is as follows.

<web-app>
  <display-name>TimerManagerSample</display-name>
  <servlet> 
    <servlet-name>SampleServlet</servlet-name>
    <display-name>SampleServlet</display-name>
    <servlet-class>SampleServlet</servlet-class> 
  </servlet>
  ...
  <resource-ref>
    <res-ref-name>timer/MyTimer</res-ref-name>
    <res-type>commonj.timers.TimerManager</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Unshareable</res-sharing-scope>
  </resource-ref>
</web-app>

TimerManager is created whenever a lookup by JNDI is performed in an application. Created TimerManager is destroyed when you execute the stop method or end the application. You can define multiple TimerManager, as and when required.

(2) Implementing the processing to be executed in the listener of TimerManager

To use TimerManager, you must create a listener, with which the process to be executed is implemented. Listener interfaces consist of- an interface that must be implemented and interfaces to be implemented as and when required. The interface that must be implemented and the interfaces to be implemented if required are as follows:

Interface that must be implemented
  • TimerListener

Interfaces to be implemented as and when required
  • StopTimerListener

  • CancelTimerListener

For details on APIs, see API specifications in Timer and Work Manager for Application Servers.

The example of a class where TimerListener, StopTimerListener, and CancelTimerListener are implemented is as follows.

public class MyTimerListener
        implements TimerListener,StopTimerListener, CancelTimerListener {
    private int count = 0;
    
        public MyTimerListener() {
        }
        public void timerStop(Timer timer) {
                System.out.println("Timer stopped:  " + timer);
        }
 
        public void timerCancel(Timer timer) {
                System.out.println("Timer cancelled:  " + timer);
        }
 
        public void timerExpired(Timer timer) {
                System.out.println("Timer expired !");
                    if(count++ > 10) {
                //Canceled because the set count is reached
                timer.cancel();
            } else {
             System.out.println("The next timer will fire at : " + 
                                   timer.getScheduledExecutionTime());
            }
        }
}

(3) Creating EJBs or servlets, which is the schedule source

To use TimerManager, implement lookup of the JNDI name of TimerManager, which is defined in properties, and the process scheduling of TimerManager, in EJBs or servlets, which are the schedule sources.

Lookup by using JNDI of TimerManager, which is defined in properties

Perform lookup for the JNDI name of TimerManager, which is defined in properties, to acquire TimerManager. Use java:comp/env for lookup. The example of acquiring TimerManager is as follows:

InitialContext ic = new InitialContext();
TimerManager tm = (TimerManager)ic.lookup
                     ("java:comp/env/timer/MyTimer");
Scheduling the TimerManager processes

Schedule the TimerManager processes by invoking the schedule method of TimerManager. The example of scheduling the TimerManager processes is as follows:

InitialContext ctx = new InitialContext();
TimerManager mgr = (TimerManager)
              ctx.lookup("java:comp/env/timer/MyTimer");
TimerListener listener = new MyTimerListener();
mgr.schedule(listener, 1000*60,1000*10);
mgr.stop();

(4) Compiling J2EE applications, which use TimerManager

Include the following JAR file for compiling J2EE applications, which use TimerManager.

Cosminexus-installation-directory\CC\lib\ejbserver.jar