当您尝试取消已排入队列的计划 Azure 服务总线消息时会发生什么情况?

What happens when you try to cancel a scheduled Azure service bus message that has already been enqueued?

我正在尝试想出为 Azure 服务总线队列或主题安排消息的最佳方式,同时让选项保持打开状态以立即发送消息而不是计划的消息。如果我尝试在第一条消息的预定时间或之后发送替换消息,我想确保我可以防止创建重复消息。

如果我在消息已入队后尝试使用 CancelScheduledMessageAsync(对于 QueueClient 和 TopicClient 类)取消计划的消息,会发生什么情况?它会抛出异常吗?

根据您的描述,我找到了一个博客(Canceling Scheduled Messages)在谈论类似的问题。

Before version 3.3.1, a scheduled message needs to be canceled prior to becoming visible, it was not possible. And any attempt to access its value would result in InvalidOperationException. Therefore, any messages scheduled in the future and no longer needed would be "stuck" on the broker until the later time.

With Microsoft Azure Service Bus >= 3.3.1 QueueClient or TopicClient can be used to schedule a message and cancel it later.

此外,我已经通过以下代码在我这边进行了测试:

BrokeredMessage brokerMsg= new BrokeredMessage("Hello World!!!");
long sequenceNumber = await queueClient.ScheduleMessageAsync(brokerMsg, DateTimeOffset.UtcNow.AddSeconds(30));

await Task.Delay(TimeSpan.FromMinutes(1));
// Cancel scheduled message
await queueClient.CancelScheduledMessageAsync(sequenceNumber);

我登录到 Azure 门户并检查了活动消息数计划消息数。我可以在预定消息激活之前取消它,但是如果我在预定消息激活后通过 sequenceNumber 取消预定消息,那么我将检索异常如下: