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! ...