首页 > Web开发 > 详细

ASP.NET MQ 消息队列

时间:2017-10-25 20:28:31      阅读:496      评论:0      收藏:0      [点我收藏+]

1.引入

2.发送消息

3.接收消息

概述:MQ消息存放在内存,重启后,消息丢失。接收后,消息丢失(只取一次),不取,一直在且速度快。

一 引入DLL

技术分享

 

二 发送消息

 

 try
            {
                InitFactory();
                using (IConnection connection = _factory.CreateConnection())
                {
                    //Create the Session  
                    using (ISession session = connection.CreateSession())
                    {
                        //Create the Producer for the topic/queue  
                        IMessageProducer prod = session.CreateProducer(
                            new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(type));

                        //Send Messages  

                        ITextMessage message = prod.CreateTextMessage();
                        message.Text = msg;
                        prod.Send(message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue);


                    }
                }
            }
            catch (Exception ex)
            {

            }

三 接收消息

        protected static void GetTopic(string type, string name, GetTop handel)
        {
            InitFactory();
            //通过工厂构建连接
            IConnection connection = _factory.CreateConnection();
            //这个是连接的客户端名称标识
            connection.ClientId = name;
            //启动连接,监听的话要主动启动连接
            connection.Start();
            //通过连接创建一个会话
            ISession session = connection.CreateSession();
            //通过会话创建一个消费者,这里就是Queue这种会话类型的监听参数设置
            IMessageConsumer consumer = session.CreateDurableConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(type), name, null, false);
            //注册监听事件
            consumer.Listener += new MessageListener(message =>
            {
                var msg = (ITextMessage)message;
                handel(msg.Text);
            });
        }



        public delegate void GetTop(string message);

 

ASP.NET MQ 消息队列

原文:http://www.cnblogs.com/ligenyun/p/7731942.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!