![](/img/trans.png)
[英]Azure Service Bus SubscriptionClient high latency / not receiving messages concurrently
[英]Stop receiving messages from SubscriptionClient
如何停止從訂閱客戶端接收消息作為事件驅動的消息泵? 我目前有一些代碼可以工作,但是當我連續運行兩個測試時,他們第二次休息。 我很確定消息仍然是從我創建的第一個實例中刪除訂閱。
http://msdn.microsoft.com/en-us/library/dn130336.aspx
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = true; // Indicates if the message-pump should call complete on messages after the callback has completed processing.
options.MaxConcurrentCalls = 1; // Indicates the maximum number of concurrent calls to the callback the pump should initiate
options.ExceptionReceived += LogErrors; // Enables you to be notified of any errors encountered by the message pump
// Start receiveing messages
Client.OnMessage((receivedMessage) => // Initiates the message pump and callback is invoked for each message that is received. Calling Close() on the client will stop the pump.
{
// Process the message
Trace.WriteLine("Processing", receivedMessage.SequenceNumber.ToString());
}, options);
你需要做兩件事。
首先,調用subscriptionClient.Close()
,它最終會(但不會立即)停止消息泵。
其次,在你的消息收到回調,檢查客戶端是否關閉,如下所示:
if (subscriptionClient.IsClosed)
return;
我看了看,看到完全相同的行為。 即使您關閉訂閱客戶端,消息泵也不會停止嘗試處理消息。 如果您的信息處理操作試圖做一個.Complete
或.Abandon
上的消息,它會雖然拋出一個異常,因為客戶端是封閉的。 需要一種方法來阻止泵人。
您可以調用SubscriptionClient.Close()
來阻止進一步處理消息。
代碼中的注釋也表明:
在客戶端上調用Close()將停止泵。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.