Pass message payload by reference: Example code for producer and consumer applications

Code your JMS applications so that you can safely pass message payloads by reference for asynchronous messaging between producer and consumer applications within a single server.

When large object messages or bytes messages are sent, the cost in memory and processor use of serializing, deserializing, and copying the message payload can be significant. If the producer and consumer applications are in the same JVM and you enable the pass message payload by reference properties on the associated connection factories and activation specifications, message payloads can be passed by reference from producer application to consumer application. This can reduce or bypass the data copying and improve performance and memory use.

In the following figure, messages pass from a JMS producer application, through a producer connection factory, to a queue on a messaging engine. They are then taken off the queue and passed through a consumer connection factory or activation specification, to a JMS consumer application.

CAUTION:
The parts of the JMS Specification that are bypassed by these properties are defined to ensure message data integrity. Any of your JMS applications that use these properties must strictly follow the rules that are described or you risk losing data integrity.
Figure 1. Producing and consuming messages
This figure describes the flow of messages from a JMS producer application to a queue on a messaging engine. Then, the messages are passed onto a JMS consumer application through a consumer connection factory or activation specification properties.
If you enable the producerDoesNotModifyPayloadAfterSet property for the producer connection factory, your producer application must guarantee not to modify the payload object after it has been set into object or bytes messages. To help you achieve this, here is some example code that you can adapt for use in your application:
DataObject data = new DataObject(); 
data.setXXX("xxx"); 
data.setYYY(yyy); 
ObjectMessage message = session.createObjectMessage(); 
message.setObject(data); 
data = null; 
producer.send(message); 
For bytes messages, your producer application must also guarantee to write only a single full byte array into the message. To help you achieve this, here is some example code that you can adapt for use in your application:
byte [] data = myByteData; 
BytesMessage message = session.createBytesMessage(); 
message.writeBytes(data); 
data = null;	 
producer.send(message);
If you enable the consumerDoesNotModifyPayloadAfterGet property for the consumer connection factory or activation specification, your consumer application must guarantee not to modify the payload it gets from the object message (consumption of bytes messages is not affected by the consumerDoesNotModifyPayloadAfterGet property). To help you achieve this, here is some example code that you can adapt for use in your application:
public void onMessage (Message message)
{
   ObjectMessage oMessage = (ObjectMessage) message;
   DataObject data = oMessage.getObject();
   System.out.print(data.getXXX());
   System.out.print(data.getYYY());
}