Click here to Skip to main content
15,890,512 members
Articles / Programming Languages / C#
Article

IBM Websphere MQ Process Message Immediately with C#

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
21 Sep 2008CPOL2 min read 74.2K   1.4K   18   8
IBM Websphere MQ process message immediately with C#
Sample Image - maximum width is 600 pixels

Introduction

In IBM Websphere MQ applications, usually we need to process a suitable message to arrive into the queue immediately. In this case, the Polling method is not a good solution; waiting for the message or event-driven are better solutions.

IBM Websphere MQ provides Triggering for process messages arriving on queues. Actually, MQ's Triggering is very fussy. You must create the initial queue, process, etc. It is important that you must run the 'runmqtrm' command for monitoring MQ's Triggering on the queue that you want.

I present a simple solution for this case. If you what to know when the message is arriving in queues, why don't you wait for the message.

Background

Before it is possible to use the WebSphere MQ object's properties, methods, or events, a reference must be created to the object. The technique to add a WebSphere MQ reference is by adding amqmdnet.dll as a reference. This Dynamic Link Library (DLL) is typically located at: C:\Program Files\IBM\WebSphere MQ\bin\amqmdnet.dll and using IBM.WMQ;

Using the Code

The MQMessageListener class provides the Listen method to wait for a suitable message that arrives on the queue manager qmName and queue queueName.

In the MQMessageListener class, MQQueueManager set open options are MQOO_INPUT_AS_Q_DEF and MQOO_FAIL_IF_QUIESCING for connecting to Websphere MQ for getting a message using queue-define and fails if that queue is in quiescing mode. Then MQGetMessageOptions set are MQGMO_FAIL_IF_QUIESCING and MQGMO_WAIT for MQGMO_WAIT. This option is used with the WaitInterval method when you set WaitInterval is MQWI_UNLIMITED application will wait for the message to arrive in the queue at queue.Get(message, gmo). When the message is arriving on the queue, the application will run to return the message.

C#
public class MQMessageListener
    {
        public static MQMessage Listen(string qmName, string queueName)
        {
            /** MQOO_INPUT_AS_Q_DEF -- open queue to get message 
	    *  using queue-define default.
             *  MQOO_FAIL_IF_QUIESCING -- access fail if queue manager is quiescing. **/
            int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING;
            MQQueueManager mqManager = new MQQueueManager(qmName);
            MQQueue queue = mqManager.AccessQueue(queueName,openOptions);

            /** MQGMO_FAIL_IF_QUIESCING -- 
	    *  get message fail if queue manager is quiescing.
             *  MQGMO_WAIT -- waits for suitable message to arrive.
             *  MQWI_UNLIMITED -- unlimited wait interval. **/
            MQGetMessageOptions gmo = new MQGetMessageOptions();
            gmo.Options = MQC.MQGMO_FAIL_IF_QUIESCING | MQC.MQGMO_WAIT;
            gmo.WaitInterval = MQC.MQWI_UNLIMITED;
            MQMessage message = new MQMessage();
            //wait for message
            queue.Get(message, gmo);
            queue.Close();

            //release resource.
            mqManager = null;
            queue = null;
            gmo = null;
            System.GC.Collect();

            return message;
        }
    }

The Program class is the default class when you create a project with Visual Studio. This class contains the Main method and the application starts here.

C#
class Program
   {
       static void Main(string[] args)
       {
           Console.WriteLine("==START==");
           String message = null;
           do{
               Console.WriteLine("Waiting...");
               MQMessage mqMessage = MQMessageListener.Listen
               ("MANAGER.QUEUE", "LOCAL.QUEUE");
               message = mqMessage.ReadString(mqMessage.MessageLength);
               Console.WriteLine("\tMessage: " + message);
           }while(message != "BYE");
           Console.WriteLine("==END==");
       }
   }

References

History

  • 21st September, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Thailand Thailand
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerMore Information Pin
Velma W. Johnson4-Jun-14 2:06
Velma W. Johnson4-Jun-14 2:06 
GeneralVery Good Example Fuangwith Pin
sidiq9-Mar-11 22:29
sidiq9-Mar-11 22:29 
GeneralBadly designed MQ code sample Pin
RogerLacroix22-Sep-08 9:20
RogerLacroix22-Sep-08 9:20 
GeneralRe: Badly designed MQ code sample Pin
Mr.Fuangwith S.23-Sep-08 2:02
Mr.Fuangwith S.23-Sep-08 2:02 
GeneralRe: Badly designed MQ code sample Pin
Mario_F27-Sep-08 11:54
Mario_F27-Sep-08 11:54 
GeneralRe: Badly designed MQ code sample Pin
RogerLacroix28-Sep-08 6:37
RogerLacroix28-Sep-08 6:37 
GeneralRe: Badly designed MQ code sample Pin
Mario_F28-Sep-08 10:11
Mario_F28-Sep-08 10:11 
QuestionRe: Badly designed MQ code sample Pin
Sergio_R_S_Pinheiro1-Apr-19 8:54
professionalSergio_R_S_Pinheiro1-Apr-19 8:54 
Hello Roger !

I know that this article is a bit old, but I tryed to implement you code, and I had some dificulties>

First, I had to make some modification on it in order to compile.

/** 
 * Author: Mr.Fuangwith S.
 * Date: 21/Sep/2008
 * Todo: Application start at here.
 **/

using System;
using System.Collections.Generic;
using System.Text;

using IBM.WMQ;
using System.Collections;

namespace MQWaitMessageDemo
{
    
    class Program
{
   static void Main(string[] args)
   {
      Console.WriteLine("==START==");
      String message = null;
      try
      {

         Hashtable props = new Hashtable();                          
         props.Add(MQC.HOST_NAME_PROPERTY, "127.0.0.1");       
         props.Add(MQC.PORT_PROPERTY, "1414");           
         props.Add(MQC.CHANNEL_PROPERTY, "SYSTEM.DEF.SVRCONN");      
         
         MQQueueManager qmgr = new MQQueueManager("QMANAGER", props);  // ===> I had to put it outside the "Try" block
         MQQueue queue = openQueue(qmgr, "TEST.LOCAL.RESPONSE");

         try
         {
             

            do{
                Console.WriteLine("Waiting...");
                MQMessage mqMessage = MQMessageListener.Listen(qmgr, queue);
                message = mqMessage.ReadString(mqMessage.MessageLength);
                Console.WriteLine("\tMessage: " + message);
            }while(message != "BYE");
            Console.WriteLine("==END==");

            queue.Close();
            queue = null;
         }
         catch (MQException mqex)
         {
            System.Console.Out.WriteLine("MQTest01 cc=" + mqex.CompletionCode + " : rc=" + mqex.ReasonCode);
         }
         finally
         {
            if (queue != null)
               queue.Close();
            qmgr.Disconnect();
         }
      }
      catch (MQException mqex)
      {
         System.Console.Out.WriteLine("MQTest01 cc=" + mqex.CompletionCode + " : rc=" + mqex.ReasonCode);
      }
   }

   public static MQQueue openQueue(MQQueueManager qmgr, string queueName)
   {
      /** MQOO_INPUT_AS_Q_DEF -- open queue to get message using queue-define default.
       *  MQOO_FAIL_IF_QUIESCING -- access fail if queue manange is quiescing. **/
      int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING;

      return qmgr.AccessQueue(queueName,openOptions);
   }
}
     
}



/** Author: Mr.Fuangwith S.
 *  Date: 21/Sep/2008
 *  Todo: This class waits for suitable message to arrive.
 **/

//add reference from C:\Program Files\IBM\WebSphere MQ\bin\amqmdnet.dll
using IBM.WMQ;

namespace MQWaitMessageDemo
{
    public class MQMessageListener
    {
        public static MQMessage Listen(MQQueueManager qmgr, MQQueue queue)  // ===> I had to declare it as Static
        {
            /** MQGMO_FAIL_IF_QUIESCING -- get message fail if queue manager is quiescing.
             *  MQGMO_WAIT -- waits for suitable message to arrive.
             *  MQWI_UNLIMITED -- unlimited wait interval. **/
            MQGetMessageOptions gmo = new MQGetMessageOptions();
            gmo.Options = MQC.MQGMO_FAIL_IF_QUIESCING | MQC.MQGMO_WAIT;
            gmo.WaitInterval = MQC.MQWI_UNLIMITED;
            MQMessage message = new MQMessage();
            //wait for message
            queue.Get(message, gmo);

            return message;
        }
    }
}


The code works, but, after some time (around 30 minutes), the Listener stops to listen the queue en does not proccess any new message.

Have you experienced this ?

Thank you very Much !

Regards,

Sérgio R. S. Pinheiro

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.