예: TimedObject 인터페이스에서 타이머 서비스 사용
이 예는 예정된 이벤트 발생 시 호출되는 ejbTimeout() 메소드의 구현을 보여줍니다.

ejbTimeout 메소드는 일반적으로 Bean의 비즈니스 메소드에 위치하는 모든 코드를 포함할 수 있습니다. 트랜잭션 또는 runAs 같은 메소드 레벨 속성은 애플리케이션 어셈블러에 의해 이 메소드와 연관될 수 있습니다. 메소드가 실행하게 만드는 Timer 오브젝트의 인스턴스가 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
30초 안에 ejbTimeout 메소드를 시작하는 타이머가 작성됩니다. 타이머 작성 시 타이머를 식별하기 위한 간단한 문자열 오브젝트가 전달됩니다.
// 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