MSMQ(微软消息队列)简单使用-支持离线提交数据(供阅)

8 篇文章 0 订阅
 #region MessageQueue消息队列(发送/接收)
            //第一条数据
            MessageQueue sendQueue = new MessageQueue(".\\Private$\\MSMQDemo");
            Message sendsg = new Message();
            sendsg.Body = "This is test of messagequeue ";
            sendsg.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
            Console.WriteLine("MessageQueue has sent succefully");
            sendQueue.Send(sendsg);
            //接收第一条
            MessageQueue accpetQueue = new MessageQueue(".\\Private$\\MSMQDemo");
            Message accpetMsg = accpetQueue.Receive();
            accpetMsg.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
            string accptTxt = accpetMsg.Body.ToString();
            Console.WriteLine(accptTxt);
           
            //加载第二条数据
            sendsg.Body = "No.2 test of messagequeue ";
            sendsg.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
            Console.WriteLine("MessageQueue has sent succefully");
            sendQueue.Send(sendsg);
            //接收第二条
            MessageQueue accpetQueue2 = new MessageQueue(".\\Private$\\MSMQDemo");
            Message accpetMsg2 = accpetQueue.Receive();
            accpetMsg2.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
            string accptTxt2 = accpetMsg2.Body.ToString();
            Console.WriteLine(accptTxt2);
            #endregion
 
在使用MSMQ之前 要先添加MSMQ消息服务  在添加删除程序里面添加Windows组件  然后在计算机-管理-服务和应用-消息队列里面在专用队列新增一个专用队列 如我这里建的是名为MSMQDemo的队列
 
消息队列的用处:消息队列可以保证客户端与服务器端通信的时候  由于并发而产生的服务端无法及时反馈给客户端信息而造成的服务不善  消息队列就相当于先将客户端提交的数据按照队列的FIFO规则加载到队列中  然后逐个发送给服务端  这样保证客户端与服务端失去连接后仍然能将数据提交给服务端  因为消息队列支持离线提交数据  有了它  就可以防止服务端因高并发而造成宕机
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值