Thursday, October 04, 2012

[ Dot Net Remoting ] - Using MSMQ for Sending Message


1) Introduction

MSMQ stands for "Microsoft Message Queue". MSMQ is a message storage area where one more application contacts to send or receive the messages. The message is packed information and it can be plain text in XML or Binary or another supported format. Look the Fig.1 below:

Fig.1


Message Queues allows communication between one or more application and in the meantime it won’t force the application being communicated should be in live connection. Application 1 can send information and quit or do another task. Application 2 when free looks at the queue and catches the passed-in communication.

MSMQ can maintain one or more queue. In the above picture, three queues are shown for an example. We are going do develop a sample in this article, which will send and receive the information through the MSMQ as shown in fig.1. We are going to use only one queue say Q1. Nevertheless, you can create multiple queues based on the need.



2) Installing MSMQ Windows Component

MSMQ is a windows component like IIS services. So first, you should make sure that the MSMQ component is installed on your machine. To do that, go to the services window by typing services.msc in the run prompt. Make sure that the services shown in Fin.2 are available and running on your machine:

Fig.2



If the services are not available then the MSMQ windows component is not installed on your machine. The video given below shows how you can install the MSMQ in Win 7. The steps are same for windows XP also. Only the user interface displays changed slightly.

Video 1
Steps:
1) Control panel is invoked
2) Program Features is opened
3) Turn Windows Feature On/Off is selected from side pane
4) From the window displayed, MSMQ component is selected and installed.
If you are using the WindowsXP, Add/Remove Windows Components should be selected as shown in Fig.3.
Fig.3
Once you install the MSMQ component, it is a good practice to restart the machine for a smooth launch of the MSMQ services. However, this step is not a mandatory one.

3) Accessing the MSMQ

Once MSMQ windows component is installed on your machine, you can access it through the computer management utility. There are many ways available to invoke the computer management. Fig.4 shows the computer management and MSMQ location in the left side tree. In the run prompt, one should type "mmc.exe compmgmt.mmc" to invoke the computer management.

Fig.4

The video shown below is loading the computer management using other two methods. In the first method, we loading it through the Administrative tools and in the second method we are using the window explorer to load it. In windows explorer, we invoked the context menu for the My Computer icon then selected the menu option Manage.

Video 2

Steps:

1) From the start menu, administrator tool is accessed
2) Then, the computer management option selected from the displayed menu.
3) The Computer management window is closed
4) Windows explorer is opened
5) Manage option is selected by right clicking the My Computer icon
6) Location MSMQ is navigated from the displayed Computer Management administration window.

4) About the Sample


The sample application’s screenshot is shown below in fig.5

Fig.5
When we click the send button, the details entered into the textboxes are posted to the MSMQ as separate messages. The send button also forms a class instance with all three details and sends it as a single message. There is nothing more to talk about this sample at this moment. Let us start with developing the sample.
This sample deals with only sending the message to MSMQ. In the next article, a separate sample will be given and that will read message send by a sample application that we are going to discuss in this article.

5) Providing reference to MSMQ


When we started the C# Windows application project, the IDE will not provide the reference to the MSMQ DLL. Therefore, we cannot access the message queuing functionalities. To bring the message queuing capabilities to the application we should provide assembly reference to the "System.Messaging". This is shown in the below video:

Video 3


Steps

1) From the project, explorer reference node is right clicked
2) From the context menu Add reference… is invoked
3) From the displayed dialog, the Assembly binary for System.Messaging is selected
4) Then in the source window using statement for the messaging is provided
The using statement included in the source code is below:
//Sample 01: Include the required namespace
using System.Messaging;

6) Create MSMQ


You can create the Message Queue from the computer management screen itself. But, I will leave that to you to explore it yourself (Not a big deal). First, we declared a variable that holds the Queue Name:

//Sample 02: Message Queue Name
string QueueName = ".\\Private$\\Q1";

The . notation says that the queue is on the local machine. You can try the queue on a remote machine by replacing the dot with system name. Then we specified that the queue is a private queue and queue name is Q1. Once you see the Q1 in the Computer Management screen, you will come to know about the above statement easily.

Next, we checked that the queue specified above exists or not. When it doesn't exist, we create that queue. Checking the existence and creating the queue is done in the form load of the sample. This code is shown below:

private void frmSender_Load(object sender, EventArgs e)
{
    //Sample 03: Check Queue Exists, if not create it.
    if (!MessageQueue.Exists(QueueName))
        MessageQueue.Create(QueueName);
}

The Queue Q1 is shown in the below video.

Video 4


Steps
1) Computer management is accessed
2) Then, inside the Management window, MSMQ is navigated
3) Queue created in the Form Load is shown

7) Person Class


We are going to send the person class instance to the MicroSoft Message Queue. To add a person class right-click on the project name and select Add Class. The complete person class is shown below:

//Sample 04: The person class that needs to be sent to MSMQ
[Serializable()]
public class Person
{
    public string m_name;
    public string m_age;
    public string m_occupation;
    public Person() { }
    public void SetDetails(string name, string occ, string age)
    {
        m_name = name;
        m_age = age;
        m_occupation = occ;
    }
}

As we are going to send the instance of the Person class to the message queue, we specified that it should be serializable by marking the class with the arttribute [Serializable()]. Also, note that we provided a public constructor as well as a default constructor (A constructor that won’t take any parameter(s) ). The member that we want to include in the message body is set with the public scope.

8) Sending Message to the created Queue


When we click the send button of our sample application, the queue is referenced and through that, we will send the message. Note that the queue existence is already performed in the form load. In the click event handler of the send button, we are just going to get a reference to the existing queue.

//Sample 04: Get the reference the Queue that exists/created (During for load)
MessageQueue Q1 = new MessageQueue(QueueName);
Person p = new Person();
p.SetDetails(txtName.Text, txtOccupation.Text, txtAge.Text);

Here, we first get a reference to the Q1 queue. Then, an object for a person is created and the person’s details are set through a member function. We are going to send this instance p to the message queue using the "default xml formator". We will explore other formators in a different article later.
Sending message to the message queue is done through a call to Send member function. First, we sent three string messages, which are nothing, but the Person’s details collected from the form. All these three messages are separate entries in the Queue Q1. Next, we sent all the three information packed inside the instance called p as a single message. Below is the code:

//Sample 06: Now send the person to the message queue
Q1.Send(txtName.Text);
Q1.Send(txtOccupation.Text);
Q1.Send(txtAge.Text);
Q1.Send(p);

The below video shows the complete application in action:

Video 5

Steps
1) The sample exe is loaded
2) Data entered into the form and send button is clicked
3) Q1 in the message queue is navigated
4) All four messaged are shown [Three string and one class instance in xml format]

Note reading the queue is not shown in the article. We see that in the next article on Dotnet.remoting. See U then.

Source Code: Download

No comments:

Post a Comment

Leave your comment(s) here.

Like this site? Tell it to your Firend :)