Sun GlassFish Message Queue 4.4 Technical Overview

JMS/Java EE Programming: Message-Driven Beans

In addition to the general JMS client programming model introduced in Chapter 2, Client Programming Model, there is a more specialized adaptation of a JMS client used in the context of Java EE platform applications. This specialized client is called a message-driven bean and is one of a family of Enterprise JavaBeans (EJB) components described in the EJB 2.0 (and later) Specification (

Message-driven beans provide asynchronous messaging; other EJB components (session beans and entity beans) can only be called synchronously, through standard EJB interfaces. However, enterprise applications often need asynchronous messaging, to allow server-side components to communicate without tying up server resources. Any application whose server-side components must respond to application events needs an EJB component that can receive and consume messages without being tightly coupled to the message producer. In enterprise applications, this capability must also scale under increasing load.

A message-driven bean (MDB) is an EJB component supported by a specialized EJB container, that provides distributed services for the components it supports.

An MDB implements the JMS MessageListener interface. The onMessage method (written by the MDB developer) is invoked when the MDB container receives a message. The onMessage() method consumes the message, just as the onMessage() method of a standard MessageListener object would. (You do not remotely invoke methods on MDBs as you do on other EJB components: therefore there are no home or remote interfaces associated with them.) Each MDB can consume messages from only a single destination. The messages can be produced by standalone JMS applications, JMS components, or EJB components, as shown in the following figure.

Figure 5–1 Messaging with MDBs

Diagram showing JMS message producers sending messages
to consuming MDB instances in a Java EE environment.

A specialized EJB container supports the MDB. This MDB container creates instances of the MDB and sets them up for asynchronous consumption of messages. The container sets up a connection with the broker (including authentication), creates a pool of sessions associated with a given destination, and manages the distribution of messages among the pooled sessions. Since the container controls the life cycle of MDB instances, it manages the pool of MDB instances to accommodate incoming message loads.

Associated with each MDB is a deployment descriptor that specifies the connection factory attributes and destination properties that the container uses in setting up message consumption. The deployment descriptor can also include other information needed by deployment tools to configure the container. Each such container supports all instances of a single MDB.