目录

EJB - 定时服务( Timer Service)

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

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

创建计时器的步骤

使用@Resource注释在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();
}

例子 Example Application

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

描述
1EJB - Create Application章节中解释,在com.iowiki.timer包下创建一个名为EjbComponent的项目。
2 按照EJB - Create Application章节中的说明创建TimerSessionBean.javaTimerSessionBeanRemote 。 保持其余文件不变。
3 清理并构建应用程序以确保业务逻辑按照要求运行。
4 最后,在JBoss Application Server上以jar文件的形式部署应用程序。 如果JBoss Application服务器尚未启动,它将自动启动。
5 现在创建EJB客户端,一个基于控制台的应用程序,其方式与EJB - Create Application章节中主题Create Client to access EJB

EJBComponent (EJB Module)

TimerSessionBean.java

package com.iowiki.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.iowiki.timer;
import javax.ejb.Remote;
@Remote
public interface TimerSessionBeanRemote {
   public void createTimer(long milliseconds);
}
  • 只要在JBOSS上部署EjbComponent项目,请注意jboss日志。

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

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

JBoss应用服务器日志输出

...
16:30:01,401 INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
   TimerSessionBean/remote - EJB3.x Default Remote Business Interface
   TimerSessionBean/remote-com.iowiki.timer.TimerSessionBeanRemote - EJB3.x Remote Business Interface
16:30:02,723 INFO  [SessionSpecContainer] Starting jboss.j2ee:jar=EjbComponent.jar,name=TimerSessionBean,service=EJB3
16:30:02,723 INFO  [EJBContainer] STARTED EJB: com.iowiki.timer.TimerSessionBeanRemote ejbName: TimerSessionBean
...   

EJBTester (EJB Client)

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.iowiki.test;
import com.iowiki.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()方法中,使用名称“TimerSessionBean/remote”完成jndi查找以获取远程业务对象(timer statelessless EJB)。

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

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

运行客户端以访问EJB

在项目资源管理器中找到EJBTester.java。 右键单击EJBTester类并选择run file

在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!
...
↑回到顶部↑
WIKI教程 @2018