EJB定時器服務


定時器服務使用計劃應用程式可以建立一個機制。例如,每月1日的工資單生成。 EJB3.0規範指定超時注釋,這有助於程式設計一個無狀態或訊息驅動Bean的EJB服務。 EJB容器呼叫的方法,這是注釋@Timeout.

EJB計時器服務是有助於創造的定時器,並安排回撥計時器到期時由EJB容器提供的服務。

建立定時器的步驟

使用@ Resource註解注入SessionContext的bean

@Stateless
public class TimerSessionBean {

   @Resource
   private SessionContext context;
   ...
}

使用SessionContext物件TimerService創造定時器的。傳遞時間(以毫秒為單位)和訊息。

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

使用定時器的步驟

使用@Timeout批註的方法。返回型別必須為void,並傳遞一個引數型別的定時器。我們取消計時器後第一次執行,否則將繼續執行,修正後的時間間隔。

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

範例應用程式

讓我們建立一個測試測試計時器服務在EJB的EJB應用程式中。

Step 描述
1 Create a project with a name EjbComponent under a package com.tutorialspoint.timer as explained in the EJB - Create Application chapter.
2 Create TimerSessionBean.java and TimerSessionBeanRemote as explained in the EJB - Create Application chapter. Keep rest of the files unchanged.
3 Clean and Build the application to make sure business logic is working as per the requirements.
4 Finally, deploy the application in the form of jar file on JBoss Application Server. JBoss Application server will get started automatically if it is not started yet.
5 Now create the ejb client, a console based application in the same way as explained in theEJB - Create Application chapter under topic Create Client to access EJB.

EJBComponent (EJB Module)

TimerSessionBean.java

package com.tutorialspoint.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.tutorialspoint.timer;

import javax.ejb.Remote;

@Remote
public interface TimerSessionBeanRemote {
   public void createTimer(long milliseconds);
}
  • 一旦你在Jboss應用伺服器部署EjbComponent專案,發現jboss紀錄檔。

  • JBoss已經自動為我們的對談bean建立一個JNDI條目 -TimerSessionBean/remote.

  • 我們將使用這個查詢字串來獲得遠端型別的業務物件 -com.tutorialspoint.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.tutorialspoint.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.tutorialspoint.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
  • 這些屬性是用來初始化InitialContext物件的Java命名服務

  • InitialContext物件將被用於查詢無狀態對談bean

EJBTester.java

package com.tutorialspoint.test;
   
import com.tutorialspoint.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 
1. Add Book
2. Exit 
Enter 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”,以獲得遠端業務物件(定時器無狀態EJB)。

  • 然後的呼叫createTimer通過預定時間為2000毫秒。

  • 2秒後EJB容器呼叫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!
...