8.4.4 Message-driven bean life cycle

 

+

Search Tips   |   Advanced Search

 

The EJB container is responsible for hosting and managing message-driven bean instances. It controls the life cycle of the message-driven bean and uses the callback methods within the bean implementation class to notify the instance when important state transitions are about to occur.

The life cycle of a message-driven bean is shown in Figure 8-14.

Figure 8-14 Message-driven bean life cycle

The relevant state transitions for a message-driven bean are:

  1. Message-driven bean creation

    Message-driven bean instances are created in three steps by the EJB container:

    • The EJB container invokes the Class.newInstance() method on the bean implementation class.

    • The EJB container provides the new instance with its MessageDrivenContext reference by invoking the setMessageDrivenContext method.

    • The EJB container gives the new message-driven bean instance the opportunity to perform one-time initialization by invoking the ejbCreate method. The message-driven bean is able to allocate any resources that it requires here.

  2. Message listener method invocation

    Once in the method-ready pool, a message-driven bean instance is available to process any message that is sent to its associated destination or endpoint. When a message arrives at this destination, the EJB container receives the message and allocates a message-driven bean instance from the method-ready pool to process the message. When processing is complete, the message-driven bean instance is returned to the method-ready pool.

    The EJB container performs a number of other operations during the processing of a message, such as ensuring that the processing takes place within the specified transactional context and performing any required security checks. These steps have been omitted for clarity.

  3. Message-driven bean removal

    The EJB container decides at any time that it needs to release resources. To do this, it can reduce the number of message-driven bean instances in the method-ready pool. As part of the removal process, it invokes the ejbRemove method on the instance being removed to give the message-driven bean the opportunity to release any resources that it might be holding.

Next