Identify correct and incorrect statements or examples about Timer Services, including the bean provider's view and responsibilities, the TimerService, Timer and TimerHandle interfaces, and @Timeout callback methods.

[Note]
  • [EJB_3.0_CORE] 18.1; 18.2; 18.2.1; 18.2.2; 18.2.3; 18.2.4; 18.2.5; 18.3.1; 18.3.2; 4.3.8; 5.4.7

The EJB Timer Service is a container-managed service that provides methods to allow callbacks to be scheduled for time-based events. The container provides a reliable and transactional notification service for timed events. Timer notifications may be scheduled to occur at a specific time, after a specific elapsed duration, or at specific recurring intervals.

The Timer Service is implemented by the EJB container. An enterprise bean accesses this service by means of dependency injection, through the EJBContext interface, or through lookup in the JNDI namespace.

The EJB Timer Service is a coarse-grained timer notification service that is designed for use in the modeling of application-level processes. It is not intended for the modeling of real-time events.

Bean Provider's View of the Timer Service

The EJB Timer Service is a container-provided service that allows enterprise beans to be registered for timer callback methods to occur at a specified time, after a specified elapsed time, or after specified intervals. The timer service provides methods for the creation and cancellation of timers, as well as for locating the timers that are associated with a bean.

A timer is created to schedule timed callbacks. The bean class of an enterprise bean that uses the timer service must provide a timeout callback method. This method may be a method that is annotated with the Timeout annotation, or the bean may implement the javax.ejb.TimedObject interface. The javax.ejb.TimedObject interface has a single method, the timer callback method ejbTimeout.

Timers CAN be created for:

Timers CANNOT be created for:

A timer that is created for a 2.1 entity bean is associated with the entity bean's identity. The timeout callback method invocation for a timer that is created for a stateless session bean or a message-driven bean may be called on ANY bean instance in the pooled state.

When the time specified at timer creation elapses, the container invokes the timeout callback method of the bean. A timer may be cancelled by a bean before its expiration. If a timer is cancelled, the timeout callback method is not called. A timer is cancelled by calling its cancel method.

Invocations of the methods to create and cancel timers and of the timeout callback method are typically made within a transaction.

The timer service is intended for the modelling of long-lived business processes. Timers SURVIVE container crashes, server shutdown, and the activation/passivation and load/store cycles of the enterprise beans that are registered with them.

The Timer Service Interface

The Timer Service is accessed via dependency injection, through the getTimerService method of the EJBContext interface, or through lookup in the JNDI namespace. The TimerService interface has the following methods:

public interface javax.ejb.TimerService {

	public Timer createTimer(long duration, java.io.Serializable info);	
	public Timer createTimer(long initialDuration, long intervalDuration, java.io.Serializable info);
	public Timer createTimer(java.util.Date expiration, java.io.Serializable info);
	public Timer createTimer(java.util.Date initialExpiration, long intervalDuration, java.io.Serializable info);
	
	public Collection getTimers();
	
}
					

The timer creation methods allow a timer to be created as a single-event timer or as an interval timer. The timer expiration (initial expiration in the case of an interval timer) may be expressed either in terms of a duration or as an absolute time.

The bean may pass some client-specific information at timer creation to help it recognize the significance of the timer's expiration. This information is stored by the timer service and available through the timer. The information object must be serializable.

The timer duration is expressed in terms of MILLISECONDS. The timer service begins counting down the timer duration upon timer creation.

The createTimer methods return a Timer object that allows the bean to cancel the timer or to obtain information about the timer prior to its cancellation and/or expiration (if it is a single-event timer).

The getTimers method returns the active timers associated with the bean. For an EJB 2.1 entity bean, the result of getTimers is a collection of those timers that are associated with the bean's identity.

Timeout Callbacks

The enterprise bean class of a bean that is to be registered with the timer service for timer callbacks must provide a timeout callback method.

This method may be a method annotated with the Timeout annotation (or a method specified as a timeout-method in the deployment descriptor) or the bean may implement the javax.ejb.TimedObject interface. This interface has a single method, ejbTimeout. If the bean implements the TimedObject interface, the Timeout annotation or timeout-method deployment descriptor element can only be used to specify the ejbTimeout method. A bean can have AT MOST ONE timeout method.

public interface javax.ejb.TimedObject {

	public void ejbTimeout(Timer timer);
	
}
					

Any method annotated as a Timeout method (or designated in the deployment descriptor as such) MUST have the signature below, where <METHOD> designates the method name. A timeout method can have public, private, protected, or package level access. A timeout method MUST NOT be declared as final or static.


void <METHOD>(Timer timer)
					
					

Timeout callback methods MUST NOT throw application exceptions.

When the timer expires (i.e., after the number of milliseconds specified at its creation expires or after the absolute time specified has passed), the container calls the timeout method of the bean that was registered for the timer. The timeout method contains the business logic that the Bean Provider supplies to handle the timeout event. The container calls the timeout method with the timer that has expired. The Bean Provider can use the getInfo method to retrieve the information that was supplied when the timer was created. This information may be useful in enabling the timed object to recognize the significance of the timer expiration.

The container interleaves calls to the timeout callback method with the calls to the business methods and the life cycle callback methods of the bean. The time at which the timeout callback method is called may therefore not correspond exactly to the time specified at timer creation. If multiple timers have been created for a bean and will expire at approximately the same times, the Bean Provider must be prepared to handle timeout callbacks that are OUT OF SEQUENCE. The Bean Provider must be prepared to handle EXTRANEOUS calls to the timeout callback method in the event that a timer expiration is outstanding when a call to the cancellation method has been made.

In general, the timeout callback method can perform the same operations as business methods from the component interface or methods from the message listener interface.

Since the timeout callback method is an internal method of the bean class, it has NO CLIENT SECURITY CONTEXT. When getCallerPrincipal is called from within the timeout callback method, it returns the container's representation of the unauthenticated identity.

If the timed object needs to make use of the identity of the timer to recognize the significance of the timer expiration, it may use the equals method to compare it with any other timer references it might have outstanding.

If the timer is a single-action timer, the container removes the timer after the timeout callback method has been successfully invoked (e.g., when the transaction that has been started for the invocation of the timeout callback method commits). If the bean invokes a method on the timer after the termination of the timeout callback method, the NoSuchObjectLocalException is thrown.

The Timer Interface

The javax.ejb.Timer interface allows the Bean Provider to cancel a timer and to obtain information about the timer.

public interface javax.ejb.Timer {
	
	public void cancel();
	public long getTimeRemaining();
	public java.util.Date getNextTimeout();
	public javax.ejb.TimerHandle getHandle();
	public java.io.Serializable getInfo();
	
}					
					

The TimerHandle Interface

The javax.ejb.TimerHandle interface allows the Bean Provider to obtain a serializable timer handle that may be persisted. Since timers are LOCAL objects, a TimerHandle MUST NOT be passed through a bean's remote business interface, remote interface or web service interface.

public interface javax.ejb.TimerHandle extends java.io.Serializable {

	public javax.ejb.Timer getTimer();
	
}
					

Timer Identity

The Bean Provider cannot rely on the == operator to compare timers for "object equality". The Bean Provider MUST use the Timer.equals(Object obj) method.

Transactions

An enterprise bean typically creates a timer within the scope of a transaction. If the transaction is then rolled back, the timer creation is rolled back.

An enterprise bean typically cancels a timer within a transaction. If the transaction is rolled back, the container rescinds the timer cancellation.

The timeout callback method is typically has transaction attribute REQUIRED or REQUIRES_NEW (Required or RequiresNew if the deployment descriptor is used to specify the transaction attribute). If the transaction is rolled back, the container retries the timeout.

Enterprise Bean Class

An enterprise bean that is to be registered with the Timer Service must have a timeout callback method. The enterprise bean class may have superclasses and/or superinterfaces. If the bean class has superclasses, the timeout method may be defined in the bean class, or in any of its superclasses.

TimerHandle

Since the TimerHandle interface extends java.io.Serializable, a client may serialize the handle. The serialized handle may be used later to obtain a reference to the timer identified by the handle. A TimerHandle is intended to be storable in persistent storage.

A TimerHandle MUST NOT be passed as an argument or result of an enterprise bean's remote business interface, remote interface, or web service method.

Timeout Callbacks for Stateless Session Beans

A stateless session bean can be registered with the EJB Timer Service for time-based event notifications if it provides a timeout callback method. The container invokes the bean instance's timeout callback method when a timer for the bean has expired. Stateful session beans CANNOT be registered with the EJB Timer Service, and therefore should not implement timeout callback methods.

Timeout Callbacks for Message-Driven Beans

A message driven bean can be registered with the EJB timer service for time-based event notifications if it provides a timeout callback method. The container invokes the bean instance's timeout callback method when a timer for the bean has expired.

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine