Message Queuing Triggers

Team Fly 

Page 382

Message Queuing Triggers

One might expect that the MessageQueue class would support events. If a MessageQueue object could fire an event every time a new message arrives, we'd be able to write simple code to process messages as soon as they arrive and we wouldn't have to use asynchronous techniques to read messages from a queue. MSMQ doesn't support events, but you can use the Message Queuing Triggering service to process messages as soon as they arrive at a specific queue. This service comes as a component of Windows XP. If you're using Windows 2000, you can download it from the following URL:

http://www.microsoft.com/windows2000/technologies/communications/msmq/default.asp

The setup dialog boxes for the version of Message Queuing Triggering for Windows 2000 are a little different than the ones for the XP version, and in this section we'll describe the XP version of the component. You shouldn't have any problem applying your knowledge to Windows 2000, but bear in mind that the various dialog boxes you'll see while setting up triggers are different than the ones shown in the figures of this section.

Setting up a trigger for a specific queue is straightforward and you need not write any code. Actually, you can't set up triggers from within your code, but you should expect a new class in the next version of the .NET Framework that exposes the functionality of the Message Queuing Triggering component. The process of setting up a trigger for a queue involves two items:

Rules A rule determines the conditions under which the trigger will be fired. By default, triggers are fired every time a new message arrives at the queue to which the trigger is attached, but you can request that triggers are fired only when certain conditions are met. For example, you can request that a trigger is fired only if the message's label contains (or doesn't contain) a string, the message's priority is greater (or smaller) than a specific value, and so on.

Actions Actions are programs that process the message that caused the trigger to be fired. There are two types of actions, or equivalently two ways to process a message. You can either start an executable (an EXE file) or a call a method of a COM object (a DLL file). The program is started automatically by the trigger every time the conditions specified by the corresponding rule are met. It's also possible to pass one or more arguments to the program that services the trigger, such as the message's ID or label, the date and time it was sent or has arrived at the queue, and so on.

Once you've created a set of rules and their corresponding actions, you can create triggers. Each trigger is unique to a specific queue, but it can deploy any of the existing rules. New triggers are created by combining one or more rules and assigning the trigger to a specific queue. The same rules can be reused in as many triggers as you need.

Defining Rules

The first step in creating a trigger is to define the rule(s) that will be used by the trigger. Expand the Message Queuing Triggers item in the MSMQ snap-in, right-click the Rules item and select New image Rule. You'll be prompted to enter a name and a description for the rule, shown in Figure 14.8. Let's call our trigger NWOrderRule. We'll create a trigger to signal the arrival of a new message at the NWOrders queue.

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net