23.07.2013 Views

O'Reilly - Java Message Service

O'Reilly - Java Message Service

O'Reilly - Java Message Service

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />

A message basically has two parts: a header and payload. The header is comprised of<br />

special fields that are used to identify the message, declare attributes of the message, and<br />

provide information for routing. The difference between message types is determined<br />

largely by their payload, i.e., the type of application data the message contains. The<br />

<strong>Message</strong> class, which is the superclass of all message objects, has no payload. It is a<br />

lightweight message that delivers no payload but can serve as a simple event notification.<br />

The other message types have special payloads that determine their type and use:<br />

<strong>Message</strong><br />

This type has no payload. It is useful for simple event notification.<br />

Text<strong>Message</strong><br />

This type carries a java.lang.String as its payload. It is useful for exchanging<br />

simple text messages and also for more complex character data, such as XML<br />

documents.<br />

Object<strong>Message</strong><br />

This type carries a serializable <strong>Java</strong> object as its payload. It's useful for exchanging<br />

<strong>Java</strong> objects.<br />

Bytes<strong>Message</strong><br />

This type carries an array of primitive bytes as its payload. It's useful for exchanging<br />

data in an application's native format, which may not be compatible with other<br />

existing <strong>Message</strong> types. It is also useful where JMS is used purely as a transport<br />

between two systems, and the message payload is opaque to the JMS client.<br />

Stream<strong>Message</strong><br />

This type carries a stream of primitive <strong>Java</strong> types (int, double, char, etc.) as its<br />

payload. It provides a set of convenience methods for mapping a formatted stream of<br />

bytes to <strong>Java</strong> primitives. It's an easy programming model when exchanging primitive<br />

application data in a fixed order.<br />

Map<strong>Message</strong><br />

This type carries a set of name-value pairs as its payload. The payload is similar to a<br />

java.util.Properties object, except the values must be <strong>Java</strong> primitives or their<br />

wrappers. The Map<strong>Message</strong> is useful for delivering keyed data.<br />

2.1.3 Sessions and Threading<br />

The Chat application uses a separate session for the publisher and subscriber, pubSession<br />

and subSession, respectively. This is due to a threading restriction imposed by JMS.<br />

According to the JMS specification, a session may not be operated on by more than one<br />

thread at a time. In our example, two threads of control are active: the default main thread<br />

of the Chat application and the thread that invokes the on<strong>Message</strong>( ) handler. The thread<br />

that invokes the on<strong>Message</strong>( ) handler is owned by the JMS provider. Since the invocation<br />

of the on<strong>Message</strong>( ) handler is asynchronous, it could be called while the main thread is<br />

31

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!