Message Queuing


Message queuing provides a loosely coupled application asynchronous integration model that fits into several application architectures. With the introduction of Microsoft Message Queue (MSMQ), messaging has been a key component of applications developed using the Microsoft platform. The .NET Framework extends this capability through the System.Messaging namespace, which contains a set of classes to connect, monitor, and administer message queues and send, receive, and peek messages. The two main classes in the namespace are System.Messaging.MessageQueue and System.Messaging.Message. Whereas the former is the main class for manipulating message queues and sending and receiving messages, the latter provides a placeholder for the messages that are sent and received with the system. Coupled with the Message class are a set of three formatters that serialize and deserialize messages ”XmlMessageFormatter (XML-based loosely coupled formatter, which also is the default formatter), ActiveXMessageFormatter (MSMQ COM Control style formatting), and BinaryMessageFormatter (for faster processing).

Sending Messages

To start working with message queuing, you should first declare the class that you intend to use as the body of the message.

 
 using System; namespace hks {    public class Order    {       public String Customer;       public String[] Items;    } } 

Next, you need to compile this into a shared library (dll) so that you link it with both the sending and receiving programs.

 
 csc /target:library Order.cs 

Next you define the program that sends orders to a remote/local message queue for processing (Figure 4.3). The application (Listing 4.44) is designed in an architecture where the sending application simply sends the message and exists without waiting for the back-end processing (which typically could take a while) to complete.

Listing 4.44 Using Message Queueing
 using System; using System.IO; using System.Messaging; using System.Xml; using System.Xml.Serialization; namespace hks {    public class SendOrder    {       public static void Main()       {              try {          Order o = new Order();          o.Customer = "ABC Chemicals";          o.Items = new String[] {"Item1","Item2","Item3"};          MessageQueue q = new MessageQueue(".\Private$\OrdersQueue");          Message m = new Message(o);          q.Send(m);              } catch (Exception ex) {          Console.WriteLine(ex.Message);         }       }    } } 
Figure 4.3. Setting up message queuing.

Receiving Messages

To complete the loop, you need to define a process that will continuously receive orders (Listing 4.45) and invoke the appropriate back-end integration APIs to process them.

Listing 4.45 Receiving Messages
[View full width]
 using System; using System.Messaging; namespace hks {    public class ProcessOrders    {       public static void Main()       {         try {          MessageQueue q = new MessageQueue(".\Private$\OrdersQueue");          q.Formatter = new XmlMessageFormatter(new Type[] {typeof(hks.Order)});          while (true)          {             Order o = (Order) q.Receive().Body;             OrderProcessing.ProcessOrder(o);          }          } catch (Exception e) {          Console.WriteLine(e.Message);          }       }    }    public class OrderProcessing    {       public static void ProcessOrder(Order o)       {          Console.WriteLine("Processing Order from "+o.Customer+", for " +o.Items.Length+"  items");       }    } } 

If you compile the sender and receiver application and send orders from the sending side, you should see an output similar to the following on the receiving application:

 
 E:\hks\DotNet\Chapter 4>ProcessOrders Processing Order from ABC Chemicals, for 3 items ... 


Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

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