例: TimedObject インターフェースを持つタイマー・サービスの使用

この例は、スケジュール済みのイベントが起こるときに呼び出される ejbTimeout() メソッドの実装を示しています。

ベスト・プラクティス ベスト・プラクティス: EJB 3.x のプログラミング・モデルは、ビジネス環境内でパーシスタント・タイマーおよび非パーシスタント・タイマーを定義する追加の戦略的手法を提供します。 TimedObject インターフェースで ejbTimeout メソッドを使用してパーシスタント・タイマーを定義することも可能ですが、実装が簡単な EJB アノテーションを利用して、ビジネス・ニーズに沿ったパーシスタント・タイマーおよび非パーシスタント・タイマーを作成します。アノテーションを使用してパーシスタント・タイマーおよび非パーシスタント・タイマーを作成する方法、あるいはデプロイメント記述子内に独自のタイムアウト・メソッドを定義する方法について詳しくは、エンタープライズ Bean のための EJB タイマー・サービスの使用に関する情報を参照してください。bprac

ejbTimeout メソッドには、通常は Bean のビジネス・メソッドに置かれるコードを含めることができます。 transaction または runAs などのメソッド・レベル属性は、アプリケーション・アセンブラーによってこのメソッドと関連付けることができます。メソッドを開始させるタイマー・オブジェクトのインスタンスは、ejbTimeout メソッドに引数として渡されます。

import javax.ejb.Timer;
import javax.ejb.TimedObject;
import javax.ejb.TimerService;

public class MyBean implements EntityBean, TimedObject {

  // This method is called by the EJB container upon Timer expiration.
  public void ejbTimeout(Timer theTimer) {

    // Any code typically placed in an EJB method can be placed here.

     String whyWasICalled = (String) theTimer.getInfo():
     System.out.println("I was called because of"+ whyWasICalled);
  } // end of method ejbTimeout

ejbTimeout メソッドを 30 秒後に開始するタイマーが作成されます。単一のストリング・オブジェクトは、タイマー作成で渡され、タイマーを識別します。

  // Instance variable to hold the EJB context.
  private EntityContext theEJBContext;

  // This method is called by the EJB container upon bean creation.
  public void setEntityContext(EntityContext theContext) {

    // Save the entity context passed in upon bean creation.
    theEJBContext = theContext;
  } // end of method setEntityContext

  // This business method causes the ejbTimeout method to begin in 30 seconds.
  public void fireInThirtySeconds() throws EJBException  {

    TimerService theTimerService = theEJBContext.getTimerService();
    String aLabel = "30SecondTimeout";
    Timer theTimer = theTimerService.createTimer(30000, aLabel);
  } // end of method fireInThirtySeconds

} // end of class MyBean

トピックのタイプを示すアイコン 参照トピック



タイム・スタンプ・アイコン 最終更新: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=rejb_timerex_v8
ファイル名:rejb_timerex_v8.html