简体   繁体   中英

Awating response from IBM WebSphere MQ using .NET

In order to integrate with a data provider, I have to send and receive requests across an IBM MQ channel. I will send a request over and receive a request back from the provider. In this process though, I may be sending hundreds of requests per second and need to be able to "match" the responses to the corresponding requesting threads.

I don't want to send the request and then just continue to Get from the queue until the response has arrived because that could be a lot of traffic if potentially hundreds of threads are pinging the queue checking for their response, and I also don't want to wait a period of time and then check because I want the results to be real time.

Is there a way to subscribe not just to a queue, but to a specific response id or something coming back on the queue? This seems like something that should be doable, but I can't find it in their documentation.

You do know that you can do an 'MQGET with wait'?

(1) A default MQGET will get the next message off the queue or return with RC 2033 (No message available).

(2) The program can do an 'MQGET with wait' where you specify 'x' milliseconds to wait before returning RC of 2033 if no message was available. Note: If the message IS available then the MQGET returns as soon as the message is available - it does not wait the entire wait internal before returning.

MQMessage msg = new MQMessage();
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.Options |= MQC.MQGMO_WAIT | MQC.MQGMO_FAIL_IF_QUIESCING;
gmo.WaitInterval = 60000;  // wait 60 seconds

try
{
    inQ.Get(msg, gmo);
    System.Console.Out.WriteLine("Message Data: " + msg.ReadString(msg.MessageLength));
}
catch (MQException mqex)
{
    System.Console.Out.WriteLine("MQTest62B CC=" + mqex.CompletionCode + " : RC=" + mqex.ReasonCode);
    if (mqex.Reason == MQC.MQRC_NO_MSG_AVAILABLE)
    {
        // no meesage - life is good - loop again
    }
}

Consider using the correlation ID. The typical pattern for this is...

1) Application puts request message, MQ generates a message ID which is returned to the application

2) Back-end application processes request message and generates a response message. The correlation ID of the response message is set to the message ID of the request message.

3) Application uses GMO option MQMO_MATCH_CORREL_ID and specifies the correlation ID it wants to receive a message for. It receives the response message put in #2.

This way you can have lots of concurrent requests/responses going on and match the requests with the relevant responses.

Obviously you need the backend application to set the correlation ID to the message ID or something else predictable for this to work. What does your provider provide?

Take a look at the Request sample programs: https://www.ibm.com/support/knowledgecenter/SSFKSJ_9.0.0/com.ibm.mq.dev.doc/q024350_.htm

They're written in C for the MQI rather than .NET, but the same principles apply.

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