Friday, September 2, 2011

Simple MSMQ tester over http


Well I guess I should have called this blog monthly instead of weekly developers notes...

Anyway here's a short input from my last week where I had to test the ability to send MSMQ over http.

It's very easy writing a simple console app to test this.

First you need to install the MSMQ role on your server or dev. computer.
In my case I used windows 7 so it looks like this: (sorry it's in swedish but I hope you can guess the settings anyway)


When you'll need to create your queue. Open computer management, expand service and programs and you should see at the bottom of this expanded view a new "messages" entry. Expand it and right click private queue to create a new one. Choose it to be transactional and click ok. That's all you need to do! Per default all users can send to the queue.

Our test queue is ready. Now we just need to write a simple client to send messages.

Start visual studio and a new console application project. Add a reference to System.Messaging and a using statement in your code:

using System.Messaging;

In order to send to a transactional message queue you need the message to be ... transactional! of course...
"MessageQueueTransaction" type is used for this.

Then you need to define a queue and a message to send on it:
"MessageQueue" &
"Message" types are used for this.

Begin the transaction, define the queue, the message with its properties, send it and commit the transaction...
Below is the code sample for this:

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

namespace myTestMSMQConsoleApp
{
    class Program
    {

        MessageQueueTransaction m_mqtrans = null;

        static void Main(string[] args)
        {
            Console.WriteLine("Start testing sending transactional messages to msmq over http...");
            Console.WriteLine("Please enter the server name where to send (default is localhost)");
            string serverName = Console.ReadLine();
            if (serverName == null || serverName == "" || serverName == ".")
                serverName = "localhost";
            Console.WriteLine("Please enter the path of the queue where to send (ex: private$/myprivatetestqueue)");
            string queuePath = Console.ReadLine();
            queuePath.Trim('\\');
            queuePath.Replace("\\", "/");
            string myMessageStr = "This is a test message!";
            string myLabel = "testLabel";
            Program myProgram = new Program();
            myProgram.SendStrMessage(myMessageStr, myLabel, "FORMATNAME:DIRECT=http://" + serverName + "/msmq/" + queuePath);
            Console.WriteLine("Message sent to queue");
            int i = Console.Read();
        }

        public void SendStrMessage(string sMessage, string sLabel, string TargetQueue)
        {
            if (m_mqtrans == null)
            {
                m_mqtrans = new MessageQueueTransaction();
                m_mqtrans.Begin();
            }

            MessageQueue mq = new MessageQueue(TargetQueue);
            Message msg = new Message();
            msg.Formatter = new ActiveXMessageFormatter();
            msg.UseJournalQueue = false;
            msg.UseDeadLetterQueue = true;
            msg.Recoverable = true;
            msg.Body = sMessage;
            msg.Label = sLabel;

            mq.Send(msg, m_mqtrans);

            m_mqtrans.Commit();
            m_mqtrans.Dispose();
        }
    }
}
This was written in a hurry but I hope that it'll help then you need to test sending MSMQ messages.


No comments:

Post a Comment