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.
當傳送大型物件訊息或位元組訊息時,為了序列化、解除序列化及複製訊息有效負載,可能會耗用大量的記憶體和處理器成本。 如果生產者和消費端應用程式是在相同 JVM 中,且您在相關聯的 Connection Factory 和啟動規格上啟用了 pass message payload by reference 內容,就可以從生產者應用程式中,將訊息有效負載依參照傳遞到消費端應用程式。 如此一來,便能縮減或略過資料的複製,而增進效能和記憶體的使用。
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:
定義這些內容所略過的 JMS 規格部分,以確保訊息資料的完整性。 任何使用這些內容的 JMS 應用程式必須嚴格遵守所述的規則,否則可能喪失資料完整性。
Figure 1. Producing and consuming messages

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());
}