How MSMQ saved my day (and my application too!)
Have you ever tried opening twenty applications on a 1 GB RAM machine? Or tried transferring five blue ray movies in five different windows on that same machine? “Why would you do that, that’s gonna kill your machine!” I can hear some of you saying. I did something similar a couple of days back when I had to send an email to 5000 people. Now I know what you are thinking. Sending mails ain’t rocket science! I agree. But sending it to 5000 people would’ve made my web application unresponsive and I simply could not afford that. I needed a workaround, and that’s when I instantly thought of MSMQ!
MSMQ: My Saviour (Microsoft) Message Queue
Microsoft Messaging Queue (MSMQ) is a message storage area which can be used by one or more than one applications to store and retrieve messages. I used MSMQ to store all the emails on a button click and then wrote a windows service to send the mails to all my recipients. Sending these records to MSMQ queue was not a time consuming job, so it was done rather quickly and the application was free to service the next user request.
First things first: Installing MSMQ
Before I show you how to make MSMQ work, let me first show you how to install it properly.
Step 1: Go to Control Panel –> Programs –> Turn Windows Features On or off
Step 2: Select Microsoft Messaging Queue (MSMQ) Server
This will install MSMQ on your machine.
Creating a Queue
In order to create a new queue, simply right click on My Computer and select Manage, as I have shown below:
You can also create a queue through programing using a System.Messaging namespace. If you are working on Visual Studio for Web, you might need to add this DLL from C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.1. For creating a new queue, you will need to specify a queue name in a specific format. The queue name needs to start with a “.” and should have a “$” after private, as shown below in the example. e.g. “.Private$Queue1”
The above code will create a new queue if it does not already exist.
The battle begins: Inserting Messages in the Queue
Now came the crunch part: to insert messages in the queue I just created. I used the following code snippet to insert the messages into my newly created queue:
Providing the finishing touches: Retrieving Messages from the Queue
Now all I had to do was to retrieve these inserted messages and send mails or perform the required operation. I retrieved all the messages from Queue1 and saved them in an array, and then deleted all the messages in Queue1 using purge().
You need to add a reference to the Web Application’s DLL using which you can insert the message in the queue to get the message content in the format specified in your EmailMessagedata class.
My takeaway
Using this approach, I was able to save a lot of time as all my emails were stored in the MSMQ. I did not have to wait while my emails were being sent; I was able to continue using the application while the windows service was sending the emails in the background. All this while, my application remained responsive. If you need to execute time consuming tasks, I would recommend you use MSMQ. Just as it saved my day, I am sure it will save yours too.