C # using Message Queuing (MSMQ)

First talk about, Message Queue (MSMQ Microsoft Message Queuing) is a service provided by the MS, which is the Windows operating system function, not .Net provides.

On MSDN explained as follows:

Message Queuing (MSMQ) technology enables applications running at different times to communicate across heterogeneous networks and systems that may be temporarily offline.

Applications send messages to queues and read messages from queues.

The following illustration shows how a queue can hold messages that are generated by multiple sending applications and read by multiple receiving applications.

Message Queuing (MSMQ) technology capable of operation so that communication between the various network and system may be temporarily offline applications in different times.

Applications send messages to queues and reading messages from the queue.

The following illustration shows how to save the message queue a plurality of messages generated by the sending application, and a plurality of receiving application to read.

Once the message sent to the queue, it will persist, even though the application has been sent off.

 MSMQ service is off by default, (Window7 operating system and above) to open the following manner

1, open the Run, enter "OptionalFeatures", Microsoft Message Queue (MSMQ) server on the hook.

(Windows Server 2008R2 and above) to open the following manner

2, open the Run, and type "appwiz.cpl", select "Turn Windows features" in the task list

然后在"添加角色"中选择消息队列

 消息队列分为以下几种,每种队列的路径表示形式如下:

公用队列 MachineName\QueueName

专用队列 MachineName\Private$\QueueName

日记队列 MachineName\QueueName\Journal$

计算机日记队列 MachineName\Journal$

计算机死信队列 MachineName\Deadletter$

计算机事务性死信队列 MachineName\XactDeadletter$

这里的MachineName可以用 “."代替,代表当前计算机

 需要先引用System.Messaging.dll

创建消息队列

 

//消息队列路径
            string path = ".\\Private$\\myQueue";
            MessageQueue queue;
            //如果存在指定路径的消息队列 
            if(MessageQueue.Exists(path))
            {
                //获取这个消息队列
                queue = new MessageQueue(path);
            }
            else
            {
                //不存在,就创建一个新的,并获取这个消息队列对象
                queue = MessageQueue.Create(path);
            }

 

发送字符串消息

 

System.Messaging.Message msg = new System.Messaging.Message();
                    //内容
                    msg.Body = "Hello World";
                    //指定格式化程序
                    msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                    queue.Send(msg);

 

发送消息的时候要指定格式化程序,如果不指定,格式化程序将默认为XmlMessageFormatter(使用基于 XSD 架构定义的 XML 格式来接收和发送消息。)

接收字符串消息

 

//接收到的消息对象
              System.Messaging.Message msg = queue.Receive();
              //指定格式化程序
              msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
              //接收到的内容
              string str = msg.Body.ToString();

 

发送二进制消息(如图像)

 

1             System.Drawing.Image image = System.Drawing.Bitmap.FromFile("a.jpg");
2             Message message = new Message(image, new BinaryMessageFormatter());          
3             queue.Send(message);

 

接收二进制消息

 

1             System.Messaging.Message message = queue.Receive();
2             System.Drawing.Bitmap image= (System.Drawing.Bitmap)message.Body;       
3             image.Save("a.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

 

获取消息队列的消息的数量

 

 int num  = queue.GetAllMessages().Length;

 

清空消息队列

 

queue.Purge();

Guess you like

Origin www.cnblogs.com/ashbur/p/12073479.html