Free since 2005 · No login required
AT

Academic Tutorials

Learn at your own pace

site-mobile-top-banner · 320x50

JMS Clients Can Use Java Facilities

Added 31 Jul 2008

JMS clients, since they are based on Java, can make use of existing Java APIs such as JDBC for database access, the JavaBeans components model, JNDI for naming services, JTA for client transaction control, or any of the J2SE and J2EE APIs for enterprise application services.

JMS Details

We now look at the details of building a messaging system client using JMS, starting with messages.

What is a Message?

In messaging systems the point of communication between applications is the message itself, so a developer using JMS must understand messages..

Although the definition of a message varies greatly between messaging systems, JMS provides a unified means of describing and accessing messages. A JMS message consists of three parts:

Message header
For message identification. For example, the header is used to determine if a given message is appropriate for a "subscriber"
Properties
For application-specific, provider-specific, and optional header fields
Body
Holds the content of the message. Several formats are supported, including TextMessages, which wrap a simple String; and ObjectMessages, that wrap arbitrary Java objects (which must be serializable). Other formats are supported as well.

TextMessages

A TextMessage wraps a simple String object. This is useful in situations where only strings are being passed. It is expected that many messaging systems will be based on XML, and TextMessages are a natural container for these.

Creation of a TextMessage object is simple, as these two lines of code indicate:

       TextMessage message = 
session.createMessage();
message.setText("hello world");

(We'll see the "session" object in the next section.)

A TextMessage created in this way is ready to be published to a messaging system.

ObjectMessages

An ObjectMessage, as its name implies, is a message wrapping a Java object. Any serializable Java object can be used as an ObjectMessage. If multiple objects must be transmitted in a single message, then a Collection object (such as a List or a Set) containing several serializable objects can be used.

This is how an Object message is created:

       ObjectMessage message = session.createObjectMessage();
message.setObject(myObject);