8.4.4 Message-driven bean life cycle
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:
Message-driven bean instances are created in three steps by the EJB container:
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.
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.