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.
public class MQMessageListener
{
public static MQMessage Listen(string qmName, string queueName)
{
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING;
MQQueueManager mqManager = new MQQueueManager(qmName);
MQQueue queue = mqManager.AccessQueue(queueName,openOptions);
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.Options = MQC.MQGMO_FAIL_IF_QUIESCING | MQC.MQGMO_WAIT;
gmo.WaitInterval = MQC.MQWI_UNLIMITED;
MQMessage message = new MQMessage();
queue.Get(message, gmo);
queue.Close();
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.
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