2017-04-19 135 views
0

我使用的是當前的Apache.NMS 1.7.1和Apache.NMS.ActiveMQ 1.7.2。 我使用的是IndividualAcknowledge,所以我試圖保持加載消息的數量很低,因爲如果我有1000個消息加載而沒有加載它們,它會變得非常慢(它每次搜索所有消息的鏈表) 。NMS ActiveMQ忽略代碼中設置的預取限制

我有以下codesnippets:

BlockingCollection<IMessage> _collection = new BlockingCollection<IMessage>(); 
var factory = new ConnectionFactory("activemq:tcp://localhost:61616"); 
var _connection = (Connection) factory.CreateConnection(); 
_connection.PrefetchPolicy.All = 1000; 
var session = (Session) _connection.CreateSession(AcknowledgementMode.IndividualAcknowledge); 
var destination = SessionUtil.GetDestination(session, "queue://testQueue"); 
var messageConsumer = (MessageConsumer)session.CreateConsumer(destination); 
messageConsumer.Listener += message => _collection.Add(message); 
_connection.Start(); 

隊列testQueue包含>> 20_000消息。等待幾秒鐘後,_collection包含所有消息,而我沒有確認它們中的任何消息。

如果我明白the dokumentation的權利,我應該至多得到1000,直到我開始承認他們。

一旦券商已派出消息的預取限制數量的消費者直到消費者已經承認預取消息的至少50%,也不會派遣任何更多的消息給消費者,例如,預取/ 2,它收到。當代理收到所述確認時,它將向消費者發送進一步的預取/ 2個消息以「充值」,因爲它是預取緩衝區。

我也試着像只設置QueuePrefetchor setting the policy in the url一些變化:

activemq:tcp://localhost:61616?nms.prefetchPolicy.queuePrefetch=100 

in the queue

queue://testQueue?consumer.prefetchSize=100 

關於IndividualAcknowledge的緩慢,我已經嘗試過其他幾個選項沒有多少運氣:

messageConsumer.OptimizeAcknowledge = true; 
messageConsumer.OptimizeAcknowledgeTimeOut = 1000; 
messageConsumer.OptimizedAckScheduledAckInterval = 500; 

雖然我不完全清楚最後一個選項的差異。

回答

1

由於您使用的是異步偵聽器,因此客戶端會繼續向代理髮送每條消息發送給異步事件偵聽器的授權,從而爲代理髮送所有內容。要真正限制在任何給定時間傳遞給客戶端的消息數量,客戶端需要使用同步接收呼叫。個人確認最好與同步消費配對,以便您可以控制有多少消息被讀取,並在準備就緒時在某個時間點確認它們。

已優化的確認設置不適用於單獨的確認模式,因此對性能無幫助。