简体   繁体   English

延迟消息并保持传递计数 Azure 服务总线

[英]Delay Message and Maintain Delivery Count Azure Service Bus

We have a scenario when we pull message from Azure Service Bus Queue and for some reason if one of the down stream is down than we would like to delay a message and put back to queue.当我们从 Azure 服务总线队列中提取消息时,我们有一个场景,并且由于某种原因,如果向下 stream 之一关闭,而不是我们希望延迟消息并放回队列。 I understand we can do through multiple ways(Set the property ScheduledEnqueueTime or use Schedule API)but either way we will have to create a new message and put back to queue which will lose the delivery count and also can result in an issue where we have duplicate message where sending the clone and completing the original are not an atomic operation and one of them fails.我知道我们可以通过多种方式(设置属性 ScheduledEnqueueTime 或使用 Schedule API)但无论哪种方式,我们都必须创建一条新消息并放回队列,这将丢失传递计数,也可能导致我们遇到的问题重复消息,其中发送克隆和完成原始消息不是原子操作,其中一个失败。

https://www.markheath.net/post/defer-processing-azure-service-bus-message https://www.markheath.net/post/defer-processing-azure-service-bus-message

based on the above article only way seems to be have our custom property, Is that the only way still as this article was written in 2016.根据上面的文章,唯一的方法似乎是拥有我们的自定义属性,这仍然是唯一的方法,因为这篇文章是在 2016 年写的。

Scheduling a new message back does not increase the delivery count.重新安排新邮件不会增加传递计数。 And as you said, sending a message and completing a message are not atomic, it can be atomic with the help of transactions , thereby ensuring that all operations belonging to a given group of operations either succeed or fail jointly.正如您所说,发送消息和完成消息不是原子的,它可以在事务的帮助下是原子的,从而确保属于给定操作组的所有操作共同成功或失败。

Here's an example:这是一个例子:

ServiceBusClient client = new ServiceBusClient("<connection-string>");
ServiceBusReceiver serviceBusReceiver = client.CreateReceiver("<queue>");
ServiceBusSender serviceBusSender = client.CreateSender("<queue>");

var message = await serviceBusReceiver.ReceiveMessageAsync();
// Your condition to handle the down stream
if (true)
{
    using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
    {
        await serviceBusReceiver.CompleteMessageAsync(message);
        var newMessage = new ServiceBusMessage(message);
        newMessage.ScheduledEnqueueTime = new DateTimeOffset(DateTime.UtcNow.AddMinutes(1));
        await serviceBusSender.SendMessageAsync(newMessage);
        ts.Complete();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM