EJB - 定時器服務



定時器服務是一種機制,透過它可以構建計劃應用程式。例如,每月1日生成工資單。EJB 3.0 規範指定了@Timeout 註解,這有助於在無狀態或訊息驅動 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,並傳遞型別為 Timer 的引數。我們在第一次執行後取消定時器,否則它將在固定間隔後繼續執行。

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

示例應用程式

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

步驟 描述
1

在包com.tutorialspoint.timer下建立一個名為EjbComponent的專案,如EJB - 建立應用程式章節中所述。

2

建立TimerSessionBean.javaTimerSessionBeanRemote,如EJB - 建立應用程式章節中所述。保持其餘檔案不變。

3

清理並構建應用程式以確保業務邏輯按要求工作。

4

最後,以 jar 檔案的形式將應用程式部署到 JBoss 應用程式伺服器上。如果 JBoss 應用程式伺服器尚未啟動,它將自動啟動。

5

現在建立 EJB 客戶端,一個與EJB - 建立應用程式章節中主題建立訪問 EJB 的客戶端下所述相同方式的基於控制檯的應用程式。

EjbComponent(EJB 模組)

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);
}
  • 一旦您將 EjbComponent 專案部署到 JBOSS 上,請注意 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 客戶端)

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.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 \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 查詢以獲取遠端業務物件(定時器無狀態 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!
...
廣告