Microsoft has provided a wonderful Queuing framework .It is known as Microsoft message Queuing .This can used for data storage during failure at the same time send message between two processes in the same machine or 2 different machine
In case of 2 processes in a local machine we use Private Queue
In case of 2 processes in 2 remote machines we use public Queues
The message Queue Installation is available in Add remove Windows Components Message Queuing
After installing the same .you will able to create an use Message Queues
The Queues can be created by following the steps
1. Start CompMgmt.msc
2. You get MessageQueing Sub menu
3. You can Select Private and Public Queues and Select New
4. Enter the Queue Name
5. Select Transactional and Non-Transactional
6. The public queue name format is machine-name\public$\Queue-Name
7. The private queue name format is machine-name\private$\Queue-Name
I have written an article in this blog that demonstrates a QueueServer and a QueueClient.
The Queue Server pushes the message to a private Queue with name test Queue
The Queue Client Recieves the message and displays it on the console
The Queue stores the messages that is Enqueued and Dequeued in the first in first out order
Messages contain 3 important parts which I have used
Label –unique id for the message
Formatter – the way in which message is stored it may be binary,text,xml or custom .
Body – The field which stores the message .
The message can be retrieved by the client in Synchronous and Asynchronous fashion
I have used asynchronous mechanism to retrieve the messages in the message queue client side
Following is the example
Queue Server
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;
namespace MessageQueueServer
{
class Program
{
static void Main(string[] args)
{
//create a Queue if it doesnot exists
if (!MessageQueue.Exists(@"mycomputer\private$\testQueue"))
{
//create a message queue
MessageQueue.Create(@"mycomputer\private$\testQueue");
}
//instance representing the Queue
MessageQueue Queue = new MessageQueue(@"mycomputer\private$\testQueue");
int messagecount = int.MaxValue;
int messageindex = 0;
while (messageindex < messagecount)
{
//create messages
Message QueMessage = new Message();
//unique id for the message
QueMessage.Label = messageindex.ToString();
//can be any information
QueMessage.Body ="m"+ messageindex;
QueMessage.Formatter = new BinaryMessageFormatter();
//send the message to the Queue
Console.WriteLine("pushing message no " + messageindex + "to " + Queue.QueueName);
Queue.Send(QueMessage);
messageindex++;
}
Console.ReadLine();
}
}
}
The code above pushes the messages to the Queue
Queue Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;
namespace MessageQueueClient
{
class Program
{
static MessageQueue queue;
static void Main(string[] args)
{
//access the queue
queue = new MessageQueue(@"mycomputer\private$\testQueue");
queue.ReceiveCompleted += new ReceiveCompletedEventHandler(queue_ReceiveCompleted);
//start recieving the message
queue.BeginReceive();
Console.ReadLine();
}
static void queue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
//recieve the message from the Queue and close it until one message is processed
Message message = queue.EndReceive (e.AsyncResult);
Console.WriteLine("Message recieved with id:" + e.Message.Label);
//format the message to retrieve the data
e.Message.Formatter = new BinaryMessageFormatter();
Console.WriteLine("Content in the Queue:" + e.Message.Body);
Console.WriteLine("--------------------------------");
//open the queue for recieving
queue.BeginReceive();
}
}
}
The code above is a Queue Client and receives the messages from the Queue Server
Run the Queue Server and then the Queue Client and enjoy the example.
No comments:
Post a Comment