Pass message payload by reference: Example code for producer and consumer applications
Code your JMS applications so that we 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 we 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 our JMS applications that use these properties must strictly follow the rules described or you risk losing data integrity.
Figure 1. Producing and consuming messages
If we 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 we can adapt for use in the 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 we can adapt for use in the application:byte [] data = myByteData; BytesMessage message = session.createBytesMessage(); message.writeBytes(data); data = null; producer.send(message);If we 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 we can adapt for use in the application:
public void onMessage (Message message) { ObjectMessage oMessage = (ObjectMessage) message; DataObject data = oMessage.getObject(); System.out.print(data.getXXX()); System.out.print(data.getYYY()); }
Related:
Why and when to pass the JMS message payload by reference Configure a unified connection factory for the default messaging provider Configure a queue connection factory for the default messaging provider Configure a topic connection factory for the default messaging provider Configure an activation specification for the default messaging provider createSIBJMSActivationSpec command modifySIBJMSActivationSpec command createSIBJMSConnectionFactory command modifySIBJMSConnectionFactory command Default messaging provider unified connection factory [Settings] Default messaging provider queue connection factory [Settings] Default messaging provider topic connection factory [Settings] JMS activation specification [Settings]