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.
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.
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.