MSMQ消息队列简单实例

先要开启,至于怎么开启,百度。

公共队列

MachineName\QueueName 

专用队列
MachineName\Private$\QueueName 一般用的是这个 MachineName本机的话用 . 代替,Private$是固定的,QueueName 是自己取的名字

日志队列

MachineName\QueueName\Journal$

private static string queuepath = @".\Private$\myqueue";

创建: MessageQueue.Create(queuepath);

发送消息:MessageQueue MQ = new MessageQueue(queuepath)//获得这个队列

                  var sendinfo ="{\"Messageinfo\":{\"name\":\""+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+""+"\"}";

                  System.Messaging.Message me = new Message();
                  me.Recoverable = true;
                  me.Label = "示例";
                  me.Body = sendinfo;
                  MQ.Send(me);

                  也可以自己新建一个类,用来存复杂消息

接收消息:

                  MessageQueue MQ = new MessageQueue(queuepath);

                  Message me = MQ.Receive();//每Receive一次便会将该消息从队列移除,也可以用Peek,这个不会移除

                  me.Formatter= new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });

                  var aa = me.Body.ToString();//数据



猜你喜欢

转载自blog.csdn.net/qq_35534449/article/details/80969887