Click here to Skip to main content
15,885,435 members
Articles / Desktop Programming / MFC
Article

Working with MSMQ Triggers

Rate me:
Please Sign up or sign in to vote.
4.85/5 (18 votes)
25 Mar 20036 min read 141.6K   58   15
This article gives a brief introduction to MSMQ Triggers and how it can be used in your messaging applications.

Introduction

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:

Trigger Components

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:

Creating a trigger

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.

Create Trigger Screen

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:

New Rule Screen

b) In the rule definition screen under the general tab, enter the rule name and an optional description.

New Rulw

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:

Rule Action

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.

Add Rules

b) Now in the window that appears, select all the rules that you need to associate with this trigger. This is illustrated below:

Select Rules

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.

Limitations

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.

Conclusion

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
I am a software developer and have worked on Microsoft technologies for about five years now. I have always been fascinated by Microsoft technologies and with the advent of .NET , this fascination has reached new heights. I take a lot of interest reading technical articles and equally enjoy writing them. I really like to be called a .NET junkie and will surely try to live up to this name Smile | :)

I am .NET MVP and have also completed MCAD, MCSD(VS 6), MCDBA (SQL Server 2000), MCSA (Win 2K) and MCTS (Distributed Apps) certfications.

Comments and Discussions

 
GeneralMy vote of 5 Pin
geminigeniuss24-Jan-13 20:23
geminigeniuss24-Jan-13 20:23 
GeneralMy vote of 5 Pin
Shank_Krish2-Nov-12 1:34
Shank_Krish2-Nov-12 1:34 
GeneralProblem with MSMQ Trigger firing Pin
Tejas Patel29-Feb-08 19:26
Tejas Patel29-Feb-08 19:26 
QuestionActive directory and MSMQ [modified] Pin
RicoQQ1-Oct-07 22:40
RicoQQ1-Oct-07 22:40 
GeneralMSMQ doesn't work in Win 2000 Pin
p_tourinho17-Aug-06 1:46
p_tourinho17-Aug-06 1:46 
GeneralCOM component Pin
cheelam_mze20-Apr-05 18:03
cheelam_mze20-Apr-05 18:03 
GeneralKeep it coming Pin
funtoosh badshah11-Mar-05 9:18
funtoosh badshah11-Mar-05 9:18 
Generalcall a .net component Pin
ossama9-May-04 13:27
ossama9-May-04 13:27 
AnswerRe: call a .net component Pin
Haipro3-Apr-09 19:31
Haipro3-Apr-09 19:31 
QuestionIs it possible to create MSMQ triggers Programmatically? Pin
Meghana Kulkarni3-Nov-03 21:59
Meghana Kulkarni3-Nov-03 21:59 
AnswerRe: Is it possible to create MSMQ triggers Programmatically? Pin
Manoj G5-Nov-03 16:44
Manoj G5-Nov-03 16:44 
GeneralRe: Is it possible to create MSMQ triggers Programmatically? Pin
Dragos Haiduc28-Aug-05 22:42
Dragos Haiduc28-Aug-05 22:42 
GeneralRe: Is it possible to create MSMQ triggers Programmatically? Pin
Dragos Haiduc29-Aug-05 2:22
Dragos Haiduc29-Aug-05 2:22 
QuestionQeued Component over HTTP? Pin
Adrian Bacaianu4-Aug-03 5:11
Adrian Bacaianu4-Aug-03 5:11 
GeneralInvalid links Pin
Manoj G27-Mar-03 4:36
Manoj G27-Mar-03 4:36 

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.