Skip Headers
Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide
10g (10.1.3.5.0)

Part Number E13981-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

What is a Message-Driven Bean?

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 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.

Figure 1-7 Message Driven Beans

Message Driven Bean Flow of Control
Description of "Figure 1-7 Message Driven Beans"

This section describes the following:

For more information, see the following:

What is the Life Cycle of a Message-Driven Bean?

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.

Figure 1-8 Life Cycle of an EJB 2.1 MDB

Description of Figure 1-8 follows
Description of "Figure 1-8 Life Cycle of an EJB 2.1 MDB"

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

@PostConstruct

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.

@PreDestroy

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

EJB Method Description

ejbCreate

The container invokes this method right before it creates the bean. A message-driven bean must do nothing in this method.

ejbRemove

A container invokes this method before it ends the life of a MDB. Use this method to perform any required clean-up (for example, closing external resources such as file handles).


For more information, see the following:

What is Message Driven Context?

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: