Friday, May 11, 2007

Programming MSMQ (Microsoft Message Queue) - Sample code

Yesterday, I went to "MSDN Connection Training Sneak Peak Preview" seminar at Microsoft Thailand. The topic is "Distributed Application Development with Visual Studio 2005". It is actually a shortened version of Iverson training course : if you want to take a full course, you will have to pay at least ten thousand baht for 3 days training o__O!

I really learned many things from this session. One of them is MSMQ or Microsoft Message Queue.

Message queuing is a communication tool that allows applications to reliably interconnect in a distributed environment where one of the applications may or may not be available at any given time. The queue acts as a holding container for messages as they are sent between applications. The applications send messages to and read messages from queues to communicate back and forth. An application writes a message to a queue, which will then be received and processed by another application at some point determined by the receiving application. This type of communication is designed for asynchronous use where the applications involved are not waiting for an immediate response from the other end. - http://www.codeguru.com/Csharp/.NET/net_general/netframeworkclasses/article.php/c4241/


This really fits into my project's need for a queue which can serve clients over the internet.

To program the MSMQ you need to install Messaging Queuing first. Insert your Windows Setup CD and choose "Install optional Windows components". Then, tick the checkbox in front of "Message Queuing" and go on.



Now, create a new Visual C# Console project. Add reference to System.Messaging and add the following code.




using System;
using System.Collections.Generic;
using System.Text;
using System.Messaging;
using System.Threading;

namespace MSMQSample
{
class Program
{
static void Main(string[] args)
{
// Check if queue alreasy exists.
string queuePath = @".\private$\SampleQueue";
MessageQueue queue;
if (!MessageQueue.Exists(queuePath))
// If not, create one.
queue = MessageQueue.Create(queuePath);
else
queue = new MessageQueue(queuePath);

// Send something to queue.
DateTime dt = DateTime.Now;
Console.WriteLine("Message to send: " + dt.ToString());
Message sendMsg = new Message(dt, new XmlMessageFormatter());
queue.Send(sendMsg, "My DateTime");

// Wait for five seconds.
Thread.Sleep(5000);

// Get sent message from queue.
Message receiveMsg = queue.Receive();
receiveMsg.Formatter = new XmlMessageFormatter(
new Type[] { typeof(DateTime) });
DateTime ret = (DateTime)receiveMsg.Body;

Console.WriteLine("Message Received: " + ret);
Console.Read();


}
}
}


And this is what our message looks like. You can open this window in Computer Managment/Services and Applications/Message Queuing/samplequeue



Here are some great resources on the basics of MSMQ:
And for those who want to attend next MSDN Connection Training Sneak Peak Preview you have to register for an account at http://www.msdnconnection.com/thailand and monitor this page [MSDN Connection Training Sneak Peak Preview]



4 comments:

nekrataal said...

that's a very simple code for sending message to another process

but can it send over tcp/ip?

Anonymous said...

you can do the same thing in Java by using JMS as the API to connect to any Queue Server that support it.

Apache ActiveMQ is one of the famous open source Q-Server.

@nekrataal : which kind of protocol can be used is depend on each Q-Server. Developer shouldn't care what underlying protocol will be used.

Unknown said...

nekrataal: Yes, the ability to send and receive messages over HTTP was implemented in MSMQ 3.0 (version which came with XP and 2003). You may be interested in this article - Send MSMQ Messages Securely Across the Internet with HTTP and SOAP [http://msdn.microsoft.com/msdnmag/issues/03/12/MSMQandNET/]

RocketMail Customer Service said...

Despite so many features, sometimes RocketMail fails to face all the challenges of the users. The platform provides several services like managing email accounts, contacts, account settings, resetting or recovering email passwords and many more. In order to avoid all the technical errors for an email account, it is requested to contact our expert and professional team members at RocketMail Customer Service Number. With years of experience, they are fully dedicated to assisting users with 100% satisfaction.