C#使用ActiveMQ實例

1. ActiveMQ消息總線簡介

消息隊列(Message Queue,簡稱MQ),從字面意思上看,本質是個隊列,FIFO先入先出,只不過隊列中存放的內容是message而已。主要用做不一樣進程、應用間的通訊方式。php

常見的消息隊列有rabbitMQ、activeMQ、zeroMQ、Kafka、Redis 比較 。html

其中ActiveMQ是Apache出品的一款開源消息總線,支持多種語言和協議編寫客戶端。語言: Java,C,C++,C#,Ruby,Perl,Python,PHP。應用協議: OpenWire,Stomp REST,WS Notification,XMPP,AMQPjava

ActiveMQ主要有兩種消息分發方式:Queue和Topic。mysql

  Queue相似編程語言中的Queue,每條消息只會被一個消費者接收;web

  Topic相似廣播,發送的消息會被多個消費者接受,前提是訂閱了該主題的消息。spring

2. ActiveMQ安裝 

2.1. 下載ActiveMQ sql

官方網站下載地址:http://activemq.apache.org/ 數據庫

2.2. 運行ActiveMQ apache

解壓縮apache-activemq-5.10.0-bin.zip,而後雙擊apache-activemq-5.10.0\bin\win32\activemq.bat運行ActiveMQ程序。 編程

看見控制檯最後一行輸出: 「access to all MBeans is allowed」 證實啓動成功。 

啓動ActiveMQ之後,可使用瀏覽器登錄:http://localhost:8161/admin/驗證, 默認用戶名是:admin  密碼是:admin 

(前提是安裝好Java環境) 

同時下載.net版Dll:Apache.NMS-1.7.0-bin.zip和Apache.NMS.ActiveMQ-1.7.0-bin.zip 

都從這裏下載:http://archive.apache.org/dist/activemq/apache-nms/1.7.0/ 

3. ActiveMQ Queue

在ActiveMQ中Queue是一種點對點的消息分發方式,生產者在隊列中添加一條消息,而後消費者消費一條消息,這條消息保證送達而且只會被一個消費者接收

這裏使用Winform編寫程序,其中須要添加兩個dll,都在Apache.NMS-1.7.0-bin.zip和Apache.NMS.ActiveMQ-1.7.0-bin.zip中。 

// 生產者
// 須要添加一個label, button, textbox 
public Form1()
        {
            InitializeComponent();
            InitProducer();
        }
        private IConnectionFactory factory;

        public void InitProducer()
        {
            try
            {
                //初始化工廠,這裏默認的URL是不須要修改的
                factory = new  ConnectionFactory("tcp://localhost:61616");

            }
            catch
            {
                lbMessage.Text = "初始化失敗!!";
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //經過工廠創建鏈接
            using (IConnection connection = factory.CreateConnection())
            {
                //經過鏈接建立Session會話
                using (ISession session = connection.CreateSession())
                {
                    //經過會話建立生產者,方法裏面new出來的是MQ中的Queue
                    IMessageProducer prod = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("firstQueue"));
                    //建立一個發送的消息對象
                    ITextMessage message = prod.CreateTextMessage();
                    //給這個對象賦實際的消息
                    message.Text = txtMessage.Text;
                    //設置消息對象的屬性,這個很重要哦,是Queue的過濾條件,也是P2P消息的惟一指定屬性
                    message.Properties.SetString("filter","demo");
                    //生產者把消息發送出去,幾個枚舉參數MsgDeliveryMode是否長鏈,MsgPriority消息優先級別,發送最小單位,固然還有其餘重載
                    prod.Send(message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue);
                    lbMessage.Text = "發送成功!!";
                    txtMessage.Text = "";
                    txtMessage.Focus();
                }
            }
        }

 

    // 消費者
     public Form1()
        {
            InitializeComponent();
            InitConsumer();

        }
         public void InitConsumer()
         {
             //建立鏈接工廠
             IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616");
             //經過工廠構建鏈接
             IConnection connection = factory.CreateConnection();
             //這個是鏈接的客戶端名稱標識
             connection.ClientId = "firstQueueListener";
             //啓動鏈接,監聽的話要主動啓動鏈接
             connection.Start();
             //經過鏈接建立一個會話
             ISession session = connection.CreateSession();
             //經過會話建立一個消費者,這裏就是Queue這種會話類型的監聽參數設置
             IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("firstQueue"), "filter='demo'");
             //註冊監聽事件
             consumer.Listener += new MessageListener(consumer_Listener);
             //connection.Stop();
             //connection.Close();  
 
         }
 
         void consumer_Listener(IMessage message)
         {
             ITextMessage msg = (ITextMessage)message;
             //異步調用下,不然沒法迴歸主線程
             tbReceiveMessage.Invoke(new DelegateRevMessage(RevMessage),msg);
 
         }
 
         public delegate void DelegateRevMessage(ITextMessage message);
 
         public void RevMessage(ITextMessage message)
         {
             tbReceiveMessage.Text += string.Format(@"接收到:{0}{1}", message.Text, Environment.NewLine);
         }

咱們能夠到管理平臺 http://localhost:8161 中查看對應的Queue,生產者產生消息,消費者接收後會刪掉消息。

新建項目,更改 connection.ClientId 後能夠啓動多個消費者,能夠發現每一個消費者都有機會接收消息,測試的時候是每一個消費者輪流接收一條消息,有興趣的能夠本身看一下接收規律。 

4. ActiveMQ Topic

Topic和Queue相似,不過生產者發送的消息會被多個消費者接收,保證每一個訂閱的消費者都會接收到消息。

在管理平臺能夠看到每條Topic消息有兩個記錄值,一個是訂閱的消費者數量,一個是已經接收的消費者數量。

 

//生產者
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();
}

 

//消費者
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 listener1";  
                    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 listener1", 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);  
            }  
        }

新建項目,更改connection.ClientId後能夠啓動多個消費者,能夠發現每一個消費者都會接收到消息,訂閱一次後即便下線了,上線以後也會收到消息。

 

5. ActiveMQ持久化消息

ActiveMQ的另外一個問題就是隻要是軟件就有可能掛掉,掛掉不可怕,怕的是掛掉以後把信息給丟了,因此本節分析一下幾種持久化方式:

5.1 持久化爲文件

ActiveMQ默認就支持這種方式,只要在發消息時設置消息爲持久化就能夠了。

打開安裝目錄下的配置文件:

D:\ActiveMQ\apache-activemq\conf\activemq.xml在越80行會發現默認的配置項:

<persistenceAdapter>

     <kahaDB directory="${activemq.data}/kahadb"/>

</persistenceAdapter>

注意這裏使用的是kahaDB,是一個基於文件支持事務的消息存儲器,是一個可靠,高性能,可擴展的消息存儲器。

     他的設計初衷就是使用簡單並儘量的快。KahaDB的索引使用一個transaction log,而且全部的destination只使用一個index,有人測試代表:若是用於生產環境,支持1萬個active connection,每一個connection有一個獨立的queue。該表現已經足矣應付大部分的需求。

而後再發送消息的時候改變第二個參數爲:

MsgDeliveryMode.Persistent

Message保存方式有2種
PERSISTENT:保存到磁盤,consumer消費以後,message被刪除。
NON_PERSISTENT:保存到內存,消費以後message被清除。
注意:堆積的消息太多可能致使內存溢出。

而後打開生產者端發送一個消息:

 

 

不啓動消費者端,同時在管理界面查看:

 

 

發現有一個消息正在等待,這時若是沒有持久化,ActiveMQ宕機後重啓這個消息就是丟失,而咱們如今修改成文件持久化,重啓ActiveMQ後消費者仍然可以收到這個消息。

 

 

2、持久化爲數據庫

咱們從支持Mysql爲例,先從http://dev.mysql.com/downloads/connector/j/下載mysql-connector-java-5.1.34-bin.jar包放到:

D:\ActiveMQ\apache-activemq\lib目錄下。

打開並修改配置文件:

 

<beans
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
 
    <!-- Allows us to use system properties as variables in this configuration file -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>file:${activemq.conf}/credentials.properties</value>
        </property>
    </bean>
 
   <!-- Allows accessing the server log -->
    <bean id="logQuery" class="org.fusesource.insight.log.log4j.Log4jLogQuery"
          lazy-init="false" scope="singleton"
          init-method="start" destroy-method="stop">
    </bean>
 
    <!--
        The <broker> element is used to configure the ActiveMQ broker.
    -->
    <broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}">
 
        <destinationPolicy>
            <policyMap>
              <policyEntries>
                <policyEntry topic=">" >
                    <!-- The constantPendingMessageLimitStrategy is used to prevent
                         slow topic consumers to block producers and affect other consumers
                         by limiting the number of messages that are retained
                         For more information, see:
 
                         http://activemq.apache.org/slow-consumer-handling.html
 
                    -->
                  <pendingMessageLimitStrategy>
                    <constantPendingMessageLimitStrategy limit="1000"/>
                  </pendingMessageLimitStrategy>
                </policyEntry>
              </policyEntries>
            </policyMap>
        </destinationPolicy>
 
 
        <!--
            The managementContext is used to configure how ActiveMQ is exposed in
            JMX. By default, ActiveMQ uses the MBean server that is started by
            the JVM. For more information, see:
 
            http://activemq.apache.org/jmx.html
        -->
        <managementContext>
            <managementContext createConnector="false"/>
        </managementContext>
 
        <!--
            Configure message persistence for the broker. The default persistence
            mechanism is the KahaDB store (identified by the kahaDB tag).
            For more information, see:
 
            http://activemq.apache.org/persistence.html
            <kahaDB directory="${activemq.data}/kahadb"/>
        -->
        <persistenceAdapter>
            <jdbcPersistenceAdapter dataDirectory="${activemq.base}/data" dataSource="#derby-ds"/>
        </persistenceAdapter>
 
 
          <!--
            The systemUsage controls the maximum amount of space the broker will
            use before disabling caching and/or slowing down producers. For more information, see:
            http://activemq.apache.org/producer-flow-control.html
          -->
          <systemUsage>
            <systemUsage>
                <memoryUsage>
                    <memoryUsage percentOfJvmHeap="70" />
                </memoryUsage>
                <storeUsage>
                    <storeUsage limit="100 gb"/>
                </storeUsage>
                <tempUsage>
                    <tempUsage limit="50 gb"/>
                </tempUsage>
            </systemUsage>
        </systemUsage>
 
        <!--
            The transport connectors expose ActiveMQ over a given protocol to
            clients and other brokers. For more information, see:
 
            http://activemq.apache.org/configuring-transports.html
        -->
        <transportConnectors>
            <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
            <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
        </transportConnectors>
 
        <!-- destroy the spring context on shutdown to stop jetty -->
        <shutdownHooks>
            <bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
        </shutdownHooks>
 
    </broker>
  <bean id="derby-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost/activemq?relaxAutoCommit=true"/>
    <property name="username" value="root"/>
    <property name="password" value=""/>
    <property name="maxActive" value="200"/>
    <property name="poolPreparedStatements" value="true"/>
  </bean>
    <!--
        Enable web consoles, REST and Ajax APIs and demos
        The web consoles requires by default login, you can disable this in the jetty.xml file
 
        Take a look at ${ACTIVEMQ_HOME}/conf/jetty.xml for more details
    -->
    <import resource="jetty.xml"/>
 
</beans>
<!-- END SNIPPET: example -->

 

重啓ActiveMQ打開phpmyadmin發現多了3張表: 

 

而後啓動生產者(不啓動消費者) 

在Mysql中能夠找到這條消息: 

 

 

關掉ActiveMQ並重啓,模擬宕機。

而後啓動消費者:

 

 

而後發現Mysql中已經沒有這條消息了。

相關文章
相關標籤/搜索