2014-01-10 93 views
0

方案:我發送五萬消息到名爲JUST.CN的隊列。然後每消息1000個消息,我會創建一個消息propertyString「myfilter ='abc'」。現在,我使用相同的選擇器創建消費者以使用消息。但是,在30000消息之後,特別是消費速率非常慢。我無法更改activeMQ中的默認配置。 核心代碼如下:activeMQ上消費者的選擇問題

IDestination destination = SessionUtil.GetDestination(session, "JUST.CN"); 
        IMessageProducer producer = session.CreateProducer(destination); 
        string msg = "Hello hello hello world!~~testing~Hello hello hello world!~~testing~"; 

        for (int i = 0; i < 50000; i++) 
        { 
         ITextMessage message; 

         if (i % 1000 == 0) 
         { 
          message = session.CreateTextMessage(msg); 
          message.Properties.SetString("myfilter", "abc"); 
         } 
         else 
         { 
          message = session.CreateTextMessage(msg); 
         } 
         producer.Send(message, MsgDeliveryMode.Persistent, MsgPriority.Normal, TimeSpan.MinValue); 
        } 

消費者的代碼:

IDestination destination = SessionUtil.GetDestination(session, "JUST.CN");    
IMessageConsumer consumer = session.CreateConsumer(destination, "myfilter='abc'", false); 

        int count = 0; 
        DateTime dtstart = DateTime.Now; 
       for (int i = 0; i < 50; i++) 
        { 
         IMessage iMsg = consumer.Receive(); 
         ITextMessage msg = (ITextMessage)iMsg; 
         Console.WriteLine(msg.Text); 
         count++; 

        } 
        DateTime dtend = DateTime.Now; 
        TimeSpan time = dtend - dtstart; 
        Console.WriteLine(time); 
        Console.WriteLine(count); 

是否有我需要使用的選擇到ActiveMQ的任何特殊的設置? 預先感謝您的任何意見。

回答

1

一般來說,在隊列中使用消息選擇器是一種反模式。有一篇很好的文章,說明爲什麼這是在幾年前的博客上發佈的Ade on Middleware

如果您正在尋找在使用上隊列消息選擇,使用情況總體上是某些消費者對某些產品感興趣的消息。您可以通過使用composite destinations上應用過濾器(通過filteredDestination)的經紀人配置好得多的辦法解決這個用例是選擇邏輯相當於:

<broker xmlns="http://activemq.apache.org/schema/core"> 
    <destinationInterceptors> 
    <virtualDestinationInterceptor> 
     <virtualDestinations> 
     <compositeQueue name="myapp.in" forwardOnly="true"> 
      <forwardTo> 
      <filteredDestination selector="myHeader > 5" queue="myapp.out.high"/> 
      <filteredDestination selector="myHeader <= 5" queue="myapp.out.low"/> 
      </forwardTo> 
     </compositeQueue> 
     </virtualDestinations> 
    </virtualDestinationInterceptor> 
    </destinationInterceptors> 
</broker> 

這裏會發生什麼事是,當郵件到達myapp.in隊列時,會針對郵件運行SQL92篩選器,並對郵件進行適當排序。想要僅消費高消息的用戶訂閱myapp.out.high

通過這樣做,你都有效地將倒掛問題和使用消息時消除對複雜處理的需求。

+0

謝謝Jake.Your答案幫助我lot.Creating虛擬目的地和發送消息there.This會導致大量queue.Is有任何其他方式做到這一點有一個隊列?也許這是爲每個消費者創建一個隊列的唯一方法,如果發送消息給特定的消費者。該系統將有1000個消費者到PTP通信。我無法想象在ActiveMQ服務器上會發生什麼。 – Yolanda

+0

一般而言,經紀人擁有大量目的地即可。聽起來你可能會使用一組錯誤的功能(隊列可能不是解決特定問題的最佳方法)。如果你能說出你想要達到的目標,我可能會指出你正確的方向。 –

+0

我們的系統中將有大約1000個用戶。我想實現彼此之間的點對點通信(也許可能有300個用戶同時在線),並且消息將被持久存儲在activemq中代理用戶A發送一條消息給用戶B. userB會在線接收消息,一個用戶可能有三種類型的端點,比如WebPage,Instant Communication Tool和Mobile等。這意味着一條消息會被髮送到所有使用的客戶端。有沒有什麼好的方法來實現這一點?謝謝傑克。 – Yolanda