この例は、スケジュール済みのイベントが起こるときに呼び出される ejbTimeout() メソッドのインプリメンテーションを 示しています。
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; } // 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