客户端
1 public void StartClient() 2 { 3 #region 方式一 4 //创建连接工厂 5 IConnectionFactory factory = new ConnectionFactory("tcp://192.168.1.20:7018"); 6 try 7 { 8 //通过工厂构建连接 9 connection = factory.CreateConnection("admin", "roZXu8WD"); 10 //这个是连接的客户端名称标识 11 connection.ClientId = "shanghaotest"; 12 connection.Start(); 13 //通过连接创建一个会话 14 ISession session = connection.CreateSession(); 15 //通过会话创建一个消费者,这里就是Queue这种会话类型的监听参数设置 16 //IMessageConsumer consumer = session.CreateConsumer(new ActiveMQTopic("aimms.aimmsmq.topic.aimms.aimms.detection.data")); 17 IMessageConsumer consumer = session.CreateDurableConsumer(new ActiveMQTopic("aimms.aimmsmq.topic.aimms.aimms.detection.data"), "shanghaotest", null, false); 18 //注册监听事件 19 consumer.Listener += new MessageListener(consumer_Listener); 20 //ITextMessage message; 21 //while (true) 22 //{ 23 // try 24 // { 25 // message = (ITextMessage)consumer.Receive(); 26 // Console.WriteLine("Receive msg:" + message.Text); 27 // } 28 // catch (Exception ex) 29 // { 30 // Console.WriteLine("接收消息失败!" + ex); 31 // } 32 //} 33 //connection.Stop(); 34 //connection.Close(); 35 //启动连接,监听的话要主动启动连接 36 37 } 38 catch (Exception ex) 39 { 40 var a = ex; 41 } 42 #endregion 43 44 #region 方式二 45 46 ////创建连接工厂 47 //IConnectionFactory factory = new ConnectionFactory("tcp://192.168.1.20:7018", "shanghaotest"); 48 49 //try 50 //{ 51 // //通过工厂构建连接 52 // using (IConnection connection = factory.CreateConnection("admin", "roZXu8WD")) 53 // { 54 // //这个是连接的客户端名称标识 55 // connection.ClientId = "shanghaotest"; 56 // //启动连接,监听的话要主动启动连接 57 // connection.Start(); 58 // //通过连接创建一个会话 59 // using (ISession session = connection.CreateSession()) 60 // { 61 // //通过会话创建一个消费者,这里就是Queue这种会话类型的监听参数设置 62 // using (IMessageConsumer consumer = session.CreateConsumer(new ActiveMQTopic("aimms.aimmsmq.topic.aimms.aimms.detection.data"))) 63 // { 64 65 // //consumer.Listener += new MessageListener(consumer_Listener); 66 // ITextMessage message; 67 // while (true) 68 // { 69 // try 70 // { 71 // message = (ITextMessage)consumer.Receive(); 72 // Console.WriteLine("Receive msg:" + message.Text); 73 // } 74 // catch (Exception ex) 75 // { 76 // Console.WriteLine("接收消息失败!" + ex); 77 // } 78 // } 79 // } 80 81 // } 82 // } 83 //} 84 //catch (Exception ex) 85 //{ 86 // Console.WriteLine("启动接收失败!" + ex); 87 //} 88 #endregion 89 } 90 public void consumer_Listener(IMessage message) 91 { 92 ITextMessage msg = (ITextMessage)message; 93 //异步调用下,否则无法回归主线程 94 //tbReceiveMessage.Invoke(new DelegateRevMessage(RevMessage), msg); 95 96 }
标签:ActiveMQ,aimms,connection,ex,new,message,consumer From: https://www.cnblogs.com/sugarwxx/p/16843423.html