Jay SenSharma

Jay SenSharma

The EJB3 timer service is an EJB-container provided service that allows you to create timers that schedule callbacks to occur when a timer object expires. Timer objects can be created for entity beans, message-driven beans, and stateless session beans. Timer objects expire at a specified time, after an elapsed period of time, or at specified intervals. For instance, you can use the timer service to send out notification when an EJB remains in a certain state for an elapsed period of time.  http://download.oracle.com/docs/cd/E15051_01/wls/docs103/ejb/implementing.  html

Here we are going to create a very simple EJB3 Stateless Timer.

Step1).  Create a Directory somewhere in your filesystem: F:DELETEEJB3_TimerService

Step2).  Create a Directory “EJBTimerTest” inside “F:DELETEEJB3_TimerService

Step3). Now Create 3 folders inside “F:DELETEEJB3_TimerServiceEJBTimerTest” with the following names “META-INF”, “MyEJBApp” and “MyWebApp”.

Step4). Write the “JackTimer.java” interface inside “F:DELETEEJB3_TimerServiceEJBTimerTestMyEJBApp” directory.

package ejbs;
import javax.ejb.*;
public interface JackTimer
public void createTimer();
Step5). Write the StatelessBean “JackTimerBean.java” inside “F:DELETEEJB3_TimerServiceEJBTimerTestMyEJBApp” directory
package ejbs;
import static javax.ejb.TransactionAttributeType.NOT_SUPPORTED;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;
import javax.ejb.Local;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

@Stateless (name="JackTimerBean", mappedName="JackTimerBean")
@Local (JackTimer.class)
@TransactionManagement(value=TransactionManagementType.BEAN )
public class JackTimerBean implements JackTimer

TimerService timerService;
public void createTimer()
System.out.println("ntinside createTimer() ....GOT the injected TimerService: "+timerService);
System.out.println("ntCrating timer");
System.out.println("nttimerService [" + timerService + "]");
Timer timer = timerService.createTimer(20000,"Created new timer");

public void timeout(Timer timer)
System.out.println("ntTimeout occurred !!!");
Step6). compile the above two classes…   (Your EJB3.0 Timer is ready)

javac -d . *.java

Step7). Now move inside your WebApplication “F:DELETEEJB3_TimerServiceEJBTimerTestMyWebApp” and then write “index.jsp” page…

<a href="TestServlet">Click Here to See EJB Injection in a Servlet...(It will Succeed)</a>
Step8). Place the following “web.xml” file indide “F:DELETEEJB3_TimerServiceEJBTimerTestMyWebAppWEB-INF”
<?xml version="1.0"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">



Step9). Now write “TestServlet.java” class inside “F:DELETEEJB3_TimerServiceEJBTimerTestMyWebAppWEB-INFclasses”
package servlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.ejb.EJB;
public class TestServlet extends HttpServlet
private ejbs.JackTimer reconTimer;

public void service (HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
  PrintWriter out=res.getWriter();
  out.println("ntTestServlet service()nt EJBBean injection: reconTimer = "+reconTimer);
  System.out.println("nnt EJBBean injection: reconTimer = "+reconTimer);
Step10). Compile the “TestServlet.java”
javac -d . TestServlet.java
Step11). write “application.xml” file inside… “F:DELETEEJB3_TimerServiceEJBTimerTestMETA-INF” directory
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.4">
<description>Split Directory Example</description>
<display-name>Split Directory Example</display-name>



Step12). Write the following “build.xml” file to deploy and undeploy your Enterprise application “EJBTimerTest”.(your EAR application Name)…
Please place the build.xml file in the following location (Just beside your EAR application): “F:DELETEEJB3_TimerService”
<project name="webservices-hello_world" default="deploy">
<!-- set global properties for this build -->
<property name="wls.username" value="weblogic" />
<property name="wls.password" value="weblogic" />
<property name="wls.hostname" value="localhost" />
<property name="wls.port" value="7001" />
<property name="wls.server.name" value="AdminServer" />

<target name="deploy">
<wldeploy action="deploy"  name="EJBTimerTest" source="EJBTimerTest" user="${wls.username}"
password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" />

<target name="undeploy">
<wldeploy action="undeploy" name="EJBTimerTest" failonerror="false" user="${wls.username}"
password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" />

Step 13). Now open a Shell prompt and then run “setWLSEnv.sh” then move to “F:DELETEEJB3_TimerService” directory in the Shell …and finally run the ANT
Step 14). Hit the URL:    http://localhost:7001/MyWebApp/index.jsp
As soon as you hit the Servlet …you will see that the EJB3.0 Timer created…and after “20000 millisecond” (20-seconds) it will timeout…as mentioned in the Bean class “JackTimerBean.java”.  The output of this program execution you can see in the Server STDOUT.
Jay SenSharma
If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.