使用ActiveMQ发送和接受消息(1)
来源:互联网 发布:网络红歌排行榜 编辑:程序博客网 时间:2024/05/14 04:34
第一步:搭建ActiveMQ环境
a.安装jdk与tomcat,设置环境变量%JAVVA_HOME% = C:\Program Files\Java\jdk1.7.0_11
b.解压下载得到的apache-activemq-5.7.0-bin.zip,输出路径:E:\ActiveMQ\apache-activemq-5.7.0
下载地址:http://activemq.apache.org/download-archives.html
c.修改E:\ActiveMQ\apache-activemq-5.7.0\conf\activemq.xml
Line129: <storeUsage limit="10 gb"/>
Line132: <tempUsage limit="1 gb"/>
若磁盘空间不够,在安装过程中会得到WARN信息。
d.双击运行apache-activemq-5.7.0\bin\activemq.bat,一切顺利得信息“INFO | Started SelectChannelConnector@0.0.0.0:8161”
打开http://localhost:8161/,效果如下:
第二步:使用C#实现本地和ActiveMQ之间发送/接受消息
a.下载Apache.NMS.ActiveMQ.dll和Apache.NMS.dll
下载地址:http://activemq.apache.org/nms/download.html
Apache.NMS.ActiveMQ-1.5.6-bin\build\net-2.0\release\Apache.NMS.ActiveMQ.dll
Apache.NMS-1.5.1-bin\net-2.0\release\Apache.NMS.dll
b.新建C#项目,添加引用Apache.NMS.ActiveMQ.dll和Apache.NMS.dll
c.编写生产者类,实现向服务器发送消息
//#define TOPIC //使用topic模式#define QUEUE // 使用queue模式using System;using System.Threading;using Apache.NMS;using Apache.NMS.ActiveMQ;using Apache.NMS.ActiveMQ.Commands;//生产者namespace Producer{ class Program { static void Main(string[] args) { try { IConnectionFactory pConnFactory = new ConnectionFactory("tcp://localhost:61616"); using (IConnection pConn = pConnFactory.CreateConnection()) { using (ISession pSession = pConn.CreateSession()) {#if TOPIC IDestination pDestination = new ActiveMQTopic("myTopicName");#endif#if QUEUE IDestination pDestination = new ActiveMQQueue("myQueryName"); #endif IMessageProducer pProducer = pSession.CreateProducer(pDestination); int i = 0; while (!Console.KeyAvailable) { ITextMessage msg = pProducer.CreateTextMessage(); msg.Text = i.ToString(); msg.Properties.SetInt("ITEM_TYPE", i); // 为消息添加属性 Console.WriteLine(string.Format("Sending: {0}", i)); pProducer.Send(msg, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue); Thread.Sleep(2000); i++; } } } Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.Read(); } } }}
d.编写消费者类,实现从服务器接受消息
//#define TOPIC#define QUEUEusing System;using System.Threading;using Apache.NMS;using Apache.NMS.ActiveMQ;using Apache.NMS.ActiveMQ.Commands;//消费者namespace Consumer{ class Program { static void Main(string[] args) { try { IConnectionFactory pFactory = new ConnectionFactory("tcp://localhost:61616/"); using (IConnection conn = pFactory.CreateConnection()) { conn.ClientId = "myListenser"; conn.Start(); using (ISession pSession = conn.CreateSession()) {#if TOPIC ITopic pTopic = new ActiveMQTopic("myTopicName"); IMessageConsumer pConsumer = pSession.CreateDurableConsumer(pTopic, "myListenser", null, false);#endif#if QUEUE IDestination pDestination = new ActiveMQQueue("myQueryName"); //IMessageConsumer pConsumer = pSession.CreateConsumer(pDestination, "ITEM_TYPE%3=0"); IMessageConsumer pConsumer = pSession.CreateConsumer(pDestination);#endif pConsumer.Listener += new MessageListener(consumer_Listener); Console.ReadLine(); } conn.Stop(); conn.Close(); } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadKey(); } } static void consumer_Listener(IMessage message) { try { ITextMessage msg = message as ITextMessage; Thread.Sleep(3000); Console.WriteLine(string.Format("Receive 3s ago: {0}", msg.Text)); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadKey(); } } }}
//#define TOPIC#define QUEUEusing System;using System.Threading;using Apache.NMS;using Apache.NMS.ActiveMQ;using Apache.NMS.ActiveMQ.Commands;//消费者2namespace Consumer2{ class Program { static void Main(string[] args) { try { IConnectionFactory pFactory = new ConnectionFactory("tcp://localhost:61616/"); using (IConnection conn = pFactory.CreateConnection()) { conn.ClientId = "myListenser2"; conn.Start(); using (ISession pSession = conn.CreateSession()) {#if TOPIC ITopic pTopic = new ActiveMQTopic("myTopicName"); IMessageConsumer pConsumer = pSession.CreateDurableConsumer(pTopic, "myListenser2", null, false);#endif#if QUEUE IDestination pDestination = new ActiveMQQueue("myQueryName"); IMessageConsumer pConsumer = pSession.CreateConsumer(pDestination);#endif pConsumer.Listener += new MessageListener(consumer_Listener); Console.ReadLine(); } conn.Stop(); conn.Close(); } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadKey(); } } static void consumer_Listener(IMessage message) { try { ITextMessage msg = message as ITextMessage; Console.WriteLine(string.Format("Receive: {0}", msg.Text)); Thread.Sleep(3000); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadKey(); } } }}
e.效果如下
参考资料:
ActiveMQ在C#中的应用
- 使用ActiveMQ发送和接受消息(1)
- 如何使用activemq jms发送和接受消息
- ActiveMQ (二) 使用Queue或者Topic发送/接受消息
- ActiveMQ 使用Queue或者Topic发送/接受消息
- ActiveMQ发送消息,接受消息。点对点连接
- activemq能发送消息但是接受不了消息的解决方法
- activemq使用JMS发送消息和接收消息
- ActiveMQ使用笔记(三)ActiveMQ消息发送与接收
- ActiveMQ使用笔记(三)ActiveMQ消息发送与接收
- weblogic中使用jms发送和接受消息
- 使用ActiveMQ 发送/接收消息
- 使用JMS发送消息--ActiveMQ
- ActiveMQ消息发送和接收
- ActiveMQ消息发送和接收
- ActiveMQ消息发送和接收
- kafka消息发送和消息接受
- JMS_使用ActiveMQ实现消息的发送和接收
- ActiveMQ 发送消息和接收消息 Demo
- 智能家居地址
- hadoop学习笔记之-使用bind配置DNS实验
- rsync传输性能测试总结
- socket通信框架mina使用详解(一)
- jquery 操作下拉列表 获取值与显示值
- 使用ActiveMQ发送和接受消息(1)
- mono for Android配置环境和listview简单显示数据(1)
- 关于C语言指针
- mysql null排序
- PHP与MYSQL排序的效率比对
- ie6下iframe出现水平滚动条的解决办法
- 学习linux的步骤
- jsp 分页类的实现
- [源码分享]NPOI读取Excel到 dataTable