Thursday, September 9, 2010

Implement MSMQ in Dot Net

In Dot net, MSMQ provides a powerful set of services. These you can get from
System.Messaging name space. In order to use MSMQ explorer our Dot Net IDE can also be used to add and remove messages form MSMQ.

How you can use MSMQ:
In order to use MSMQ first you have to install MSMQ in your machine.
Since we are looking at MSMQ in Dot Net, you need to have Windows 2000 + Service Pack1, MSMQ, .NET Framework Beta1 and Visual Studio.NET .

How to Use MSMQ:
MSMQ Enterprise can be managed through the MSMQ Explorer.
The MSMQ Explorer provides an interface for managing all machines in a MSMQ environment from a single point of control. This MSMQ Explorer you can get from Computer Management/Services and Applications/MessageQueing.

Here I am trying to show a simple Example in VS.Net(VB).
In this example I am using a Windows Application in the C# Environment.This example will help you to know how to send and receive messages using MSMQ.
In this example I am basically using System.Messaging name space to manage the
messages using msmq.

Following example will help you to know how to send and receive messages using MSMQ.

In this I have created a Windows application in VB using VS.net.

Take a form and put two buttons, one for sending data and one for receiving data. I also used a text box.While sending message, i am taking message form the textbox.Button one is used to send message.Other button will be used to receive the data.

To use the MSMQ name space System.Messaging, go to Tools and Component Tab, then Drag and Drop the MessageQueue to the Form (Design View).Once you add it, as shown below, code will be added to your application


Private WithEvents MessageQueue1 As System.Messaging.MessageQueue

The above code will be added in

public class Form1 : System.WinForms.Form

and one more important line

Me.MessageQueue1 = New System.Messaging.MessageQueue()
will be added in Private Sub InitializeComponent()

While your application initializes it will create the messageQueue1 .
Once your object has been Created or Instantiated you can use it's methods and
Properties.

In our example we are trying to Send a message to "Test" Private Queue.
"Test" is a Queue name which we are using in the example. It's not a system Queue.

The code below is used for sending the message to a queue when you click Button1
button.
****************************
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim q As MessageQueue
If MessageQueue.Exists(".Private$Test") Then
q = New MessageQueue(".Private$Test")
Else
q = MessageQueue.Create(".Private$Test")
End If

Dim BW As New BinaryWriter(New MemoryStream())
bw.Write(textbox1.Text)
Dim m As New Messaging.Message()
m.BodyStream = BW.BaseStream
m.Label = "My Test Message"

q.Send(m)
msgbox("Message sent successfully",Microsoft.VisualBasic.MsgBoxStyle.Information, "MSMQ Message")

End Sub
******************************

When you click the command one, first i am checking for Queue if queue is not
there then i am creating the Queue , if queue is there i am sending the message to the
queueusingn Send method.
Send Can basically will have two arguments a) Message b) Label for that message.

Button Two will help you to receive the messages:
******************************
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim q As MessageQueue
Dim m As Messaging.Message
Dim Br As BinaryReader
Dim x As String

If MessageQueue.Exists(".Private$Test") Then
q = New MessageQueue(".Private$Test")
Else
q = MessageQueue.Create(".Private$Test")
End If


Try
m = q.Receive(New TimeSpan(0, 0, 3))
Br = New BinaryReader(m.BodyStream)
x = New String(Br.ReadChars(CInt(m.BodyStream.Length)))
Catch
x = "(no message)"

End Try
msgbox(x, Microsoft.VisualBasic.MsgBoxStyle.Information, "MSMQ Message")

End Sub
********************************
If you look into the code, basically I am testing for the existence of the Smile
Private Queue.If it is not there, I am popping up a message box. If it is there, then I am instantiating a PostMessage as a Message Queue.

Here I have to explain one thing. The message object holds the result of the
received message. So here I am using m as the message object, and we also created a TimeSpan object from the System name space. The TimeSpan object will tell how long to wait for the Receive method before timing out (hours, minutes, seconds).
Before .Net, we could simply set a message object Body property to a string value.
But in Dot Net the data will be automatically wrapped in XML using SOAP encoding.
It makes consistent with other languages with in Dot Net.

Note:
Take care with the following Steps:
1. Must install MSMQ on your machine.
2. Drag and Drop the Message Queue into your Design Form.
3. You must be using System.Messaging;
4. Must declare a message for receiving messages. Here we declared as m.
5. Build the code before running.