我需要通过传递消息ID来实现从IBM MQ中读取消息的代码,我已经实现的程序一次只能读取一条消息,但是我的代码没有覆盖消息ID
public final void ReadMessage (String queueName) throws Exception {
int options = MQC.MQOOINQUIRE + MQC.MQOOFAILIFQUIESCING + MQC.MQOOINPUTSHARED;
System.out.printin ("start Creating the Queue....... )
MQQueue myQueue = this.mqManager.accessQueue(queueName, options) ;
MQMessage mgMessage = new MQMessage ( ) ;
MQGetMessageOptions gmo = new MQGetMessageOptions ( ) ;
gmo.options = MQC.MQGMO NO WAIT + MQC.MQGMO FAIL IF QUIESCING;
gmo.matchOptions = MQC.MQMO NONE;
gmo.waitlnterval = 15000;
try {
System.out.println("end of get Message from myqueue") ;
System.out.print In ("Message lenth" + mgMessage ( ) ) ;
mgMessage.characterSet = 300;
int length = mqMessage.getMessageLength( );
System. out ( of the message" + length) ;
System. out ( of the message" + mgMessage.readString(length)) ;
gmo.options = MQC.MQGMOWAIT | MQC.MQGMOBROWSENEXT;
}
catch (Exception e) {
}
}
此代码能够从队列中读取1条消息。但是我需要传递消息ID,并且基于消息ID,我需要阅读消息。
这个要求可行吗?如果是这样,请与我分享一些IBM MQ Client的示例。
想知道如何在代码中传递消息ID。
MQQueue myQueue = this.mqManager.accessQueue(queueName, options, MessageID) ;
谢谢
答案 0 :(得分:0)
您可以在执行MQGET操作时使用messageId。
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.matchOptions = MQC.MQMO_MATCH_MSG_ID;
mgMessage.messageId=messageId;
以下页面还讨论了如何基于MessageId或CorrelId或groupId获取消息 https://www.ibm.com/support/knowledgecenter/SSFKSJ_8.0.0/com.ibm.mq.ref.dev.doc/q097550_.htm
答案 1 :(得分:0)
用于请求/答复场景的IBM MQ标准用于请求应用程序:
示例:
请求应用程序的第一步(输入请求消息):
MQPutMessageOptions pmo = new MQPutMessageOptions();
MQMessage requestMsg = new MQMessage();
requestMsg.messageId = CMQC.MQMI_NONE;
requestMsg.correlationId = CMQC.MQCI_NONE;
requestMsg.format = CMQC.MQFMT_STRING;
requestMsg.messageType = CMQC.MQMT_REQUEST;
requestMsg.replyToQueueManagerName = qMgrName;
requestMsg.replyToQueueName = replyQName;
requestMsg.writeString("This is a test message");
outQ.put(requestMsg, pmo);
byte[] savedMsgId = requestMsg.messageId;
请求申请(获取回复消息)的步骤2:
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.options = CMQC.MQGMO_FAIL_IF_QUIESCING;
gmo.matchOptions = CMQC.MQMO_MATCH_CORREL_ID;
MQMessage replyMsg = new MQMessage();
replyMsg.messageId = CMQC.MQMI_NONE;
// Specifically get the message with the matching value.
replyMsg.correlationId = savedMsgId;
inQ.get(replyMsg, gmo);