我正在尝试在我的系统从入站队列收到的消息上设置记帐令牌。我使用以下内容设置此令牌。
msg.setObjectProperty(JmsConstants.JMS_IBM_MQMD_ACCOUNTINGTOKEN,value)
此外,我的JmsSUpport类中还有以下属性
((JmsDestination) dest).setBooleanProperty(WMQConstants.WMQ_MQMD_READ_ENABLED, true);
((JmsDestination) dest).setBooleanProperty(WMQConstants.WMQ_MQMD_WRITE_ENABLED, true);
((MQDestination) dest).setMQMDWriteEnabled(true);
((MQDestination) dest).setMQMDReadEnabled(true);
((MQDestination) dest).setTargetClient(JMSC.MQJMS_CLIENT_NONJMS_MQ);
由于出站队列是MQDestination,我必须设置上述属性。现在我观察到我正在设置出站消息的值。但是当另一个应用程序从MQ读取消息时,它具有Accounting Token的默认值。这可能是因为MQ以这种方式配置来覆盖值吗?或者是因为其他应用程序没有正确读取MQ消息?或者我应该使用任何其他属性来启用会计令牌吗?
是因为我正在设置((MQDestination)dest).setTargetClient(JMSC.MQJMS_CLIENT_NONJMS_MQ); msg被剥夺了MQMD Headers?
答案 0 :(得分:2)
不,MQMD永远不会被剥夺。您必须在发送消息之前在目标上设置标识上下文。否则,队列管理器将忽略Accounting标记。请参阅示例代码:
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import com.ibm.msg.client.jms.JmsConnectionFactory;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import com.ibm.msg.client.jms.JmsDestination;
import com.ibm.msg.client.wmq.WMQConstants;
import com.ibm.msg.client.jms.JmsConstants;
import com.ibm.mq.jms.MQDestination;
public class AccountingTokenDemo {
public static void main(String[]args) {
// TODO Auto-generated method stub
AccountingTokenDemo demo = new AccountingTokenDemo();
demo.putMessageWithAccountingToken();
}
public void putMessageWithAccountingToken() {
JmsConnectionFactory cf = null;
Connection connection = null;
Session session = null;
Destination reqQ = null;
MessageProducer producer = null;
try {
// Create a connection factory
JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
cf = ff.createConnectionFactory();
// Set the properties
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_BINDINGS);
cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, "QM2");
// Create JMS objects
connection = cf.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create a 32 byte accounting toke
byte [] accountingToken = new byte[32];
byte b = 'a';
for(int i=0; i < 32;i++)
accountingToken[i] = b++;
// Create destination to send requests
reqQ = session.createQueue("queue:///REQUESTQ");
((MQDestination) reqQ).setTargetClient(WMQConstants.WMQ_CLIENT_NONJMS_MQ);
((JmsDestination) reqQ).setBooleanProperty(WMQConstants.WMQ_MQMD_READ_ENABLED, true);
((JmsDestination) reqQ).setBooleanProperty(WMQConstants.WMQ_MQMD_WRITE_ENABLED, true);
((MQDestination) reqQ).setMQMDMessageContext(WMQConstants.WMQ_MDCTX_SET_IDENTITY_CONTEXT);
// Create producer
producer = session.createProducer(reqQ);
// Create a request message
Message requestMessage = session.createTextMessage("Setting Accounting token on message");
requestMessage.setObjectProperty(JmsConstants.JMS_IBM_MQMD_ACCOUNTINGTOKEN, accountingToken);
// Send it off
producer.send(requestMessage);
}catch(Exception ex){
System.out.println(ex);
}
}
}