The Life Cycle of a Message-Driven Bean
Because an MDB exists in the context of the WebLogic EJB container, its lifetime is also controlled by the container. An MDB initially exists in the
Does Not Exist
state in the WebLogic EJB container. At its instance creation, the bean is placed in a
Method Ready
state in WebLogic's EJB free pool waiting to process a message on the JMS destination (queue or topic) it's assigned to as a message listener. As shown in Figure 22.5, the following steps describe the life cycle of an MDB instance:
Figure 22.5. The life cycle of an MDB.
-
An MDB instance's life starts when the container calls
newInstance()
on the MDB class to create a new instance.
-
The container provides the EJB container context reference to the MDB by calling the
setMessageDrivenContext()
method.
-
The container then calls
ejbCreate()
on the MDB instance to instantiate the bean to the EJB free pool (the Method Ready state). The MDB instance is then ready to receive a message sent via its assigned JMS destination by any client.
Note
During the
ejbCreate()
or
setMessageDrivenContext()
methods
, an MDB can acquire connections to various resources, such as JMS connections. These resources are bound to the MDB as long as it remains in the Method Ready state.
-
When a JMS message is posted to a topic or queue, the container calls the designated MDB's
onMessage()
method. The bean instance in
turn
parses and processes the message. After the method completes, the bean instance is returned to the free pool.
-
The MDB instance goes into the Does Not Exist state from the Method Ready state, when the container decides to reduce the number of bean instances in the free pool, either to
reclaim
the allocated resources or because it no longer needs additional instances of the bean in the free pool. The WebLogic EJB container
removes
the bean from the free pool by calling the bean's
ejbRemove()
method.
When WebLogic Server starts, if MDBs have been deployed to the server, the WebLogic EJB container automatically instantiates multiple instances of the bean, as specified by the value of the
<initial-beans-in-free-pool>
element in the
weblogic-ejb-jar.xml
deployment descriptor. These bean instances are placed in the free pool, ready to service an incoming message on their assigned JMS destinations. Populating the free pool in this manner
improves
the initial response time for the MDB because initial
requests
for the bean can be satisfied without generating a new bean instance.
Note
<initial-beans-in-free-pool>
defaults to 0 if the element is not defined, which implies that WebLogic Server will not prepopulate the free pool with any MDB instances.
The EJB container creates new instances of MDBs as needed for concurrent and parallel message processing. However, the maximum number of MDB instances that can be accommodated in the free pool depends on two factors:
-
The value of the
<max-beans-in-free-pool>
element in the
weblogic-ejb-jar.xml
deployment descriptor, which places an upper boundary on the number of MDB instances the container creates.
-
The number of execution threads available in WebLogic Server to service each message consumption by an MDB. For example, if
<max-beans-in-free-pool>
is set to 20 but only 10 execution threads are available to the EJB container, only 10 of the MDBs actually receive messages.
To learn how to configure WebLogic Server execute threads,
see
"The WebLogic Server Execute Queues,"
p. 1021
, in Chapter 28, "Performance Tuning the WebLogic Server."
Note
The default value of the
<max-beans-in-free-pool>
element enables you to run beans in parallel, using as many threads as possible. The only reason to change this setting is to limit the number of beans running in parallel because of memory/processor constraints that affect the performance or operation of WebLogic Server.
Hence, when a JMS queue or topic receives a message, the WebLogic EJB container calls an associated MDB as
follows
:
-
If a bean instance is available in the free pool, WebLogic Server uses that instance, and the message is consumed through the bean's
onMessage()
method.
-
If no bean instances are available in the free pool because all instances of an MDB class are active and
<max-beans-in-free-pool>
has been reached, the message waits in the JMS destination until an MDB becomes available to
consume
the message.
-
If the free pool is empty and
<max-beans-in-free-pool>
has not been reached, the EJB container creates a new instance of the MDB to consume the message.
|