Azure 服务总线 - 更新锁无法正常工作
Azure service bus - Renew lock is not working properly
有时锁不会自动更新。如果我做错了,请告诉我。
我正在使用 Microsoft.Azure.ServiceBus 库 (v3.4.0),下面是我的代码片段。
var client = new QueueClient(connectionString, queueName, ReceiveMode.PeekLock);
client.RegisterMessageHandler(async (message, token) =>
{
var logger = new AzureLogger();
logger.Debug("----------------------------------------------------------------------------");
logger.Debug("Message received.");
var body = Encoding.UTF8.GetString(message.Body);
logger.Debug($"Message : {body}");
try
{
for (int i = 1; i <= 30; i++)
{
logger.Debug(i.ToString());
logger.Debug("Lock time: " + message.SystemProperties.LockedUntilUtc.ToString("yyyy-MM-dd hh:mm:ss"));
logger.Debug("Lock token info: " + message.SystemProperties.IsLockTokenSet);
logger.Debug("Lock token: " + message.SystemProperties.LockToken);
Thread.Sleep(60000);
}
logger.Debug("Processing completed.");
await client.CompleteAsync(message.SystemProperties.LockToken);
}
catch (Exception ex)
{
logger.Error(ex.Message);
logger.Error(ex.StackTrace);
await client.DeadLetterAsync(message.SystemProperties.LockToken);
}
}, new MessageHandlerOptions(LogMessageHandlerException)
{
AutoComplete = false,
MaxAutoRenewDuration = TimeSpan.FromMinutes(120)
});
日志
锁更新不是有保证的操作。这是一个客户端 operation/request ,当它失败时,将无法扩展锁。在依赖此功能时,您必须牢记这一点。
服务总线消息上的锁只能在服务总线消息上的锁未过期之前更新。如果消息上的锁已过期,即使在触发锁更新之前也会引发异常,这将不能保证如@Sean Feldman 提到的那样更新所有消息上的锁。
If the operation performed on your locked message takes more time than the lock duration of your message you will get the Lock supplied is invalid or expired exception when you try to complete or dead-letter the message
还有一个建议是您的 MaxAutoRenewDuration 应该与消息的锁定持续时间同步。
有时锁不会自动更新。如果我做错了,请告诉我。 我正在使用 Microsoft.Azure.ServiceBus 库 (v3.4.0),下面是我的代码片段。
var client = new QueueClient(connectionString, queueName, ReceiveMode.PeekLock);
client.RegisterMessageHandler(async (message, token) =>
{
var logger = new AzureLogger();
logger.Debug("----------------------------------------------------------------------------");
logger.Debug("Message received.");
var body = Encoding.UTF8.GetString(message.Body);
logger.Debug($"Message : {body}");
try
{
for (int i = 1; i <= 30; i++)
{
logger.Debug(i.ToString());
logger.Debug("Lock time: " + message.SystemProperties.LockedUntilUtc.ToString("yyyy-MM-dd hh:mm:ss"));
logger.Debug("Lock token info: " + message.SystemProperties.IsLockTokenSet);
logger.Debug("Lock token: " + message.SystemProperties.LockToken);
Thread.Sleep(60000);
}
logger.Debug("Processing completed.");
await client.CompleteAsync(message.SystemProperties.LockToken);
}
catch (Exception ex)
{
logger.Error(ex.Message);
logger.Error(ex.StackTrace);
await client.DeadLetterAsync(message.SystemProperties.LockToken);
}
}, new MessageHandlerOptions(LogMessageHandlerException)
{
AutoComplete = false,
MaxAutoRenewDuration = TimeSpan.FromMinutes(120)
});
日志
锁更新不是有保证的操作。这是一个客户端 operation/request ,当它失败时,将无法扩展锁。在依赖此功能时,您必须牢记这一点。
服务总线消息上的锁只能在服务总线消息上的锁未过期之前更新。如果消息上的锁已过期,即使在触发锁更新之前也会引发异常,这将不能保证如@Sean Feldman 提到的那样更新所有消息上的锁。
If the operation performed on your locked message takes more time than the lock duration of your message you will get the Lock supplied is invalid or expired exception when you try to complete or dead-letter the message
还有一个建议是您的 MaxAutoRenewDuration 应该与消息的锁定持续时间同步。