Search code examples
ibm-mqxms

Websphere MQ using XMS.Net


I wanted to understand how can I use Web sphere MQ for the following scenario:

1.How I can read the message from the queue without removing that message from the queue. 2. We have a web application so we need the Listener to read the Queue. Is there any tool to do this ?


Solution

  • Yes, it's possible to read message without removing from a queue, it's known as Browsing. You will need to create a browser consumer to read the messages. I have posted snippet here, same code is available in Tools\dotnet\samples\cs\xms\simple\wmq\SimpleQueueBrowser\SimpleQueueBrowser.cs also.

      // Create connection.
      IConnection connectionWMQ = cf.CreateConnection();
      // Create session
      ISession sessionWMQ = connectionWMQ.CreateSession(false, AcknowledgeMode.AutoAcknowledge);
      // Create destination
      IDestination destination = sessionWMQ.CreateQueue(queueName);
      // Create consumer
      IQueueBrowser queueBrowser = sessionWMQ.CreateBrowser(destination);
      // Create message listener and assign it to consumer
      MessageListener messageListener = new MessageListener(OnMessageCallback);
      queueBrowser.MessageListener = messageListener;
      // Start the connection to receive messages.
      connectionWMQ.Start();
    

    Callback method

        static void OnMessageCallback(IMessage message)
        {
            try
            {
                // Display received message
                Console.Write(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught in OnMessageCallback: {0}", ex);
            }
        }