Pass message payload by reference: Usage scenarios and example code for forwarding applications

JMS 轉遞應用程式會接收訊息(透過 Connection Factory,如果是訊息驅動 Bean,則透過啟動規格),然後將訊息物件傳送到另一個目的地。 Explore the different usage scenarios, then code your JMS forwarding applications so that you can safely pass message payloads by reference when forwarding messages from one queue to another within a single server.

當傳送大型物件訊息或位元組訊息時,為了序列化、解除序列化及複製訊息有效負載,可能會耗用大量的記憶體和處理器成本。 如果您在 Connection Factory 或啟動規格上啟用 pass message payload by reference 內容,您會通知預設傳訊提供者置換 JMS 1.1 規格,這項資料的複製有可能會縮減或略過。

In the following figure, messages pass from queue1 on a messaging engine, through a consumer activation specification or connection factory, to a JMS forwarding application. They are then forwarded through a producer connection factory to queue2 on the same messaging engine.

CAUTION:
定義這些內容所略過的 JMS 規格部分,以確保訊息資料的完整性。 任何使用這些內容的 JMS 應用程式必須嚴格遵守所述的規則,否則可能喪失資料完整性。
Figure 1. Forwarding messages
The figure illustrates the flow of messages from one queue to another. From queue1, the messages pass to the JMS forwarding application through the consumer activation specification or connection factory property. From the JMS forwarding application, the messages are then forwarded to queue2 through the producer connection factory property.
To understand the usage scenarios and associated example code given in this topic, you must note these important characteristics of a JMS forwarding application:
  • A forwarding application does not replace the message object. This is useful if your application is just logging or otherwise recording (for example, printing out) the message before forwarding it, and also means that the forwarded message retains some useful message properties such as the JMSCorrelationID, JMSReplyTo and JMSType properties.
  • A forwarding application can modify or replace the message payload. If it replaces the payload, it sets the new payload in the message object and changes the payload reference to point to the new message payload.
  • For a forwarding application, the forwarded message is "created" and configured by the consumer connection factory or activation specification. The producer connection factory is used solely to route the forwarded message and has no effect upon the contents of the forwarded message.

The following table describes the four forwarding application usage scenarios that affect how you set the "pass message payload by reference" properties. Note that, because the producer connection factory has no effect upon the contents of the forwarded message, you set both the consumer properties and the producer/forwarder properties on the consumer connection factory or activation specification.

Table 1. Effect of the "pass message payload by reference" property settings on the forwarding application usage scenarios. The first column of the table lists the four forwarding application usage scenarios. The second column indicates the consumer property setting for the scenarios. The third column indicates the connection or the activation specification property setting for the scenarios.
Forwarding application usage scenario

consumerDoesNotModify
PayloadAfterGet

property setting

producerDoesNotModify
PayloadAfterSet

(for connection factories) or

forwarderDoesNotModify
PayloadAfterSet

(for activation specifications) property setting
Scenario 1: The application receives a message, looks at the payload but does not modify it, and forwards the message on without modifying or replacing the payload. Enabled Not required, but can be enabled
Scenario 2: The application receives a message, looks at the payload but does not modify it, replaces the payload in the message with a new payload and forwards the message on without modifying the payload after the call to set it into the message. Enabled Enabled
Scenario 3: The application receives a message, looks at and modifies the payload, then sets the modified payload or some other data back into the message and forwards the message on without further modifying the payload after the call to set it into the message. NOT enabled Enabled
Scenario 4: The application receives a message, looks at and modifies the payload, then sets the modified payload or some other data back into the message, then further modifies the payload after the call to set it into the message. NOT enabled NOT enabled

For scenarios 1, 2 and 3 you can enable one or more of the "pass message payload by reference" properties, provided that your forwarding application can guarantee to behave as described in the scenario. To help you achieve this, here is some example code that you can adapt for use in your applications.

Forwarding application: scenario 1

The application receives a message, looks at the payload but does not modify it, and forwards the message on without modifying or replacing the payload.

public void onMessage (Message message)
{
   ObjectMessage oMessage = (ObjectMessage) message;
   DataObject data = oMessage.getObject();
   System.out.print(data.getXXX());
   System.out.print(data.getYYY());

   // get a session to forward on the received message
   
   producer.send(message);
   session.close();
}

Forwarding application: scenario 2

The application receives a message, looks at the payload but does not modify it, replaces the payload in the message with a new payload and forwards the message on without modifying the payload after the call to set it into the message.

public void onMessage (Message message)
{
   ObjectMessage oMessage = (ObjectMessage) message;
   DataObject data = oMessage.getObject();
   System.out.print(data.getXXX());
   System.out.print(data.getYYY());

   // get a session to forward on the received message
   
   message.setObject(newData);

   producer.send(message);
   session.close();
}
如果是位元組訊息,您的應用程式也必須保證只會將單一完整位元組陣列寫入訊息。
byte [] data = myByteData;
BytesMessage message = session.createBytesMessage();
message.writeBytes(data);
data = null;	
producer.send(message);

Forwarding application: scenario 3

The application receives a message, looks at and modifies the payload, then sets the modified payload or some other data back into the message and forwards the message on without further modifying the payload after the call to set it into the message.

public void onMessage (Message message)
{
   ObjectMessage oMessage = (ObjectMessage) message;
   DataObject data = oMessage.getObject();
   System.out.print(data.getXXX());
   System.out.print(data.getYYY());

   // get a session to forward on the received message

   data.setXXX(xxx);
   data.setYYY(yyy);
   message.setObject(data);

   producer.send(message);
   session.close();
}
如果是位元組訊息,您的應用程式也必須保證只會將單一完整位元組陣列寫入訊息。
byte [] data = myByteData;
BytesMessage message = session.createBytesMessage();
message.writeBytes(data);
data = null;	
producer.send(message);

指出主題類型的圖示 概念主題



時間戳記圖示 前次更新: July 9, 2016 11:10
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=cjn_passbyref_forward
檔名:cjn_passbyref_forward.html