首页 > ActiveMQ在C#中的应用

ActiveMQ在C#中的应用

ActiveMQ是个好东东,不必多说。ActiveMQ提供多种语言支持,如Java, C, C++, C#, Ruby, Perl, Python, PHP等。由于我在windows下开发GUI,比较关心C++和C#,其中C#的ActiveMQ很简单,Apache提供NMS(.Net Messaging Service)支持.Net开发,只需如下几个步骤即能建立简单的实现。C++的应用相对麻烦些,稍后写文章介绍。

1、去ActiveMQ官方网站下载最新版的ActiveMQ,网址:http://activemq.apache.org/download.html。我之前下的是5.3.1,5.3.2现在也已经出来了。

2、去ActiveMQ官方网站下载最新版的Apache.NMS,网址:http://activemq.apache.org/nms/download.html,需要下载Apache.NMS和Apache.NMS.ActiveMQ两个bin包,如果对源码感兴趣,也可下载src包。这里要提醒一下,如果下载1.2.0版本的NMS.ActiveMQ,Apache.NMS.ActiveMQ.dll在实际使用中有个bug,即停止ActiveMQ应用时会抛WaitOne函数异常,查看src包中的源码发现是由于Apache.NMS.ActiveMQ-1.2.0-srcsrcmaincsharpTransportInactivityMonitor.cs中的如下代码造成的,修改一下源码重新编译即可。看了一下最新版1.3.0已经修复了这个bug,因此下载最新版即可。

view plaincopy to clipboardprint?

private void StopMonitorThreads()  

        {  

            lock(monitor)  

            {  

                if(monitorStarted.CompareAndSet(true, false))  

                {  

                    AutoResetEvent shutdownEvent = new AutoResetEvent(false);  

                    // Attempt to wait for the Timers to shutdown, but don't wait  

                    // forever, if they don't shutdown after two seconds, just quit.  

                    this.readCheckTimer.Dispose(shutdownEvent);  

                    shutdownEvent.WaitOne(TimeSpan.FromMilliseconds(2000));  

                    this.writeCheckTimer.Dispose(shutdownEvent);  

                    shutdownEvent.WaitOne(TimeSpan.FromMilliseconds(2000));  

                                                    //WaitOne的定义:public virtual bool WaitOne(TimeSpan timeout,bool exitContext)  

                    this.asyncTasks.Shutdown();  

                    this.asyncTasks = null;  

                    this.asyncWriteTask = null;  

                    this.asyncErrorTask = null;  

                }  

            }  

        } 

private void StopMonitorThreads()

        {

            lock(monitor)

            {

                if(monitorStarted.CompareAndSet(true, false))

                {

                    AutoResetEvent shutdownEvent = new AutoResetEvent(false);

                    // Attempt to wait for the Timers to shutdown, but don't wait

                    // forever, if they don't shutdown after two seconds, just quit.

                    this.readCheckTimer.Dispose(shutdownEvent);

                    shutdownEvent.WaitOne(TimeSpan.FromMilliseconds(2000));

                    this.writeCheckTimer.Dispose(shutdownEvent);

                    shutdownEvent.WaitOne(TimeSpan.FromMilliseconds(2000));

                                                    //WaitOne的定义:public virtual bool WaitOne(TimeSpan timeout,bool exitContext)

                    this.asyncTasks.Shutdown();

                    this.asyncTasks = null;

                    this.asyncWriteTask = null;

                    this.asyncErrorTask = null;

                }

            }

        }

3、运行ActiveMQ,找到ActiveMQ解压后的bin文件夹:...apache-activemq-5.3.1in,执行activemq.bat批处理文件即可启动ActiveMQ服务器,默认端口为61616,这可在配置文件中修改。

4、写C#程序实现ActiveMQ的简单应用。新建C#工程(一个Producter项目和一个Consumer项目),WinForm或Console程序均可,这里建的是Console工程,添加对Apache.NMS.dll和Apache.NMS.ActiveMQ.dll的引用,然后即可编写实现代码了,简单的Producer和Consumer实现代码如下:

producer:

view plaincopy to clipboardprint?

using System;  

using System.Collections.Generic;  

using System.Text;  

using Apache.NMS;  

using Apache.NMS.ActiveMQ;  

using System.IO;  

using System.Xml.Serialization;  

using System.Runtime.Serialization.Formatters.Binary;  

namespace Publish  

{  

    class Program  

    {  

        static void Main(string[] args)  

        {  

            try 

            {  

                //Create the Connection Factory  

                IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616/");  

                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("testing"));  

                        //Send Messages  

                        int i = 0;  

                        while (!Console.KeyAvailable)  

                        {  

                            ITextMessage msg = prod.CreateTextMessage();  

                            msg.Text = i.ToString();  

                            Console.WriteLine("Sending: " + i.ToString());  

                            prod.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue);  

                            System.Threading.Thread.Sleep(5000);  

                            i++;  

                        }  

                    }  

                }  

                Console.ReadLine();  

           }  

            catch (System.Exception e)  

            {  

                Console.WriteLine("{0}",e.Message);  

                Console.ReadLine();  

            }  

        }  

    }  



using System;

using System.Collections.Generic;

using System.Text;

using Apache.NMS;

using Apache.NMS.ActiveMQ;

using System.IO;

using System.Xml.Serialization;

using System.Runtime.Serialization.Formatters.Binary;

namespace Publish

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                //Create the Connection Factory

                IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616/");

                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("testing"));

                        //Send Messages

                        int i = 0;

                        while (!Console.KeyAvailable)

                        {

                            ITextMessage msg = prod.CreateTextMessage();

                            msg.Text = i.ToString();

                            Console.WriteLine("Sending: " + i.ToString());

                            prod.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue);

                            System.Threading.Thread.Sleep(5000);

                            i++;

                        }

                    }

                }

                Console.ReadLine();

           }

            catch (System.Exception e)

            {

                Console.WriteLine("{0}",e.Message);

                Console.ReadLine();

            }

        }

    }

}

consumer:

view plaincopy to clipboardprint?

using System;  

using System.Collections.Generic;  

using System.Text;  

using Apache.NMS;  

using Apache.NMS.ActiveMQ;  

using System.IO;  

using System.Xml.Serialization;  

using System.Runtime.Serialization.Formatters.Binary;  

namespace Subscribe  

{  

    class Program  

    {  

        static void Main(string[] args)  

        {  

            try 

            {  

                //Create the Connection factory  

                IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616/");  

                //Create the connection  

                using (IConnection connection = factory.CreateConnection())  

                {  

                    connection.ClientId = "testing listener";  

                    connection.Start();  

                    //Create the Session  

                    using (ISession session = connection.CreateSession())  

                    {  

                        //Create the Consumer  

                        IMessageConsumer consumer = session.CreateDurableConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("testing"), "testing listener", null, false);  

                        consumer.Listener += new MessageListener(consumer_Listener);  

                        Console.ReadLine();  

                    }  

                    connection.Stop();  

                    connection.Close();  

                }  

            }  

            catch (System.Exception e)  

            {  

                Console.WriteLine(e.Message);  

            }  

        }  

        static void consumer_Listener(IMessage message)  

        {  

            try 

            {  

                ITextMessage msg = (ITextMessage)message;  

                Console.WriteLine("Receive: " + msg.Text);  

           }  

            catch (System.Exception e)  

            {  

                Console.WriteLine(e.Message);  

            }  

        }  

    }  



using System;

using System.Collections.Generic;

using System.Text;

using Apache.NMS;

using Apache.NMS.ActiveMQ;

using System.IO;

using System.Xml.Serialization;

using System.Runtime.Serialization.Formatters.Binary;

namespace Subscribe

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                //Create the Connection factory

                IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616/");

                //Create the connection

                using (IConnection connection = factory.CreateConnection())

                {

                    connection.ClientId = "testing listener";

                    connection.Start();

                    //Create the Session

                    using (ISession session = connection.CreateSession())

                    {

                        //Create the Consumer

                        IMessageConsumer consumer = session.CreateDurableConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("testing"), "testing listener", null, false);

                        consumer.Listener += new MessageListener(consumer_Listener);

                        Console.ReadLine();

                    }

                    connection.Stop();

                    connection.Close();

                }

            }

            catch (System.Exception e)

            {

                Console.WriteLine(e.Message);

            }

        }

        static void consumer_Listener(IMessage message)

        {

            try

            {

                ITextMessage msg = (ITextMessage)message;

                Console.WriteLine("Receive: " + msg.Text);

           }

            catch (System.Exception e)

            {

                Console.WriteLine(e.Message);

            }

        }

    }

}

程序实现的功能:生产者producer建立名为testing的主题,并每隔5秒向该主题发送消息,消费者consumer订阅了testing主题,因此只要生产者发送testing主题的消息到ActiveMQ服务器,服务器就将该消息发送给订阅了testing主题的消费者。

编译生成producer.exe和consumer.exe,并执行两个exe,即可看到消息的发送与接收了。

这个例子是建的主题(Topic),ActiveMQ还支持另一种方式:Queue,即P2P,两者有什么区别呢?区别在于,Topic是广播,即如果某个Topic被多个消费者订阅,那么只要有消息到达服务器,服务器就将该消息发给全部的消费者;而Queue是点到点,即一个消息只能发给一个消费者,如果某个Queue被多个消费者订阅,没有特殊情况的话消息会一个一个地轮流发给不同的消费者,比如:

msg1-->consumer A

msg2-->consumer B

msg3-->consumer C

msg4-->consumer A

msg5-->consumer B

msg6-->consumer C

特殊情况是指:ActiveMQ支持过滤机制,即生产者可以设置消息的属性(Properties),该属性与消费者端的Selector对应,只有消费者设置的selector与消息的Properties匹配,消息才会发给该消费者。Topic和Queue都支持Selector。

Properties和Selector该如何设置呢?请看如下代码:

producer:

view plaincopy to clipboardprint?

ITextMessage msg = prod.CreateTextMessage();  

                            msg.Text = i.ToString();  

                            msg.Properties.SetString("myFilter", "test1");  

                            Console.WriteLine("Sending: " + i.ToString());  

                            prod.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue); 

ITextMessage msg = prod.CreateTextMessage();

                            msg.Text = i.ToString();

                            msg.Properties.SetString("myFilter", "test1");

                            Console.WriteLine("Sending: " + i.ToString());

                            prod.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue);

consumer:

view plaincopy to clipboardprint?

//生成consumer时通过参数设置Selector  

IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("testing"), "myFilter='test1'"); 

//生成consumer时通过参数设置Selector

IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("testing"), "myFilter='test1'");

 

本文来自CSDN博客,出处:http://blog.csdn.net/bodybo/archive/2010/06/04/5647968.aspx

转载于:https://www.cnblogs.com/guthing/archive/2010/06/17/1759333.html

更多相关:

  • IHostingEnviroment 获取环境相关洗洗 IsDevelopment()、IsStaging()、IsProduction() 分别为:开发、准生产、生产环境 IsEnviroment("Uat") 自定义环境,比如自定义Uat环境 新建: appsettings.Uat.json文件 {"Enviroment":...

  • 七. DockPanel DockPanel定义一个区域,在此区域中,您可以使子元素通过描点的形式排列,这些对象位于 Children 属性中。停靠面板其实就是在WinForm类似于Dock属性的元 素。DockPanel会对每个子元素进行排序,并停靠在面板的一侧,多个停靠在同侧的元素则按顺序排序。     如果将 LastChild...

  • 该链接有导入,导出源码,我的代码有下链接改写,完善而成的, http://www.cnblogs.com/colder/p/3611906.html using System;using System.Collections.Generic;using System.Linq;using System.Web;using System...

  • 转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 对于SharePoint中已经是Record的Item,我们想要修改他的属性,这在UI界面是无法完成的: 这时需要通过Records.BypassLocks API来完成。设计一个tool,利用Records.BypassLocks...

  • C# async await 学习笔记1(http://www.cnblogs.com/siso/p/3691059.html)  提到了ThreadId是一样的,突然想到在WinForm中,非UI线程是无法直接更新UI线程上的控件的问题。 于是做了如下测试: using System; using System.Collectio...

  • 上篇笔记中梳理了一把 resolver 和 balancer,这里顺着前面的流程走一遍入口的 ClientConn 对象。ClientConn// ClientConn represents a virtual connection to a conceptual endpoint, to // perform RPCs. // //...

  • 我的实验是基于PSPNet模型实现二维图像的语义分割,下面的代码直接从得到的h5文件开始往下做。。。 也不知道是自己的检索能力出现了问题还是咋回事,搜遍全网都没有可以直接拿来用的语义分割代码,东拼西凑,算是搞成功了。 实验平台:Windows、VS2015、Tensorflow1.8 api、Python3.6 具体的流程为:...

  • Path Tracing 懒得翻译了,相信搞图形学的人都能看得懂,2333 Path Tracing is a rendering algorithm similar to ray tracing in which rays are cast from a virtual camera and traced through a s...

  • configure_file( [COPYONLY] [ESCAPE_QUOTES] [@ONLY][NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ]) 我遇到的是 configure_file(config/config.in ${CMAKE_SOURCE_DIR}/...

  •     直接复制以下代码创建一个名为settings.xml的文件,放到C:UsersAdministrator.m2下即可