Ejb-timer-service
提供:Dev Guides
EJB-タイマーサービス
タイマーサービスは、スケジュールされたアプリケーションを構築できるメカニズムです。 たとえば、毎月1日の給与明細生成。 EJB 3.0仕様では、@ Timeoutアノテーションが指定されています。これは、ステートレスBeanまたはメッセージ駆動型BeanでEJBサービスをプログラミングするのに役立ちます。 EJBコンテナは、@ Timeoutで注釈が付けられたメソッドを呼び出します。
EJBタイマーサービスは、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();
}
応用例
EJBでタイマーサービスをテストするためのテストEJBアプリケーションを作成しましょう。
| Step | Description |
|---|---|
| 1 | Create a project with a name EjbComponent under a package com.finddevguides.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 the EJB - Create Application chapter under topic Create Client to access EJB. |
EJBComponent(EJBモジュール)
TimerSessionBean.java
package com.finddevguides.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.finddevguides.timer;
import javax.ejb.Remote;
@Remote
public interface TimerSessionBeanRemote {
public void createTimer(long milliseconds);
}
- EjbComponentプロジェクトをJBOSSにデプロイするとすぐに、jbossログに注目してください。
- JBossは、セッションBeanのJNDIエントリを自動的に作成しました- TimerSessionBean/remote 。
- このルックアップ文字列を使用して、タイプのリモートビジネスオブジェクトを取得します- com.finddevguides.timer.TimerSessionBeanRemote
JBoss Application Serverのログ出力
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
TimerSessionBean/remote - EJB3.x Default Remote Business Interface
TimerSessionBean/remote-com.finddevguides.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.finddevguides.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.finddevguides.test;
import com.finddevguides.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"で実行され、リモートビジネスオブジェクト(タイマーステートレスEJB)が取得されます。
- 次に、スケジュール時間として2000ミリ秒を渡してcreateTimerが呼び出されます。
- 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 Application Serverのログ出力
JBossログで次のコールバックエントリを見つけることができます
...
11:35:49,555 INFO [STDOUT] timeoutHandler : Hello World!
...