gpt4 book ai didi

c# - Windows 服务总线 LockDuration 属性的值

转载 作者:行者123 更新时间:2023-12-03 01:55:15 24 4
gpt4 key购买 nike

嘿,

我有一个关于 LockDuration 属性的问题。我有这个接收功能:

// Use the MessagingFactory to create a queue client for the orderqueue.
QueueClient queueClient = factory.CreateQueueClient("orderqueue");

// Receive messages from the queue with a 10 second timeout.
while (true)
{
// Receive a message using a 10 second timeout
BrokeredMessage msg = queueClient.Receive(TimeSpan.FromSeconds(10));

if (msg != null)
{
// Deserialize the message body to an order data contract.
Order order = msg.GetBody<Order>();
// Output the order.
Console.WriteLine("{0} {1} {2} {3} {4} ${5}",
order.OrderNumber,
order.Customer.FirstName,
order.Customer.LastName,
order.ShipTo.City,
order.ShipTo.Province,
order.Total);
// Update the database
try
{
// Add the order to the database.
OrdersData.AddOrder(order);
// Mark the message as complete.
msg.Complete();
}
catch (Exception ex)
{
Console.WriteLine("Exception: {0}", ex.Message);
// Something went wrong, abandon the message.
msg.Abandon();
}
}
else
{
// No message has been received, we will poll for more messages.
Console.WriteLine("Polling, polling, polling...");
}
}

如果我收到如上例所示的消息。如果一切正常,我会使用 Complete() 函数删除该消息。如果出现问题,我会调用 Abondon() 函数,以便消息被解锁。所以我的问题是:

当我使用 peeklock 接收模式时,有 QueueDescription.LockDuration 属性和 SubscriptionDescription.LockDuration 属性来设置消息的锁定持续时间。您可以将其更改为 5 分钟。我在某处读到您应该仔细设置此探针的值。为什么我不应该将其设置为 5 分钟,因为如果 abandom() 函数出现错误,消息无论如何都会被解锁(请参阅代码示例中的 catch block )。

致以诚挚的问候

最佳答案

决定锁定持续时间的主要考虑因素是:

1) 如果失败,您的读取会延迟多长时间?

2)处理一条消息需要多长时间?

假设您将锁定持续时间设置为 5 分钟,然后锁定一条消息并且您的处理器死机。这意味着该消息将在 5 分钟后发送给下一个接收者。如果没有失败并且您完成甚至放弃了该消息,那么该消息将立即可用。

假设您主要需要 1 分钟来处理一条消息,您可以将锁定持续时间设置为 2 分钟,并且不必更新锁定,但如果您需要 10 分钟来处理,则需要适本地调用 RenewLock。因此,如果您不太关心第一种情况(失败时的延迟)并且希望避免更新锁,而您的消息处理始终可以在 5 分钟内完成,那么始终选择 5 分钟就可以了。

关于c# - Windows 服务总线 LockDuration 属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23594302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com