23.07.2013 Views

O'Reilly - Java Message Service

O'Reilly - Java Message Service

O'Reilly - Java Message Service

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

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

The Stream<strong>Message</strong> 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 bytes<br />

to <strong>Java</strong> primitives. Primitive types are read from the <strong>Message</strong> in the same order they were<br />

written. Here's the definition of the Stream<strong>Message</strong> interface:<br />

public interface Stream<strong>Message</strong> extends <strong>Message</strong> {<br />

}<br />

public boolean readBoolean( ) throws JMSException;<br />

public void writeBoolean(boolean value) throws JMSException;<br />

public byte readByte( ) throws JMSException;<br />

public int readBytes(byte[] value) throws JMSException;<br />

public void writeByte(byte value) throws JMSException;<br />

public void writeBytes(byte[] value) throws JMSException;<br />

public void writeBytes(byte[] value, int offset, int length)<br />

throws JMSException;<br />

public short readShort( ) throws JMSException;<br />

public void writeShort(short value) throws JMSException;<br />

public char readChar( ) throws JMSException;<br />

public void writeChar(char value) throws JMSException;<br />

public int readInt( ) throws JMSException;<br />

public void writeInt(int value) throws JMSException;<br />

public long readLong( ) throws JMSException;<br />

public void writeLong(long value) throws JMSException;<br />

public float readFloat( ) throws JMSException;<br />

public void writeFloat(float value) throws JMSException;<br />

public double readDouble( ) throws JMSException;<br />

public void writeDouble(double value) throws JMSException;<br />

public String readString( ) throws JMSException;<br />

public void writeString(String value) throws JMSException;<br />

public Object readObject( ) throws JMSException;<br />

public void writeObject(Object value) throws JMSException;<br />

public void reset( ) throws JMSException;<br />

On the surface, the Stream<strong>Message</strong> strongly resembles the Bytes<strong>Message</strong>, but they are not<br />

the same. The Stream<strong>Message</strong> keeps track of the order and types of primitives written to the<br />

stream, so formal conversion rules apply. For example, an exception would be thrown if<br />

you tried to read a long value as a short:<br />

Stream<strong>Message</strong> stream<strong>Message</strong> = session.createStream<strong>Message</strong>( );<br />

stream<strong>Message</strong>.writeLong(2938302);<br />

// The next line throws a JMSException<br />

short value = stream<strong>Message</strong>.readShort( );<br />

While this would work fine with a Bytes<strong>Message</strong>, it won't work with a Stream<strong>Message</strong>. A<br />

Bytes<strong>Message</strong> would write the long as 64 bits (8 bytes) of raw data, so that you could later<br />

read some of the data as a short, which is only 16 bits (the first 2 bytes of the long). The<br />

Stream<strong>Message</strong>, on the other hand, writes the type information as well as the value of the<br />

long primitive, and enforces a strict set of conversion rules that prevent reading the long as<br />

a short.<br />

47

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

Saved successfully!

Ooh no, something went wrong!