Gal Ratner
Gal Ratner is a Techie who lives and works in Los Angeles. Follow galratner on Twitter
How to send and receive messages from Microsoft message Queue


The message Queue is a windows component designed to reliably store cross domain messages. It has may uses in enterprise systems. For example: storing orders from a web service for background processing by a windows service.
Queues can be transactional and not transactional, meaning pulling of messages can be wrapped inside a transaction. Useful when pulling systems go offline in order to prevent loosing messages.
How to send a message to a Queue:                               

            MessageQueue myQueue = new MessageQueue("Path to the Queue");

            bool transactional = myQueue.Transactional;

            MessageQueueTransaction transaction = new MessageQueueTransaction();

 

            try

            {

                if (transactional)

                {

                    transaction.Begin();

                    myQueue.Send(message, "message title", transaction);

                    transaction.Commit();

                }

                else

                {

                    myQueue.Send(message, "message title");

                }

            }

            catch (Exception e)

            {

                if (transactional)

                    transaction.Abort();

            }

            finally

            {

                transaction.Dispose();

                myQueue.Dispose();

            }

How to receive a message from a Queue:

           

            MessageQueue myQueue = new MessageQueue("Path to the Queue");

            bool transactional = myQueue.Transactional;

            MessageQueueTransaction transaction = new MessageQueueTransaction();

            ((XmlMessageFormatter)myQueue.Formatter).TargetTypes = new System.Type[] { Type.GetType("my message class") };

            workflowQueue.MessageReadPropertyFilter.Priority = true;

 

            try

            {

                if (transactional)

                {

                    transaction.Begin();

                    Message queueMessage = myQueue.Receive(transaction);

                    transaction.Commit();

                }

                else

                {

                    Message queueMessage = myQueue.Receive();

                }

            }

            catch (Exception e)

            {

                if (transactional)

                    transaction.Abort();

            }

            finally

            {

                transaction.Dispose();

                myQueue.Dispose();

            }


Note: currently only Message Queue 4.0 supports transactional remote receive, so, you should be running Windows Server 2008 if you are going to be working with transactional Queues.
 

Posted 14 Jul 2009 3:20 PM by Gal Ratner

Comments

Gal Ratner wrote re: How to send and receive messages from Microsoft message Queue
on 8 Sep 2009 10:21 PM

On a side note: Queue.Transactional is only available on a local Queue. For a remote Queue, you will have set this value manually.

Powered by Community Server (Non-Commercial Edition), by Telligent Systems