Message Queue in .Net

In this article I will explain in brief about Message Queue concept in .Net Microsoft Message Queue (MSMQ) as an example.

What is the Queue?
Queue is nothing but FIFO (First In First Out).

Before going to Message Queue concept, why we need Message Queuing?
Answer is that, we require Message Queue mechanism when we want to store insignificant data which is not required to store and we want to do some operations on it.

For example, we want to accept data from multiple users and we have to do some operations on that data, after that we require writing data to some files. Then in this situation we need not require data to store in database, we just want to do some operations on that data and copy that to some files. This requirement will be fulfilled by Message Queue.

If we take another example, where we have two applications in two different systems. One application will send the data and another will need to process the data.

Here I am explaining second example but two applications are in same system.

To run Message Queue concept in .Net, we need Message Queue service to be installed in our system. By default installation of Operating System, it will not install. To install Message Queue service
Start->settings->control panel ->Add/Remove programs-> Click on Add/Remove window components-> Check Message Queuing ->Click Next button

If you unable install, please refer documentation of corresponding Operating System.

After successful installation of Message Queue, you have to write the code to use it.
Here I am explaining Message Queue by using VB.Net.

In this example we need two applications, one is to store data in Message Queue and another is to retrieve data from Queue.

To Store data in Message Queue (Sender Application):

Open Visual studio Framework->Create New Project-> Select Windows Application->save it as Sender_MQ

Before proceeding into the code, add reference to System.Messaging.
Right click on Solution Explorer and select Add Reference System.Messaging, press ok.

The code to insert messages into Message Queue is given below.

    Imports System.Messaging
    Imports System.Runtime.Serialization
    
    Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
                            Handles Button1.Click
        sendToQueue()
    End Sub
    Sub sendToQueue()
        Try
            Dim strque As String
            strque = ".\PRIVATE$\MsgQueue"

            sender_queue.Text = sender_queue.Text.Trim
            If sender_queue.Text = "" Then
                MessageBox.Show("Please enter message for Queue")
                Exit Sub
            End If

            If (MessageQueue.Exists(strque)) = False Then
                MessageQueue.Create(strque)
            End If
            Dim oQueue As New MessageQueue(strque)
            oQueue.Send(sender_queue.Text)

            MessageBox.Show("Message submitted to Queue")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class


First we need to import namespaces for System.Messaging and System.Runtime.Serialization

Here we have a simple form which has one textbox and one button. In the button click event, we created MsgQueue under Private Message Queue’s type. Whenever user clicks the button we will create Message Queue and textbox will store in created Queue.

To Retrieve data From Message Queue (Receiver Application):

Open Visual studio Framework->Create New Project-> Select Windows Application->save it as Receiver_MQ

Before proceeding into the code, add reference to System.Messaging.
Right click on Solution Explorer and select Add Reference System.Messaging, press ok.

The code to retrieve messages into Message Queue is given below.

    Imports System.Messaging
    Imports System.Runtime.Serialization
    
    Public Class Form1
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
                    Handles Button1.Click
        receiveFromQueue()
    End Sub
    Sub receiveFromQueue()
        Try
            Dim strque As String, msg As Message
            strque = ".\PRIVATE$\MsgQueue"

            If (MessageQueue.Exists(strque)) = False Then
                MessageQueue.Create(strque)
            End If

            Dim oque As New MessageQueue(strque)


            oque.Formatter = New XmlMessageFormatter(New Type() {GetType(System.String)})

            Dim que_len As Message() = oque.GetAllMessages

            If que_len.Length > 0 Then
                msg = oque.Receive(New TimeSpan(0, 0, 5))
                receiver_queue.Text = msg.Body
            Else
                MessageBox.Show("No Messages in Queue")
                receiver_queue.Text = ""
            End If


        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class


For this also we need to import namespaces for System.Messaging and System.Runtime.Serialization.

Here also we have simple form with one textbox and one button. Whenever user clicks on button, we will check the Message Queue, if any data in the Queue will display in Textbox.

For testing execute sender application and enter some data in textbox and click submit button, then it will store in Message Queue.
Repeatedly enter data in textbox and click button. Do this for several times.

Now open Receiver application and click button, you can notice that whatever data you entered in Sender Application you will find it in Receiver textbox in FIFO(First In First Out) manner.

In this way you can use Message Queues in several situations where we want to do some operations on data from multiple users at a time.


Download source code here