EJB - 定时服务

定时服务是一种可以构建预定应用程序的机制.例如,每个月1日的工资单生成. EJB 3.0规范指定了@Timeout注释,这有助于在无状态或消息驱动的bean中编写EJB服务. EJB容器调用该方法,该方法由@Timeout注释.

EJB Timer Service是EJB容器提供的服务,它有助于创建计时器并在计时器到期时安排回调.

创建计时器的步骤

使用@Resource注释&minus注入Bean中的SessionContext;

@Stateless
public class TimerSessionBean {

   @Resource
   private SessionContext context;
   ... 
}

使用SessionContext对象获取TimerService并创建计时器.传递时间(以毫秒为单位)和消息.

public void createTimer(long duration) {
   context.getTimerService().createTimer(duration, "Hello World!");
}

使用计时器的步骤

对方法使用@Timeout注释.返回类型应为void并传递Timer类型的参数.我们在第一次执行后取消计时器,否则它将在修复间隔后继续运行.

@Timeout
public void timeOutHandler(Timer timer) {
   System.out.println("timeoutHandler : " + timer.getInfo());        
   timer.cancel();
}

示例应用程序

让我们创建一个测试EJB应用程序来测试EJB中的Timer Service.

Step描述
1

EJB  - 创建应用程序章节中解释,在包 com.it1352.timer 下创建名为 EjbComponent 的项目.

2

创建 TimerSessionBean.java TimerSessionBeanRemote ,如 EJB  - 创建应用程序一章中所述.保持其余文件不变.

3

清理并构建应用程序以确保业务逻辑按照要求运行.

4

最后,在JBoss Application Server上以jar文件的形式部署应用程序.如果JBoss应用服务器尚未启动,它将自动启动.

5

现在创建EJB客户端,一个基于控制台的应用程序,其方式与主题创建 EJB  - 创建应用程序章节中所述的相同客户端访问EJB .

EJBComponent(EJB模块)

TimerSessionBean.java

 
 package com.it1352.timer; 
 import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Timer;
import javax.ejb.Stateless;
import javax.ejb.Timeout;

@Stateless
public class TimerSessionBean implements TimerSessionBeanRemote {

   @Resource
   private SessionContext context;

   public void createTimer(long duration) {
      context.getTimerService().createTimer(duration, "Hello World!");
   }

   @Timeout
   public void timeOutHandler(Timer timer) {
      System.out.println("timeoutHandler : " + timer.getInfo());        
      timer.cancel();
   }
}

TimerSessionBeanRemote.java

 
 package com.it1352.timer; 
import javax.ejb.Remote;

@Remote
public interface TimerSessionBeanRemote {
   public void createTimer(long milliseconds);
}

  • 只要在JBOSS上部署EjbComponent项目,请注意jboss日志.

  • JBoss已自动为我们的会话bean创建了一个JNDI条目 :   TimerSessionBean/remote .

  • 我们将使用此查找字符串来获取类型为&minus的远程业务对象; com.it1352.timer.TimerSessionBeanRemote

JBoss应用服务器日志输出

 
 ... 
 16:30:01,401 INFO [JndiSessionRegistrarBase]绑定全局JNDI中的以下条目:
 TimerSessionBean/remote  - EJB3.x默认远程业务接口
 TimerSessionBean/remote-com.it1352.timer.TimerSessionBeanRemote  -  EJB3.x远程业务接口
 16:30:02,723 INFO [SessionSpecContainer]启动jboss.j2ee:jar = EjbComponent.jar,name = TimerSessionBean,service = EJB3 
 16:30:02,723 INFO [EJBContainer] STARTED EJB:com.it1352.timer.TimerSessionBeanRemote ejbName:TimerSessionBean 
 ...

EJBTester(EJB客户端)

jndi.properties

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost

  • 这些属性用于初始化java命名服务的InitialContext对象.

  • InitialContext对象将用于查找无状态会话bean.

EJBTester.java

 
 package com.it1352.test; 
 import com.it1352.stateful.TimerSessionBeanRemote; 
 import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class EJBTester {

   BufferedReader brConsoleReader = null; 
   Properties props;
   InitialContext ctx;
   {
      props = new Properties();
      try {
         props.load(new FileInputStream("jndi.properties"));
      } catch (IOException ex) {
         ex.printStackTrace();
      }
      try {
         ctx = new InitialContext(props);            
      } catch (NamingException ex) {
         ex.printStackTrace();
      }
      brConsoleReader = 
      new BufferedReader(new InputStreamReader(System.in));
   }
   
   public static void main(String[] args) {

      EJBTester ejbTester = new EJBTester();

      ejbTester.testTimerService();
   }
   
   private void showGUI() {
      System.out.println("**********************");
      System.out.println("Welcome to Book Store");
      System.out.println("**********************");
      System.out.print("Options \n1. Add Book\n2. Exit \nEnter Choice: ");
   }
   
   private void testTimerService() {
      try {
         TimerSessionBeanRemote timerServiceBean = (TimerSessionBeanRemote)ctx.lookup("TimerSessionBean/remote");

         System.out.println("["+(new Date()).toString()+ "]" + "timer created.");
         timerServiceBean.createTimer(2000);            

      } catch (NamingException ex) {
         ex.printStackTrace();
      }
   } 
}

EJBTester正在执行以下任务.

  • 从jndi.properties加载属性并初始化InitialContext对象.

  • 在testTimerService()中方法,jndi查找使用名称 - "TimerSessionBean/remote"来获取远程业务对象(timer无状态EJB).

  • 然后调用createTimer传递调度时间为2000毫秒.

  • EJB容器在2秒后调用timeoutHandler方法.

运行客户端以访问EJB

在项目资源管理器中找到EJBTester.java.右键单击EJBTester类并选择运行文件.

在Netbeans控制台中验证以下输出.

run:
[Wed Jun 19 11:35:47 IST 2013]timer created.
BUILD SUCCESSFUL (total time: 0 seconds)

JBoss应用服务器日志输出

你可以找到JBoss日志中的以下回调条目

...
11:35:49,555 INFO  [STDOUT] timeoutHandler : Hello World!
...