Developing Message-Driven Beans
This chapter focuses on message-driven beans (MDBs), the newest addition to Enterprise JavaBeans. Specifically, this chapter discusses
the characteristics of MDBs
the elements of MDBs
MessageDrivenBeaninterface
the life cycle of MDBs
comparing MDBs with session and entity beans
how to write, package, deploy, and test MDB applications
deployment descriptors
MDBs, introduced in the EJB 2.0 specification, are the newest type of Enterprise JavaBean and are designed to consume JMS messages. While session and entity beans depend on RMI-IIOP to communicate synchronously with each other, MDBs depend on asynchronous communication for exchanging JMS messages. One of the drawbacks of synchronous communication is that the client making a method invocation on a remote instance is blocked until the remote bean instance responds. A client making RMI-IIOP method invocations expects the remote instance to be available, ready to execute the method, or guaranteed to be made available by the EJB container. If the client doesn't receive a response from the remote object within a reasonable amount of time (usually a few seconds) , the client making the request receives a remote exception. In addition, there are higher resource requirements and more network traffic associated with synchronous communication, but the vast majority of interactive business applications depend on it nonetheless.
There are business applications that don't require synchronous communication and, in fact, may be better suited for asynchronous communication. Asynchronous communication is ideal for the optimized order fulfillment example we discussed in the previous chapter, and for inventory management in a B2B environment. In the B2B scenario, based on business agreements, the application at the product distributor site could automatically generate and se nd a message asynchronously to the supplier's application to replenish the inventory whien it reaches a certain level. The supplier's application would automatically acknowledge the message, inform manufacturing of the quantity and type of product to produce, and ship the product to the distributor.
Characteristics of MDBs
An MDB is implemented as an asynchronous JMS message consumer within the EJB framework. Similar to session and entity bean instances, MDBs implement business logic and execute within an EJB container. MDBs have the following characteristics:
They don't have local or remote home and component interfaces.
MDBs use lightweight, self-contained JMS messages to communicate asynchronously.
Messages can be persistent or nonpersistent, and MDBs can provide guaranteed message delivery.
Because MDBs are stateless, they can be pooled for efficiency and scalability.
MDBs have a single onMessage(msg)business method that's invoked by the container when messages arrive. Message type is checked on arrival.
MDB supports both P2P and pub/sub messaging model.
MDBs can be durable or nondurable subscribers. In a pub/sub messaging domain, when a message arrives and the nondurable subscriber isn't active, the message may not be delivered, but in the case of a durable subscriber, the message is stored and forwarded when the durable subscriber is activated later.
Even if transaction context cannot be shared between a JMS client and an MDB, MDB supports both programmatic and declarative transaction demarcation.
MDBs cannot throw any exceptions to the message sender.