例: TimedObject インターフェースを持つタイマー・サービスの使用
この例は、スケジュール済みのイベントが起こるときに呼び出される 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;
} // 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