簡體   English   中英

如何按時間間隔有選擇地接收 Azure 服務總線消息?

[英]How to selectively receive Azure Service Bus messages by time interval?

我一直在開發一個 Azure 服務總線庫,它抽象和簡化了Microsoft.Azure.ServiceBus .NET API 的重要部分。

以下代碼提供了使用該庫的訂閱操作示例:

/// <summary>
/// Subscribes string types events.
/// </summary>
/// <param name="eventBus">The event bus.</param>
private static void SubscribeStringEvents(IEventBus eventBus)
{
    IEventBusEventHandler<string> stringEventHandler = new StringEventHandler();

    eventBus.Subscribe("topicId", stringEventHandler);
}

目前,當客戶端訂閱指定主題的string類型時,底層的ISubscriptionClient將自動連接到相應的主題string訂閱。 一旦建立連接,所有存儲在string subscription 中的消息都會被注冊的stringEventHandler接收。 stringEventHandler也將接收進一步發布的string消息。 考慮的唯一時間限制是消息不應過期(因此,應移至死信隊列)。

但是,在訂閱string類型時,我還希望支持僅接收在stringEventHandler注冊后發布的消息的可能性 - 過去的消息應保留存儲在相應的string訂閱中。

支持這兩種行為的最佳方法是什么? 不幸的是, Microsoft.Azure.ServiceBus .NET API 的文檔很少。

查找來自 Microsoft 的示例: TopicFilters

它使用舊的WindowsAzure.ServiceBus (4.1.3)庫。 它的使用方式與當前版本略有不同。 不過你可以拿is作為參考。

1.創建過濾器

SqlFilter sqlfilter = new SqlFilter("sys.EnqueuedTimeUtc >= @datetime");
sqlfilter.Parameters.Add("@datetime", DateTime.Parse("2019-12-04 16:56"));
Filter filter = sqlfilter.Preprocess();

2. 獲取過濾消息

var messages = subscriptionClient.PeekBatch(100);
Console.WriteLine("filtered messages: ");
var filteredMessages = from x in messages where filter.Match(x) select x;
var filteredMessagesList = filteredMessages.ToList();

補充教程:

Azure 服務總線篩選器

Azure ServiceBus:使用日期時間過濾器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM