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 J2EE 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 (http://java.sun.com/products/ejb/docs.html).
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 JMS message driven bean is an EJB that 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. The MDB can consume messages from a single destination. The messages can be produced by standalone JMS applications, JMS components, EJB components, or Web components, as shown in Figure 5–1.
A specialized EJB container supports the MDB. It creates instances of the MDB and sets them up for asynchronous consumption of messages. The container sets up a connection with the message service (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 an MDB is a deployment descriptor that specifies the attributes for the connection factory and destinations used by the container 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 instances of a single MDB.