In a world of enterprise applications built on Microsoft technology, MSMQ has been the solution for effective messaging. MSMQ has been one of the most powerful methods for distributed applications to talk with each other in a connectionless and asynchronous manner. But using MSMQ may require a lot of infrastructure code to be written around it. For instance, you may have a sending application sending messages to a queue and a listening application constantly listening on the queue to pick them up as and when they arrive. The listening application must be up and running, constantly waiting for messages to appear on the queue. This may turn out to be an overhead for the listening application.
MSMQ Triggers is a service that allows you to associate incoming messages in a queue with functionality in a COM component or standalone executable. The advantage of triggers is that application developers no longer need to write any infrastructure code to provide such message-handling functionality, so the problem of writing constantly listening applications would be solved.
In this article I shall discus how MSMQ triggers can be created and used in an application. This article assumes that the reader is familiar with concepts of MSMQ.
Understanding MSMQ Triggers
Triggers are associated with specific queues on a computer and are invoked every time a Message Queuing message arrives at such queues. A trigger is an automatic action taken in response to a message event. In the context of MSMQ Triggers, an event is defined as one or more conditions being satisfied with respect to a message arriving at a queue. The collection of all triggers on a particular computer is called the trigger set.
The firing of a trigger can be based on conditions. A condition is a test that is applied when a message arrives at a monitored queue. A condition is always paired with an action. An action is an executable behaviour that is taken when a condition is true. An action can be expressed as an executable and argument list; or, a COM component method, and argument list.
Each trigger can be associated with one or more rules. A rule describes an action that will be taken when a specific condition is true. The collection of all rules (for all triggers) on a particular computer is called the rule set. The relationship between Queues, triggers and rules are illustrated below:
Okay, so what happens when a message arrives on a queue for which one or more triggers are configured? All the triggers would fire simultaneously. That is, the conditions are evaluated in a sequence and when they return true the corresponding action is executed. That is, a component call happens or an executable is invoked.
So, to sum things up here; we can have multiple triggers for each queue. Each trigger can have multiple rules associated to them and hence there can be multiple actions firing when a message arrives at the queue.
Installing MSMQ Triggers
MSMQ Triggers was introduced with MSMQ 3.0. MSMQ Triggers can run on Windows 2000 with Message Queuing installed and Windows NT 4.0 with Service Pack 4 and with MSMQ 1.0 installed (Intel-based processors only). MSMQ 3.0 is bundled with Microsoft XP and hence MSMQ Triggers need not be separately installed there. MSMQ Triggers can be installed through a setup file which can be downloaded from the Microsoft site. MSMQ Triggers cannot be installed on Windows 95, on Windows 98, on dependent client computers, or on server cluster nodes. MSMQ Triggers are free for download. Click here to download MSMQ Triggers
Creating and Configuring MSMQ Triggers
Having talked about concepts of MSMQ triggers, let's get practical. I will now show how we can create and use triggers in steps.
1. Create a Trigger
a) Select MSMQ Triggers from the Message Queuing section of the Computer Management Console
b) Right Click on the Trigger Definitions icon and select New trigger from the context menu that appears. This is illustrated below:
c) In the trigger definition screen that appears (shown below) , enter any name for the trigger and select the queue to which the trigger is associated. You can use the queue format name for the same. You can optionally use system queues. Select Serialize check box if you want your trigger to fire for messages in same order as they appeared on the queue.
2. Creating a Rule
a) Our next task is to create rules for the triggers. Right click on the Rule Definitions icon and select New rule from the context menu that appears. This is illustrated below:
b) In the rule definition screen under the general tab, enter the rule name and an optional description.
c) Enter the conditions if any in the condition tab. The conditions can only pertain to the properties of the message like contents of the label, message body etc.
d) Under the Action part, enter the action details. If the action is to call a COM component, the enter the PROG-ID of the component and the method name. Also enter the parameters that need to be passed to the method. If the action is an executable enter the full path of the executable and add parameters if required. This is illustrated below:
Note that in the above sample, I have entered the action as a COM Component with a PROGID of MSMQ.TriggerHandler. When the trigger fires and the condition is true, the Execute method of this component is invoked and the Message Label and Message Body are passed as parameters.
3. Associating a rule to a trigger
a) Having created the triggers and rule definitions, we have to associate them. Under trigger definitions, select a trigger to which a rule is to be added and select Add/Remove rules that appears in the context menu. You get a screen as shown below.
b) Now in the window that appears, select all the rules that you need to associate with this trigger. This is illustrated below:
Note that I have selected three out of the four rules for my trigger. So, we are done. We can test the functionality by pumping messages to the queue and check to see the actions firing against these messages, in our case, a COM component.
In the previous section, we created triggers using the administration console. They can also be created programmatically. Installing MSMQ Triggers installs MSMQTriggers Object library. This gives us a set of COM Components which we can use to create and manage triggers programmatically.
Developers have to take care of certain aspects while using MSMQ Triggers:
- When a trigger action invokes a COM component, the component runs in the process space of MSMQ Triggers (unless the component runs in a COM+ Server application). So, the developer must do proper exception handling in the component, else you might end up crashing the Triggers Service.
- MSMQ Triggers only peek at queue messages. So, you have to take care of deleting messages from the queue once the processing is over. If messages still continue to exist in the queue, then these would be picked up and processed again when the service is restarted.
MSMQ Triggers value add to the development of messaging applications. MSMQ Triggers are easy to create and manage and they solve many of the problems faced while developing against MSMQ.
For more details about MSMQ Triggers, download the documentation available as a zip file at: http://www.microsoft.com/ntserver/zipdocs/msmqtriggersdoc.zip