Practical Implementation of MSMQ Using ASP.Net and SQL Server
Microsoft Message Queuing (MSMQ) technology enables applications running at different times to communicate across different networks and even the systems that may be available. MSMQ component delivery the message without file, efficient routing, security, reliable, and priority-based messaging. Even MSMQ handles both asynchronous and synchronous messaging type queuing.
Due to the high-speed networks, new requirement for technology to cater to a lot of high scale distributed applications. Now a day’s everything has to be fast and reliable even if the applications are not in the same location. To resolve this Microsoft introduced 3-Tier Arch. Even there is some many draw back in the 3-tier Arch, Consider a situation when so many people trying to register for a course in the university. Due to the over load server can’t able to response to the huge request. Because of that huge request, so many users request are rejected. To avoid this Microsoft Introduced MSMQ. MSMQ allows the application running at different times to communicate across different network and even the systems that may be available. It supports reliable transaction.
It enables the application to communicate across different network; even the server is temporally not available. Client Applications send and stores the messages to queues and read messages from queues.
What is a message?
A message is a collection data or chunk of information that is sent from a process running on client pc to other running process on the same or different computers on different network.
Queue?
Queues are nothing but First-In-First-Out (FIFO) data structures. Queues can be created using the MSMQ management tool through code or manually. Queues can be created and used dynamically. However you need to be aware of issues that might be caused by replication delays in the case of public queues. Private queues does not have such issues And there is no limitation for queues in MSMQ. You can able to create ‘n’ number of queues.
Types of Queues:
- Public queues are those that are published in the MQIS and are replicated throughout the Enterprise. They can hence be located by any computer on the Enterprise.
- Private queues on the other hand are not published in the MQIS and can only be accessed by systems that have access to the full path name or format name of the queue.
- Store and Forward Communication
It enables applications to send requests to other applications even the destination is unreachable at the same time. It queued (store) the application and process the request once the destination is reachable. - Concurrent Execution
It allows sending request to heterogeneous network without getting acknowledged from the destination. We can also compare it with parallel process.
- Guaranteed Message Delivery
- Asynchronous communication
- Transaction support
- Message routing services
- High scalability
- Integration with Windows NT Security System
Where do we need?
MSMQ features
Sample Demo Application
This sample application to support people to register for application. And this application provide User interface using ASP.NET. Once the User completed entering the message and the message called the MessageQueue, which in turn call the “registration “ which is a private queue in my local system. A server Application implementing MSMQ provides the Message Processing by adding this information to a database and sending a response email.
Client Code to store MSMQ Message:
try
{
System.Messaging.MessageQueue mq = new System.Messaging.MessageQueue (path.Text);
mq.DefaultPropertiesToSend.Recoverable=true;
mq.DefaultPropertiesToSend.Label=”registration”;
//Test myTest = new Test(txtuserid.Text,txtpass.Text,txtcpass.Text,txtname.Text,txtcname.Text,txtadd.Text,txtcity.Text,txtcon.Text,txtemail.Text);
Test myTest = new Test(txtuserid.Text,txtpass.Text,txtcpass.Text,txtname.Text,txtcname.Text,txtadd.Text,txtcity.Text,txtcon.Text,txtemail.Text,phone.Text);
System.Messaging.Message msg=new System.Messaging.Message();
msg.Formatter =new System.Messaging.BinaryMessageFormatter();
msg.Body=myTest;
//txtadd.Text=”123″;
mq.Send(msg,”registration”,System.Messaging.MessageQueueTransactionType.Single);
// Code to send email
string strTo = txtemail.Text;
string strFrom = “Your _mail@rediffmail.com”;
string strSubject = “Thank you “+txtname.Text+” for testing my demo application”;
string message=”Dear “+txtname.Text+”,”+”\n\n”+”Thank you for testing my demo application. I hope you understand the scope of MSMQ.”+”\n\n”+”Thanks\n”+”S.SathiyaSivam”;
SmtpMail.SmtpServer = “localhost”;
System.Web.Mail.SmtpMail.SmtpServer = “localhost”;
SmtpMail.Send(strFrom, strTo, strSubject,
message);
}
Code to retrieve from message from queue:
MessageQueue q = new MessageQueue(@”.\Private$\registration”);
Test tst=null;
if(MessageId!=null)
{
// need to strip off the curly brackets to use ReceiveById!
string msgid=((string)MessageId).Replace(”}”,”");
msgid=msgid.Replace(”{”,”");
msg= q.ReceiveById(msgid);
Type[] expectedTypes= new Type[]{typeof(string)};
msg.Formatter =new System.Messaging.BinaryMessageFormatter();
System.Diagnostics.Debug.WriteLine(msg.Id.ToString());
tst=(Test)msg.Body;
}
MessageBox.Show(”Your Request Is Processed From The Queue”);
string sSql=”Insert into registration (UserId,Pass,Cpass,Name,Cname,Padd,city,Country,Email,Phone)Values(’”+tst.Uid+”‘,’”+tst.Pass+”‘,’”+tst.Cpass +”‘,’”+tst.Uname+”‘,’”+tst.Cname+”‘,’”+tst.Add+”‘,’”+tst.City+”‘,’”+tst.Con+”‘,’”+tst.Email+”‘,’”+tst.Phone+”‘)”;
System.Diagnostics.Debug.WriteLine(sSql);
SqlConnection cn =new SqlConnection(”Server=.;Database=TEST;Trusted_Connection=True;”);
cn.Open();
SqlCommand cmd=new SqlCommand(sSql,cn);
cmd.CommandType=CommandType.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
cn.Close();
Steps to create private queue :
- Create a new Private Queue called “registrtion”
- Check the Transactional Queue check box in the Private Queue window.
- Create a new Trigger for Peeking, enabled, serialized, called “Registration”
- Create a new Rule called “reg_rule” with a Rule Condition of “Message Label contains registration”
- For the Rule Action, specify the path to the backend project executable (on my machine, this is “C:\CSHARPBIN\registration\backend\bin\Debug\backend.exe”
- In Parameters, pass the MessageId as Variant, and the MessageBody as String.
- In your Trigger’s attached rules, ensure that the Rule Notify is attached.
- Ensure that both the MessageQueue and the MessageQueue Trigger services are running.
Copyright @ Sathiyasivam 2007

- None Found
- Create a Vibrant Drinks Ad
- Design a Cool Music Wallpaper
- Design a Professional Laptop Advert
- Create a Magical Image using Photo Manipulation
- Create an Awesome Paint Explosion Image
- Making of Fire Dragon
- Designing a Diving Wallpaper
- Round Tubes
- Halloween Pumpkin
- Surprise behind the curtain in Photoshop
Friends' Sites
Contact Us
Categories
- 3D
- ASP
- C#
- Database
- Flash
- GIMP
- Hosting
- Illustrator
- Java
- Javascript
- Linux
- Photoshop
- PHP
- Web Design
- Windows


2,708 views
1 Comment
1 Comment
Jump to comment form | comments rss [?] | trackback uri [?]