使用ActiveMQ發送和接受消息(1)

第一步:搭建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 [email protected]: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 QUEUE

using 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 QUEUE

using System;
using System.Threading;
using Apache.NMS;
using Apache.NMS.ActiveMQ;
using Apache.NMS.ActiveMQ.Commands;

//消費者2
namespace 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#中的應用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章