background image

The Life Cycle of a Message-Driven Bean

<< The Life Cycle of a Stateful Session Bean | More Information about Enterprise Beans >>
<< The Life Cycle of a Stateful Session Bean | More Information about Enterprise Beans >>

The Life Cycle of a Message-Driven Bean

The client initiates the life cycle by obtaining a reference to a stateless session bean. The
container performs any dependency injection and then invokes the method annotated
@PostConstruct
, if any. The bean is now ready to have its business methods invoked by the
client.
At the end of the life cycle, the EJB container calls the method annotated @PreDestroy, if any.
The bean's instance is then ready for garbage collection.
The Life Cycle of a Message-Driven Bean
Figure 20­5
illustrates the stages in the life cycle of a message-driven bean.
The EJB container usually creates a pool of message-driven bean instances. For each instance,
the EJB container performs these tasks:
1. If the message-driven bean uses dependency injection, the container injects these references
before instantiating the instance.
2. The container calls the method annotated @PostConstruct, if any.
Like a stateless session bean, a message-driven bean is never passivated, and it has only two
states: nonexistent and ready to receive messages.
Does Not
Exist
Ready
1. Dependency injection, if any
2. PostConstruct callbacks, if any
PreDestroy callbacks, if any
FIGURE 20­4
Life Cycle of a Stateless Session Bean
Does Not
Exist
Ready
1. Dependency injection, if any
2. PostConstruct callback, if any
PreDestroy callback, if any
onMessage
FIGURE 20­5
Life Cycle of a Message-Driven Bean
The Life Cycles of Enterprise Beans
Chapter 20 · Enterprise Beans
643