简体   繁体   中英

How to read IBM MQ Message based on Message ID in via Java code (IBM MQ Client)

I need to implement the code to read the message from IBM MQ by passing message id, the program i have implement will read one message at a time, but my code didn't cover the message 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) { 
}
}

This code able to read 1 message from the queue. but I need to the pass message id and based on the message id I need to read the message.

Is this requirement is possible ? If so please share me some sample for IBM MQ Client.

would like to know how to pass message id in code.

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

Thanks

You can use messageId while doing a MQGET operation.Something like

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

Following Page also talks about it on how to get the message based on the MessageId or CorrelId or groupId https://www.ibm.com/support/knowledgecenter/SSFKSJ_8.0.0/com.ibm.mq.ref.dev.doc/q097550_.htm

The IBM MQ standard for request/reply scenario is for the request application to:

  • save the Message Id after the MQPUT to the server application
  • the server application save the incoming message's Message Id. the server
  • the server application creates the reply message, it will store the saved Message Id in the out going message's Correlation Id field
  • the requesting application will issue an MQGET using the saved Message Id in the Corelation Id field

Example:

Step #1 for requesting application (putting request message):

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;

Step #2 for requesting application (getting reply message):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM