Sun GlassFish Message Queue 4.4 Developer's Guide for Java Clients

Composing Stream Messages

The session method createStreamMessage returns a new, empty stream message. You can then use the methods shown in Table 2–10 to write primitive data values into the message body, similarly to writing to a data stream: for example,

StreamMessage  outMsg = mySession.createStreamMessage();
outMsg.writeString("The Meaning of Life");
outMsg.writeInt(42);
Table 2–10 Stream Message Composition Methods

Name 

Description 

writeInt

Write integer to message stream 

writeByte

Write byte value to message stream 

writeBytes

Write byte array to message stream 

writeShort

Write short integer to message stream 

writeLong

Write long integer to message stream 

writeFloat

Write floating-point value to message stream 

writeDouble

Write double-precision value to message stream 

writeBoolean

Write boolean value to message stream 

writeChar

Write character to message stream 

writeString

Write string to message stream 

writeObject

Write value of object to message stream 

reset

Reset message stream 

As a convenience for handling values whose types are not known until execution time, the writeObject method accepts a string or an objectified primitive value of class Integer, Byte, Short, Long, Float, Double , Boolean, or Character and writes the corresponding string or primitive value to the message stream: for example, the statements

Integer  meaningOfLife = new Integer(42);
outMsg.writeObject(meaningOfLife);

are equivalent to

outMsg.writeInt(42);

This method will throw an exception (MessageFormatException) if the argument given to it is not of class String or one of the objectified primitive classes.

Once you’ve written the entire message contents to the stream, the reset method

outMsg.reset();

puts the message body in read-only mode and repositions the stream to the beginning, ready to read (see Processing Messages). When the message is in this state, any attempt to write to the message stream will throw the exception MessageNotWriteableException. A call to the clearBody method (inherited from the superinterface Message) deletes the entire message body and makes it writeable again.