Oracle GlassFish Message Queue 4.4.2 Developer's Guide for Java Clients

Composing Object Messages

The ObjectMessage interface provides just one method, setObject (Table 2–12 ), for setting the body of an object message:

ObjectMessage  outMsg = mySession.createObjectMessage();

The argument to this method can be any serializable object (that is, an instance of any class that implements the standard Java interface Serializable). If the object is not serializable, the exception MessageFormatException will be thrown.

Table 2–12 Object Message Composition Method




Serialize object to message body 

As an alternative, you can initialize the message body directly when you create the message, by passing an object to the session method createObjectMessage:

ObjectMessage  outMsg = mySession.createObjectMessage(bodyObject);

Again, an exception will be thrown if the object is not serializable.