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.
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);
public void onMessage (Message message)
{
ObjectMessage oMessage = (ObjectMessage) message;
DataObject data = oMessage.getObject();
System.out.print(data.getXXX());
System.out.print(data.getYYY());
}