Professional Documents
Culture Documents
This tutorial illustrates how an MDB is written and deployed in an EJB 2.0
Container. The MDB component is invoked by an inbound message from a client. This
is demonstrated with a sample application that is deployable on Pramati Server 3.0.
The application source can be downloaded from www.pramati.com for better
understanding of how MDB components are written.
Structure of an MDB
» MDB has no home or remote interfaces, and is only a bean class.
» A client interacts with the MDB in the same way it interacts with a JMS
application or JMS server.
» Through the MDB, EJB 2.0 container sets itself up as a listener for
asynchronous invocation and directly invokes the bean (no interfaces),
which then behaves like an enterprise bean.
» All instances of a particular MDB type are equivalent as they are not
directly visible to the client and maintain no conversational state. This
means that the container can pool instances to enhance scalability.
Message Driven Beans - A tutorial
MDB Pool
invokes onMessage ()
{
}
Client JMS
Container
picks message
Lifecycle of an MDB
The EJB container performs the following tasks at the beginning of the life cycle
of the MDB:
1 Creates a message consumer (a QueueReceiver or TopicSubscriber) to
receive the messages
2 Associates the bean with a destination and connection factory at deployment
3 Registers the message listener and the message acknowledgment mode
As MDBs are stateless, bean instances are typically pooled by the EJB Server
and retrieved by the container when a message becomes available on the Topic
or Queue.
Writing an MDB
Writing an MDB involves the following tasks:
1 Implement the javax.ejb.MessageDrivenBean and
javax.jms.MessageListener interfaces in the MDB class
2 Provide an implementation of the business logic inside the onMessage()
3 Provide a setMessageDrivenContext() method that associates the bean with
its environment
4 Provide an ejbCreate() method that returns void and takes no arguments.
This method may be blank
5 Provide an ejbRemove() method implementation. This method may be
blank, unless certain resources need to be acquired before the bean goes out
of scope.
2
Message Driven Beans - A tutorial
Sample Application
To illustrate the writing of an MDB, the tutorial uses a sample application,
StockTrader—available for download at www.pramati.com. It uses a simple
message driven bean, buyAgentMDB that is contacted by a client which wishes to
buy shares. The client looks up the BuyQueue and implements
javax.jms.MessageListener. It provides a private method buy() that takes two
arguments, a double value that holds the price and a string stockSymbol that
holds the scrip symbol.
called by the container after the instance creation, with no transaction context
4 The class must not contain
» the finalize method.
Acknowledging Messages
For MDBs that use container-managed transactions, the container automatically
4
Message Driven Beans - A tutorial
The JMSReplyTo header field contains a destination supplied by the client along
with the message. This destination is where a “reply to the message” may be sent.
Messages sent with JMSReplyTo value typically expect a response. The code to
use JMSReplyTo header looks like this:
public void onMessage(Message msg){
try {
destination d = msg.getJMSReplyTo()
}
catch(JMSException jmse){}
}
5
Message Driven Beans - A tutorial
Deployment steps
The following steps will deploy the application on Pramati Server:
1 Start Pramati Server along with the JMS Server (you may refer to
documentation on www.pramati.com for further details on working with
Pramati Server)
2 Start Pramati Management Console from the Windows shortcut. Unix users
must run runadmin.sh from the Server installation directory.
3 In the Console, select Server > Start from the main menu. This brings up the
Start Server dialog where the J2EE Server to be started is identified. Check
the Start JMS Server option to start the Message Server along with the J2EE
Server. This is essential to run the message driven beans.
6
Message Driven Beans - A tutorial
This brings up the stock repository panel, which displays the scrip and the price
along with a Buy/Sell advisory message. Observe the price fluctuations on
buying or selling of stocks.
Summary
MDBs overcome the limitations of synchronous messaging using session and
entity beans in the EJB 1.1 container. MDBs can be deployed in EJB 2.0
container, which acts as a message listener and uses inbound messages as a
trigger to invoke business methods in an asynchronous manner.
Pramati Technologies, based in Hyderabad, India but with significant operations
in the U.S. and abroad, is a global provider of Java software development
technology for small-to-medium sized businesses. Pramati Server and Studio
together are a complete platform developers use to create and deploy applications
based on Sun Microsystems' Java 2 Enterprise Edition platform but at a fraction
of the cost typically required.
Use the sample MDB application with a free full-feature Development License of
World’s First J2EE 1.3 certified Pramati Server from www.pramati.com.
Try out the free evaluation license of Pramati Studio for complete J2EE 1.3
component lifecycle management.
7
Pramati Technologies
www.pramati.com
1270-001 c 06/02