0

We're receiving messages on an IBM MQ (using C# and the IBM WebSphere MQ 8.0.0.7 NuGet package).

All the examples we found for receiving messages are simplistic and based on the pattern:

  • Receive message
  • If no message, throw exception
  • Carry on receiving

The principle being the different between "listening" and "receiving".

Given this is a no-frills queue, there is surely a better way to listen to a queue instead of sitting on a loop that is controlled by catching exceptions when there is no queue available in the queue I am "listening"/receiving from.

This is the principle code/pattern:

   public void Start(string receiveQueueName)
    {
        _receiveQueueName = receiveQueueName;
        _thread=new Thread(ReceiveText);
        _thread.Start();
    }

    private void ReceiveText()
    {
        try
        {
            string responseText = null;
            // First define a WebSphere MQ message buffer to receive the message
            MQMessage retrievedMessage = new MQMessage();

            // Set the get message options
            MQGetMessageOptions gmo = new MQGetMessageOptions(); //accept the defaults
            //same as MQGMO_DEFAULT

            // Set up the options on the queue we want to open
            int responseOptions = MQC.MQOO_INPUT_AS_Q_DEF;

            using (MQQueue responseQueue =
                _mqQueueManager.AccessQueue(receiveQueueName, responseOptions)) 
            {

                while (isEnabled)
                {
                    // creating a message object
                    MQMessage mqMessage = new MQMessage();

                    try
                    {
                        responseQueue.Get(mqMessage);

                        // ---- DEAL WITH THE MESSAGE ----

                        mqMessage.ClearMessage();
                    }
                    catch (MQException mqe)
                    {
                        if (mqe.ReasonCode == 2033)
                        {

                            // ----- NO MESSAGE ------

                            continue;
                        }
                        else
                        {
                            throw;
                        }
                    }
                }

                // Close the queue
                responseQueue.Close();
            }

        }
        catch (MQException mqExOuter)
        {
            // ----- DEAL WITH THE ERROR -----
        }

    }

Note that there is additional code here to support the threading, which has been removed for clarity.


I accept this is a "subjective", etc. question but please consider that I am appealing to the expertise of people, which is valid and important for subsequent readers.

Program.X
  • 7,250
  • 12
  • 49
  • 83
  • 1
    While it is not available on NuGet, you can download the MQ v8 or v9 Redistributable client for Windows (google for MQC7 or MQC9 to find the IBM page for downloads). The Redistributable client also does not require a install, just unzip it. It includes libraries for XMS.NET, this is IBM's JMS like implementation for .NET. You can setup a message listener which will call a "onMessage" method that you write to process the message. In this case in the background it registers a call back with the queue manager so is notified when new messages arrive. – JoshMc Jun 21 '18 at 17:00
  • 1
    "[Should each IMessageConsumer or IMessageProducer have its own ISession?](https://stackoverflow.com/questions/50743594/should-each-imessageconsumer-or-imessageproducer-have-its-own-isession)" has part of the code for this setup. There are also samples included in the zip file that demonstrate this. – JoshMc Jun 21 '18 at 17:03
  • 1
    "[MessageListener, will it get concurrent messages](https://stackoverflow.com/questions/31813384/messagelistener-will-it-get-concurrent-messages)" also looks promising. – JoshMc Jun 21 '18 at 17:06
  • @JoshMc Thanks (again). We can't go down to 7 because of security requirements. We _could_ go to 9 but this would require a new test cycle across international and corporate boundaries so may avoid this. Thanks for the reassurance that I'm not missing anything. It'll just have to smell in the short term. – Program.X Jun 22 '18 at 14:10
  • 1
    The MQC7 was a typo, as I mentioned just before that "you can download the MQ v8 and v9 Redistributable...." so it should have said "google for MQC8 and MQC9". The redistributable client was not available in MQ v7, it was new in v8. Redistributable just means you do not need to install anything that require administrative access, you can just unzip the files. – JoshMc Jun 22 '18 at 15:19
  • XMS.NET is available only from IBM MQ v905 redistributable package. So to use XMS without installing IBM MQ client, you will have to download IBM MQ v905 redistributable package. – subbaraoc Jun 25 '18 at 05:07
  • @subbaraoc thank you for the heads up on this, I had forgotten that this not included in Redist and had heard there were plans to include it in redist. – JoshMc Jun 26 '18 at 19:23

0 Answers0