Introduction to MSMQ

Microsoft Messaging Queue (MSMQ) technology is used for asynchronous communication using messages. MSMQ also can be considered to be an Inter- process communication capability.

Whenever two processes want to communicate with each other in a "Fire and Forget" manner, MSMQ is very useful for that.

Usage

For example what if Billing software needs to process 1000 bills at midnight and must send mail to all those users. Such as when the operator runs the software he wants immediate notification. The operator can't wait for all the bills to be processed and gets email.

Here MSMQ plays an important role for communication by Billing software to send those 1000's of customer email information into the Queue and the Queue event handler will handle the requests. In this way the Operator gets instant notification of the processes instead of knowing the "Behind the Scene" process.

So let's have a code walkthrough of how to create the process behind the scenes:

Step 1: Need to ensure MSMQ is properly installed in the computer.

1. How to check?

  • Type "compmgmt.msc" in the Run window or right-click on MyComputer and select "Manage Computer".
  • The following screen shot shows the MSMQ installed in the computer, if its not listed then follow Step "c" else done.

    screen-shot-shows-the-MSMQ-installed.jpg
     
  • Go to the Control Panel and select "Add or remove programs" under "Programs and Features"

    • After clicking on "Add or remove programs" again click "Turn Windows features on or off" displayed on left side.
    • Check (as in the following screenshot) whether MSMQ is enabled or not; if not then select "Enable" and press "OK"; see:

screenshot-whether-MSMQ-is-enabled.jpg

2. Once MSMQ is installed expand the MSMQ feature; there will be the following queues:

  • Private queues are queues that are not published in the Active Directory Domain Services and are displayed only on the local computer that contains them.
     
  • Public queues: In a domain environment, public queues are queues that are published in Active Directory Domain Services and hence are replicated throughout your Windows Server® 2008 family forest. Note that only the properties for these queues are replicated, not the actual queues themselves or their contents.

3. Now we have to create the queues programmatically as per our requirements. So now its time to code.

4. How to create a MSMQ using C#:

  • First we will check if the queue exisits; if it does then ok else we will create the queue as in the following.

    Declare the queue instance as follows:

    1. MessageQueue billingQ = new MessageQueue();

    //Setting the QueuPath where we want to store the messages.
    billingQ.Path = @".\private$\Bills";

    2. Now to check Queue existence:

    If(MessageQueue.Exists(billingQ.Path))

    {

    //Exists

    }

    Else

    {

    // Creates the new queue named "Bills"

    MessageQueue.Create(billingQ.Path);

    }

     

  • Now that the queue is created, the next step is to send data to the "Bills" queue.
  • The following code/function sends data/message to the "Bills" Queue:

    private void sendData2Queue()

    {

    billingQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });

    billingQ.ReceiveCompleted += billingQ _ReceiveCompleted;

    billingQ.Send("Desired Messages");

    billingQ.BeginReceive();

    billingQ.Close();

     

    }

The above function helps us to send data to the queue which is asynchronous because the operator or user using this application is not aware of the actual process happening behind the scenes.

Now once the data has arrived, the MSMQ callback handler will receive the messages and will do the actual process of "Sending the mails with the appropriate data".

billingQ.ReceiveCompleted += billingQ _ReceiveCompleted;

The above line of code tells MSMQ to receive the messages as soon as the new entry or message is placed in this queue.

The following function is used to receive the message sent by the front end (the application):

void billingQ _ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)

{

try

{

var msg = billingQ.EndReceive(e.AsyncResult);

string data = msg.Body.ToString();

// Process the logic be sending the message

//Restart the asynchronous receive operation.

billingQ.BeginReceive();

}

catch(MessageQueueException qexception)

{}

 

}


So that few lines of code makes your application communicate asynchronously and very easily.

In future articles we will learn about the Transactional Queue, so that we are assured that our sent messages are not lost.

I hope you enjoyed the article and provide me your valuable inputs.

Up Next
    Ebook Download
    View all
    Learn
    View all