Q

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

How do I use message-driven bean(MDB)?
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();
    publishToResponseQueue(response);
  }

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

Continued in part 2.

This was first published in October 2004

Dig deeper on Java Web Services

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

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:

-ADS BY GOOGLE

SearchSoftwareQuality

SearchCloudApplications

SearchAWS

TheServerSide

SearchWinDevelopment

Close