最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#中關(guān)于ActiveMQ的應(yīng)用示例

這篇文章主要介紹了C#中關(guān)于ActiveMQ的應(yīng)用示例,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)公司服務(wù)項目包括重慶網(wǎng)站建設(shè)、重慶網(wǎng)站制作、重慶網(wǎng)頁制作以及重慶網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,重慶網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到重慶省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

ActiveMQ是個好東東,不必多說。ActiveMQ提供多種語言支持,如Java, C, C++, C#, Ruby, Perl, Python, PHP等。由于我在windows下開發(fā)GUI,比較關(guān)心C++和C#,其中C#的ActiveMQ很簡單,Apache提供NMS(.Net Messaging Service)支持.Net開發(fā),只需如下幾個步驟即能建立簡單的實現(xiàn)。C++的應(yīng)用相對麻煩些,稍后寫文章介紹。

1、去ActiveMQ官方網(wǎng)站下載最新版的ActiveMQ,我之前下的是5.3.1,5.3.2現(xiàn)在也已經(jīng)出來了。

2、去ActiveMQ官方網(wǎng)站下載最新版的Apache.NMS,需要下載Apache.NMS和Apache.NMS.ActiveMQ兩個bin包,如果對源碼感興趣,也可下載src包。這里要提醒一下,如果下載1.2.0版本的NMS.ActiveMQ,Apache.NMS.ActiveMQ.dll在實際使用中有個bug,即停止ActiveMQ應(yīng)用時會拋WaitOne函數(shù)異常,查看src包中的源碼發(fā)現(xiàn)是由于Apache.NMS.ActiveMQ-1.2.0-src\src\main\csharp\Transport\InactivityMonitor.cs中的如下代碼造成的,修改一下源碼重新編譯即可??戳艘幌伦钚掳?.3.0已經(jīng)修復(fù)了這個bug,因此下載最新版即可。

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.1\bin,執(zhí)行activemq.bat批處理文件即可啟動ActiveMQ服務(wù)器,默認(rèn)端口為61616,這可在配置文件中修改。

4、寫C#程序?qū)崿F(xiàn)ActiveMQ的簡單應(yīng)用。新建C#工程(一個Producter項目和一個Consumer項目),WinForm或Console程序均可,這里建的是Console工程,添加對Apache.NMS.dll和Apache.NMS.ActiveMQ.dll的引用,然后即可編寫實現(xiàn)代碼了,簡單的Producer和Consumer實現(xiàn)代碼如下:

producer:

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:

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);   
            }   
        }   
    }   
}

程序?qū)崿F(xiàn)的功能:生產(chǎn)者producer建立名為testing的主題,并每隔5秒向該主題發(fā)送消息,消費者consumer訂閱了testing主題,因此只要生產(chǎn)者發(fā)送testing主題的消息到ActiveMQ服務(wù)器,服務(wù)器就將該消息發(fā)送給訂閱了testing主題的消費者。

編譯生成producer.exe和consumer.exe,并執(zhí)行兩個exe,即可看到消息的發(fā)送與接收了。

這個例子是建的主題(Topic),ActiveMQ還支持另一種方式:Queue,即P2P,兩者有什么區(qū)別呢?區(qū)別在于,Topic是廣播,即如果某個Topic被多個消費者訂閱,那么只要有消息到達服務(wù)器,服務(wù)器就將該消息發(fā)給全部的消費者;而Queue是點到點,即一個消息只能發(fā)給一個消費者,如果某個Queue被多個消費者訂閱,沒有特殊情況的話消息會一個一個地輪流發(fā)給不同的消費者,比如:

msg1-->consumer A

msg2-->consumer B

msg3-->consumer C

msg4-->consumer A

msg5-->consumer B

msg6-->consumer C

特殊情況是指:ActiveMQ支持過濾機制,即生產(chǎn)者可以設(shè)置消息的屬性(Properties),該屬性與消費者端的Selector對應(yīng),只有消費者設(shè)置的selector與消息的Properties匹配,消息才會發(fā)給該消費者。Topic和Queue都支持Selector。

Properties和Selector該如何設(shè)置呢?請看如下代碼:

producer:

public void SetProperties()
{
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:

public void SetSelector()
{
//生成consumer時通過參數(shù)設(shè)置Selector   
IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("testing"), "myFilter='test1'");  
//生成consumer時通過參數(shù)設(shè)置Selector 
IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("testing"), "myFilter='test1'");
}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C#中關(guān)于ActiveMQ的應(yīng)用示例”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!


新聞名稱:C#中關(guān)于ActiveMQ的應(yīng)用示例
本文網(wǎng)址:http://fisionsoft.com.cn/article/jdcegj.html