Skip Headers
Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide
10g Release 3 (10.1.3)
B14428-01
  Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
Next
Next
 

What is a Message-Driven Bean?

A message-driven bean (MDB) is an EJB 3.0 or EJB 2.1 EJB 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 Providers Can I use with My 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 EJBs 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-3 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-3 Message Driven Beans

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

This section describes:

For more information, see:

What is the Message-Driven Bean Lifecycle?

The lifecycle for EJB 3.0 (see Table 1-17) and EBJ 2.1 (see Table 1-18) message-driven beans are identical. The difference is in how you register lifecycle callback methods.

Table 1-17 lists the optional EJB 3.0 message-driven bean lifecycle callback methods you can define using annotations. For EJB 3.0 message-driven beans, you do not need to implement these methods.

Table 1-17 Lifecycle 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-18 lists the EJB 2.1 lifecycle 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-18 Lifecycle 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:

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: