Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Use MSMQ

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 442
    Comment on it

    Hi Friends,

    Today i will discuss you about MSMQ(Microsoft Message Queuing).

    MSMQ is a message queing service where you send a message from one application and receive by another application.
    The application are don't need to be in execution state. Mean if sender send the message to the queue and receiver is offline. Then receiver can get the message when it will be online again.

    Basically the queue is maintained by the host OS. 

    I have used two Methods one will send the message in the queue and another will fetch the message from the queue.

    MSMQ will use System.Messaging reference.

    I have used the below steps:-
    1. Firstly add the reference of System.Messaging in your project refrences list.
    2. We will create two methods one for Sending message to the queue and other will be used to get the message from the queue.
    3. The code to send and receive message in the queue as shown below :-

    Send Message:-

    /// <summary>
            /// Send message to queue
            /// </summary>
            /// <param name="queueName"> Name of the Queue </param>
            private static void SendMessage(string queueName)
            {
                MessageQueue msgQueue = null;
    
                //This code will check id the queue exists or not,if not exists then we will create the the Queue.
                //Otherwise we will take the existing queue object. 
                if (!MessageQueue.Exists(queueName))
                {
                    msgQueue = MessageQueue.Create(queueName);
                }
                else
                {
                    msgQueue = new MessageQueue(queueName);
                }
    
                try
                {
                    //Clear all messages from queue.
                    //msgQueue.Purge();
    
                    //Create new instance of the class and fill up object with some sample values.
    
                    MessageLog msg = new MessageLog()
                    {
                        Id = 1,
                        Message = "Test"
                    };
    
                    //send message to queue.
                    msgQueue.Send(msg);
                }
                catch (Exception ex)
                {
                    Console.Write(ex.ToString());
                }
    
                //close the queue
                msgQueue.Close();
            }

    Receive Message :-

    /// <summary>
            /// Receive message from the queue.
            /// </summary>
            /// <param name="queueName">Name of the queue</param>
            private static void ReceiveMessage(string queueName)
            {
                //Create the new instance of MessageQueue with existing queue. 
                MessageQueue msgQueue = msgQueue = new MessageQueue(queueName);
    
                try
                {
                    //We will use XmlMessageFormatter to format the message in the queue.
                    msgQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(MessageLog) });
                    //get the message from queue.
                    var message = (MessageLog)msgQueue.Receive().Body;
                }
                catch (Exception ex)
                {
                    Console.Write(ex.ToString());
                }
    
                msgQueue.Close();
            }
    

    Where MessageLog is a new class.

    public class MessageLog
    {
            public int Id { get; set; }
            public string Message { get; set; }
    }

    Where queueName is a name of the queue with path and it will be in below format :-

    const string queueName = @".\Private$\TestQueue";

    To see the queue just right click the "My Computer".
    Click on Manage. Then expand "Service and Applications".

    You can see the Message Queuing(If you are not able to see.You need to turn on and off new program feature from control panel's "Program and Feature".Otherwise goto this link 
    https://msdn.microsoft.com/en-us/library/aa967729(v=vs.110).aspx)

    Then open the Private Queues.You can see all your queue that you have created.

    To delete the Queue just write the below line.

    MessageQueue.Delete(queueName);

    If you want to clear all the messaages from the queue use below code :-

    MessageQueue msgQueue = new MessageQueue(queueName);
    msgQueue.Purge();

    If you have any query please write in the comments below.

    Thanks for reading
    Happy Coding!!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: