标签:factor 速度 star hand ges art exception logs name
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);
标签:factor 速度 star hand ges art exception logs name
原文地址:http://www.cnblogs.com/ligenyun/p/7731942.html