【问题标题】:How to read IBM MQ Message based on Message ID in via Java code (IBM MQ Client)如何通过 Java 代码读取基于消息 ID 的 IBM MQ 消息(IBM MQ 客户端)
【发布时间】:2019-03-30 00:41:49
【问题描述】:

我需要实现代码通过传递消息 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 客户端的示例。

想知道如何在代码中传递消息 ID。

MQQueue myQueue = this.mqManager.accessQueue(queueName, options, MessageID) ;

谢谢

【问题讨论】:

    标签: java ibm-mq


    【解决方案1】:

    您可以在执行 MQGET 操作时使用 messageId。类似

    MQGetMessageOptions gmo = new MQGetMessageOptions(); 
    gmo.matchOptions = MQC.MQMO_MATCH_MSG_ID;
    mgMessage.messageId=messageId;
    

    Following Page 也谈到了如何根据 MessageId 或 CorrelId 或 groupId 获取消息 https://www.ibm.com/support/knowledgecenter/SSFKSJ_8.0.0/com.ibm.mq.ref.dev.doc/q097550_.htm

    【讨论】:

      【解决方案2】:

      请求/回复场景的 IBM MQ 标准适用于请求应用程序:

      • 将 MQPUT 后的 Message Id 保存到服务器应用程序
      • 服务器应用程序保存传入消息的消息 ID。服务器
      • 服务器应用程序创建回复消息,它将保存的消息 ID 存储在外发消息的相关 ID 字段中
      • 发出请求的应用程序将使用关联 ID 字段中保存的消息 ID 发出 MQGET

      例子:

      请求应用程序的步骤#1(放置请求消息):

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

      【讨论】: