Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get acknowledgement back from put method on IBM MQ?

Tags:

c#

.net

ibm-mq

This is the sample code i am using, however, i don't know how to get a response back from put or access queue, that the message is sent.

 mqQueue = mqQMgr.AccessQueue("Queue Name", MQC.MQOO_OUTPUT |
 MQC.MQOO_INPUT_SHARED |       MQC.MQOO_INQUIRE); 
 mqQueue.Put(mqMsg, mqPutMsgOpts);

PUT method is a void return type, if it would have been bool for example, based on the return value - true/false, i can confirm, message was sent to the queue, that is what my question, how do i get back an acknowledgement,so that message is sent to the queue.

Any inputs will be highly helpful.

like image 487
Sharpeye500 Avatar asked Dec 03 '25 18:12

Sharpeye500


2 Answers

Updating as per comments clarifying the question:

Enclose your PUT in a try block and catch the MQException. If no MQException was thrown, then the PUT worked and you got back RC=0. For example, this is from the sample PUT program:

    void PutMessages()
    {
        try
        {
            // mq properties
            properties = new Hashtable();
            properties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);
            properties.Add(MQC.HOST_NAME_PROPERTY, hostName);
            properties.Add(MQC.PORT_PROPERTY, port);
            properties.Add(MQC.CHANNEL_PROPERTY, channelName);

            // display all details
            Console.WriteLine("MQ Parameters");
            Console.WriteLine("1) queueName = " + queueName);
            Console.WriteLine("2) host = " + hostName);
            Console.WriteLine("3) port = " + port);
            Console.WriteLine("4) channel = " + channelName);
            Console.WriteLine("5) numberOfMsgs = " + numberOfMsgs);
            Console.WriteLine("");

            // create connection
            Console.Write("Connecting to queue manager.. ");
            queueManager = new MQQueueManager(queueManagerName, properties);
            Console.WriteLine("done");

            // accessing queue
            Console.Write("Accessing queue " + queueName + ".. ");
            queue = queueManager.AccessQueue(queueName, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING);
            Console.WriteLine("done");

            // creating a message object
            message = new MQMessage();
            message.WriteString(messageString);

            // putting messages continuously
            for (int i = 1; i <= numberOfMsgs; i++)
            {
                Console.Write("Message " + i + " <" + messageString + ">.. ");
                queue.Put(message);
                Console.WriteLine("put");
            }

            // closing queue
            Console.Write("Closing queue.. ");
            queue.Close();
            Console.WriteLine("done");

            // disconnecting queue manager
            Console.Write("Disconnecting queue manager.. ");
            queueManager.Disconnect();
            Console.WriteLine("done");
        }

        catch (MQException mqe)
        {
            Console.WriteLine("");
            Console.WriteLine("MQException caught: {0} - {1}", mqe.ReasonCode, mqe.Message);
            Console.WriteLine(mqe.StackTrace);
        }
    }

As noted, this is from the sample code and a real program intended for Production would probably have more granular exception handling. That said, the API call will either succeed or throw an MQException. The exact behavior depends on your options. For example, if you request message conversion on a GET it's possible to get a conversion error whereas the exact same message retrieved as a binary payload would succeed.


Previous response:

I'm not sure I understand the question, but what I think you are asking is how to design a request/reply program using WebSphere MQ and that's how I'll respond. However, if by "response" you actually meant the reason and return code from the PUT, please clarify the question and I'll update my answer.

The usual pattern for request/response is that the requesting program first opens the reply-to queue. If the name of the queue that is opened is a model queue, the queue manager creates a dynamic queue and returns a handle to that. Otherwise, the queue must be a pre-existing local queue.

Once the application has a handle on the reply-to queue, it opens the request queue and creates a new request message. The name of the local reply-to queue is used to initialize the replyToQueue field of the request message. The local queue manager name is automatically filled in for the message's replyToQMgr field. The app then puts the message and waits on the reply in the reply-to queue.

If there are several app instances listening on the same reply-to queue, they usually specify a correlation ID for the specific message they want. The usual design is that the server that reads the request message puts the MQMD.MsgID into the MQMD.CorrelID field of the reply message however sometimes it transfers the incoming message ID to the outbound message ID.

Be sure to COMMIT the PUT before issuing the GET or else you will never get a reply.

There's more on this in the Infocenter topic Design of the Request sample program and you can find the sample programs at C:\Program Files (x86)\IBM\WebSphere MQ\tools\dotnet in a default Windows WMQ client installation. I would encourage you to familiarize yourself with the samples and reuse some of the code from there. If you didn't actually download and install the full client, pick up a copy free at SupportPac MQC75. Then when you install it, be sure to select the option to install the SDK and sample code.

like image 120
T.Rob Avatar answered Dec 06 '25 09:12

T.Rob


how do i get back an acknowledgement,so that message is sent to the queue

If Put method does NOT throw an exception then THAT is your acknowledgement that MQ has put the message to the queue.

like image 33
Roger Avatar answered Dec 06 '25 08:12

Roger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!