134 Part II Message Endpoints
Using MSMQ Triggers to Respond to Messages
Most EI solutions that reside on the Windows platform will use Microsoft Mes-
sage Queuing (MSMQ) at some point. As mentioned, whenever possible, your
integration code should respond to events rather than query for changes. Having
a solution execute exactly one action in response to exactly one event is impor-
tant. Compared to polling, this method is significantly more efficient. Performing
an action on a server whenever a message arrives in a specified MSMQ queue
is often necessary. Even a task as simple as retrieving the message and deter-
mining how to use it is easily accomplished by using MSMQ triggers. In this
section, we will examine how to create and use an MSMQ trigger to execute
code on a machine.
Problem
In keeping with the best practice of using event-driven solutions instead of poll-
ing-driven ones, we want to execute a task whenever a message arrives in a spe-
cific queue without having to continually poll that queue. Ideally, we also want
to determine what code to run based on either the message header or content.
Solution
You can solve this problem by using MSMQ triggers. An MSMQ trigger is set up on
the box that manages the queue and is activated based on criteria you specify.
In the following example, it will be triggered any time a message shows up in
the specified queue. The first step is to add a rule for the trigger:
1. Locate and expand Message Queuing under Computer Management
in the Administrative Tools menu.
2. Expand Message Queuing Triggers.
3. Right-click Rules and select New. Then select Rule.
G06LS11
Chapter 6 File Endpoints 135
4. When the New Rule dialog box appears, enter a name and descrip-
tion for the rule. Then click Next.
G06LS12
5. Select the condition from the drop-down list, add the parameter, and
then click Add. Your new rule should appear in the Condition List box:
G06LS13
6. Click Next.
7. Enter the ProgID and method of the COM you want to trigger, or
select an .exe file to run.
8. Click Parameters. Select any parameters you want to add and click Add.
9. Click Finish.
136 Part II Message Endpoints
Your rule is now added. The next step is to attach your new rule to an
MSMQ trigger:
1. Right-click Triggers and select New. Then select Trigger. You should
see the following:
G06LS14
2. Type a trigger name and the queue name. Select Retrieval and click
Next. A dialog box similar to the following appears:
G06LS15
3. Select your rule in the left box and click Attach. Then click Finish.

Get Enterprise Integration Solutions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.