Ask the Expert

How do I use message-driven bean (MDB)?, part 1

How do I use message-driven bean(MDB)?

    Requires Free Membership to View

An increasingly important requirement of J2EE application systems is their ability to handle asynchronous messages passed from message-oriented middleware (MOM) systems. Java provides the Java Message Service (JMS) for handling MOM messages.

JMS is an application programming interface (API) abstraction of common concepts found in all MOM systems. JMS does not define wire protocols or message-content formats.

The single parameter to the onMessage (Message msg) method is of type javax.jms.Message and it is up to the MDB programmer to cast the Message object to one of the five JMS message types: TextMessage, ObjectMessage, MapMessage, BytesMessage and StreamMessage.

You can create your own asynchronous, JMS-enabled, message listener/receiver and make calls to a service-logic object from its onMessage callback, as in the following example:

public class MyJMSReceiver implements MessageListener

  public MyJMSReceiver()
    // initialize JMS topic/queue connection here

  public void onMessage(Message msg)
    // Message received
    BusinessService busService = unmarshalMessage(msg);
    Document response = busService.execute();

  private BusinessService unmarshalMessage(Message msg)
    // perform message-to-service conversion here
    return aBusinessServiceInstance;

Continued in part 2.

This was first published in October 2004

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: