Oracle Application Server Containers for J2EE 10g (10.0.3)

Date: 08/05/03

How-To: Use a container-managed Timer service in EJB

After completing this how-to you should be able to:

Software Requirements

Notation

Introduction

The EJB 2.1 specification introduced a new feature that allows EJBs a coarse-grained, transactional, time-based event notifications. The timer service can be used to enable EJBs to model and manage higher-level business logic. OC4J supports timers with both entity beans and statelesss sessions. In this example we will demonstrate use of timer with a CMP entity bean. OC4J supports timers for CMP entity beans either using the native Persistence Manager or using the Toplink CMP engine. This sample demonstrates the use timer with CMP entity beans using the Native Persistence Manager.

For example, the timer service can be used do one of the following:

In order to use a container-managed Timer service you have to do the following:

The EJBs

This sample uses EmployeeBean which is CMP entity bean that uses the container-managed Timer service.

Following is code snippet from EmployeeBean.java.


....

public abstract class EmployeeBean implements EntityBean, TimedObject
{

...

public void initializeTimer(long timeout, String info) throws RemoteException
{
try {
TimerService ts = ctx.getTimerService();
ts.createTimer(timeout, info);

System.out.println("Timer created at " + System.currentTimeMillis() +
" with a timeout: " + timeout + " and with info: " + info);
} catch (Exception e) {
}
return;
}

...

public void ejbTimeout(Timer timer)
{
System.out.println("ejbTimeout() called at: " + System.currentTimeMillis() +
" with info: " + timer.getInfo());
return;
}
}

 


Expose the initializeTimer method in the remote interface of entity bean; Employee.java.

public interface Employee extends EJBObject
{
...
public void initializeTimer(long timeout, String info) throws RemoteException;
}

An Example

The example contains an EJB, and one JSP. EmployeeBean uses the EJB Timer Service. This example is based on the EMP table in the SCOTT schema.

 

Configuring your environment

We have provided a build script for building the application using Ant. Alternatively you can use the pre-bulit enterprise archive file  ejbtimer-app.ear located in the lib directory. This example assumes the default password for your application to be welcome. If you have a different password, please modify the password in the ant build script i.e. build.xml as follows:

<property name="deploy.password" value="ToYourPassword" />

Using Ant and admin.jar

  1. Ensure Ant 1.4.x or above is installed on your machine and configured correctly.

  2. Set JAVA_HOME and OC4J_HOME environment variables.  Note: On some operating systems Ant does not currently support the use of environment variables,  if this is the case for your operating system,  please modify the common.xml file located in the root example directory.

  3. From the example application root directory [the directory where the build.xml file is located] execute the following command:  

    ant

  4. Your should now have a newly created ejbtimer-app.ear file in your <example_home >/lib directory .


  5. Deploy this J2EE application to a running instance of OC4J 10.0.3 by executing the following commands from the J2EE_HOME directory. This assumes the default password for OC4J to be welcome.

   ant deploy-usingadmin.jar
                               

     

You should be now ready to run this demo application.

Running the application

  1. Test the application by accessing the web app at the URL http://<hostname>:8888/ejbtimer/

  2. After the welcome page is displayed you can enter the employee information and timer information and and click on the Submit button and you will see the Employee record and timer successfully processed.
  3. Now look at the OC4J console that the EJB timer be successfully created and ejbTimeout method be invoked after the timer was expired.

Summary

In this document you should have: