.NET下的使用ActiveMQ

项目结构

ActiveMQRecive下Program.cs

 1 using Apache.NMS;
 2 using Apache.NMS.ActiveMQ;
 3 using Apache.NMS.Util;
 4 using System;
 5 using System.Collections.Generic;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 
10 namespace ActiveMQRecive
11 {
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             try
17             {
18                 #region 消息消费者
19                 string queuesName = "myQueue";
20                 Uri _uri = new Uri(String.Concat("activemq:tcp://localhost:61616?wireFormat.maxInactivityDuration=0"));
21                 IConnectionFactory factory = new ConnectionFactory(_uri);
22                 // IConnectionFactory factory = new ConnectionFactory();
23                 using (IConnection conn = factory.CreateConnection("admin", "manager"))
24                 {
25                     using (ISession session = conn.CreateSession())
26                     {
27                         conn.Start();
28                         IDestination destination = SessionUtil.GetDestination(session, queuesName);
29                         using (IMessageConsumer consumer = session.CreateConsumer(destination))
30                         {
31                             consumer.Listener += (IMessage message) =>
32                             {
33                                 ITextMessage msg = (ITextMessage)message;
34                                 Console.WriteLine("接收消息:" + msg.Text);
35                             };
36                             // consumer.Listener += new MessageListener(consumer_Listener);
37                             Console.ReadLine();
38                         }
39                     }
40                 }
41                 #endregion
42 
43             }
44             catch (Exception ex)
45             {
46                 Console.WriteLine(ex.Message);
47             }
48         }
49 
50 
51         static void consumer_Listener(IMessage message)
52         {
53             try
54             {
55                 ITextMessage msg = (ITextMessage)message;
56                 Console.WriteLine("接收:" + msg.Text);
57 
58             }
59             catch (Exception ex)
60             {
61                 Console.WriteLine(ex.Message);
62             }
63         }
64     }
65 }
ActiveMQRecive

ActiveMQSend下Program.cs

 1 using Apache.NMS;
 2 using Apache.NMS.ActiveMQ;
 3 using Apache.NMS.Util;
 4 using System;
 5 using System.Collections.Generic;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading;
 9 using System.Threading.Tasks;
10 
11 namespace ActiveMQSend
12 {
13     class Program
14     {
15         static void Main(string[] args)
16         {
17             string queuesName = "myQueue";
18             Uri _uri = new Uri(String.Concat("activemq:tcp://localhost:61616"));
19             IConnectionFactory factory = new ConnectionFactory(_uri);
20             //IConnectionFactory factory = new ConnectionFactory();
21             using (IConnection conn = factory.CreateConnection("admin", "manager"))
22             {
23                 using (ISession session = conn.CreateSession())
24                 {
25                     IDestination destination = SessionUtil.GetDestination(session, queuesName);
26                     using (IMessageProducer producer = session.CreateProducer(destination))
27                     {
28                         conn.Start();
29                         //可以写入字符串,也可以是一个xml字符串等
30                         for (int i = 0; i < 100; i++)
31                         {
32                             ITextMessage request = session.CreateTextMessage("新新messsage" + i);
33                             producer.Send(request);
34                             Console.WriteLine("发送新新消息:" + i);
35                             Thread.Sleep(1000);
36 
37                         }
38 
39                     }
40                 }
41             }
42         }
43     }
44 }
ActiveMQSend

 

posted on 2019-03-29 18:42 陆地樵夫 阅读(...) 评论(...) 编辑 收藏

导航