Creating an XML-Based Message Broker in .NET
XML is the lingua franca of the Internet. Systems today talk with each other using a variety of standards that are based on XML, with such diverse goals as making data available for applications, temporary data repositories, and even B2B transactions. Click here to download code.
This article describes how to create an XML-based message broker application by using Microsoft's .NET platform. I'll demonstrate how to use the XML classes provided in the .NET framework, and show you how to create your own broker application.
The reader should be familiar with programming in C# and accustomed to working with basic XML technologies such as DOM, SAX, XPath, and so on. Because the solution in this example uses C#, you'll need the Microsoft .NET Framework installed on your system.
Data Flow for the Application
A message broker is like a server whose task is to receive a message and invoke some other process based on defined parameters in the message. It receives the file or stream, parses the headers, extracts the command, and invokes some method to be performed respective to the command type. After the specified process is finished, the message broker notifies the client about the current state of the completed task.
The application is functionally divided into two sections:
Listing 1 shows the XML file for this example. The XML file and the initial idea are adapted from an article by Dirk Reinshagen at JavaWorld.
Listing 1[em]The XML File for the Message Broker
<?xml version="1.0"?> <message> <header> <to>CompanyReceiver</to> <toemail>firstname.lastname@example.org</toemail> <from>CompanySender</from> <fromemail>email@example.com</fromemail> <type>mailinvoice</type> </header> <body> <saveinvoice> <invoice date="01-20-2002" number="123"> <address country="PK"> <name>Nauman Laghari</name> <street>145-E</street> <city>Karachi</city> </address> </invoice> </saveinvoice> </body> </message>
This XML file shows an order that includes information about the user and the invoice. Figure 1 shows the data flow for the application.
Figure 1 Application data flow.