c# - 我可以使用重复检测来取消计划的 Azure 服务总线消息吗?

标签 c# azure-servicebus-queues azure-servicebus-topics

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

如果您打开重复检测并且两条消息具有相同的 MessageId,那么在第一条消息的预定入队时间之前发送第二条消息是否会有效地取消预定消息?此外,假设如果计划的消息首先成功排队,第二条消息将被忽略是否安全?

最佳答案

If you turn on duplicate detection and both messages have the same MessageId, does sending the second message prior to the scheduled enqueueing time of the first effectively cancel the scheduled message?

我用下面的示例代码做了一个测试,发现第二条消息会被忽略。

var client = QueueClient.CreateFromConnectionString(connectionString, queueName);

for (int i = 0; i < 2; i++)
{
    if (i == 0)
    {
        client.SendAsync(new BrokeredMessage("testmes1") { MessageId = "test1", ScheduledEnqueueTimeUtc = DateTime.UtcNow.AddSeconds(60) }).Wait();
    }
    else
    {
        client.SendAsync(new BrokeredMessage("testmes2") { MessageId = "test1" }).Wait();
    }

} 

从队列中接收消息:

client.OnMessage(message =>
{
    Console.WriteLine(String.Format("Message body: {0}", message.GetBody<String>()));
    Console.WriteLine(String.Format("Message id: {0}", message.MessageId));
});

输出:

enter image description here

如果您想取消预定的消息,您可以尝试调用 CancelScheduledMessageAsync() method在该预定消息变为事件之前。

client.CancelScheduledMessageAsync(sequenceNumber).Wait();

关于c# - 我可以使用重复检测来取消计划的 Azure 服务总线消息吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45384028/

相关文章:

c# - 创建 xml 文档时的默认命名空间

azure - 使用Azure服务总线作为备份消息服务是一个好的模式吗?

c# - 如何每 X 秒批量处理来自服务总线队列的消息

azure - 使用 Azure 服务总线对队列和主题/订阅进行消息排序传送

c# - 如何将字符/字符串读取为十六进制?

javascript - 为什么我通过 AJAX 传递到 MVC Controller 的 XML 无法到达那里?

c# - 我怎样才能创建一个 WPF 样式,以便所有图像控件上都有一个 MouseDown(click) 事件?

visual-studio-2017 - Azure 服务总线触发器队列未在本地调试中命中代码

c# - Azure 服务总线主题架构

Azure 服务总线主题接收消息