Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g (10.1.3.1.0) Part Number B28221-02 |
|
|
View PDF |
A message-driven bean (MDB) is an EJB 3.0 or EJB 2.1 enterprise bean component that functions as an asynchronous message consumer. An MDB has no client-specific state but may contain message-handling state such as an open database connection or object references to another EJB. A client uses an MDB to send messages to the destination for which the bean is a message listener.
Using OC4J, you can use an MDB with a variety of message providers (see "What Message Service Providers Can you use With Your MDB?"). You associate the MDB with an existing message provider and the container handles much of the setup required, as follows:
The EJB container creates a consumer of type QueueReceiver
or TopicSubscriber
for the listener.
At deployment time, the EJB container registers the MDB with the consumer, which is either a QueueReceiver
or TopicSubscriber
, and its factory.
The EJB container specifies the message acknowledgment mode.
The EJB container dequeues messages and passes them to the MDB using its message listener method.
The EJB container sends an acknowledgment (if configured to do so).
The purpose of an MDB is to exist within a pool and to receive and process incoming messages from a message provider. The container invokes a bean from the queue to handle each incoming message from the queue. No object invokes an MDB directly: all invocation for an MDB comes from the container. After the container invokes the MDB, it can invoke other enterprise beans or Java objects to continue the request.
A MDB is similar to a stateless session bean, because it does not save conversational state and is used for handling multiple incoming requests. Instead of handling direct requests from a client, MDBs handle requests placed on a queue. Figure 1-7 demonstrates this by showing how clients place requests on a queue. The container takes the requests off of the queue and gives the request to an MDB in its pool.
This section describes the following:
For more information, see the following:
Figure 1-8 shows the life cycle of a message-driven bean. Annotations (such as @PostConstruct
) are applicable to EJB 3.0 message-driven beans only.
The life cycle for EJB 3.0 (see Table 1-18) and EBJ 2.1 (see Table 1-19) message-driven beans are identical. The difference is in how you register life cycle callback methods.
Table 1-18 lists the optional EJB 3.0 message-driven bean life cycle callback methods you can define using annotations. For EJB 3.0 message-driven beans, you do not need to implement these methods.
Table 1-18 Life Cycle Methods for an EJB 3.0 Message-Driven Bean
Annotation | Description |
---|---|
This optional method is invoked for a message-driven bean before the first business method invocation on the bean. This is at a point after which any dependency injection has been performed by the container. |
|
This optional method is invoked for a message-driven bean when the instance is in the process of being removed by the container. The instance typically releases any resources that it has been holding. |
Table 1-19 lists the EJB 2.1 life cycle methods, as specified in the javax.ejb.MessageDrivenBean
interface, that a message-driven bean must implement. For EJB 2.1 message-driven beans, you must at the least provide an empty implementation for all callback methods.
Table 1-19 Life Cycle Methods for an EJB 2.1 Message-Driven Bean
For more information, see the following:
OC4J maintains a javax.ejb.MessageDrivenContext
for each message-driven bean instance and makes this message-driven context available to the beans. The bean may use the methods in the message-driven context to make callback requests to the container.
In addition, you can use the methods inherited from EJBContext
(see "What is EJB Context?").
For more information, see the following: