gpt4 book ai didi

c# - 提供的锁无效。锁已过期,或者消息已从队列中删除

转载 作者:IT王子 更新时间:2023-10-29 04:31:02 25 4
gpt4 key购买 nike

我正在使用 Microsoft azure 服务总线队列来处理计算,并且我的程序运行良好几个小时,但随后我开始为从那时起处理的每条消息获取此异常。我不知道从哪里开始,因为前几个小时一切都运行良好。我的代码似乎也是准确的。我将发布处理 Azure 服务总线消息的方法。

public static async Task processCalculations(BrokeredMessage message)
{
try
{
if (message != null)
{
if (connection == null || !connection.IsConnected)
{
connection = await ConnectionMultiplexer.ConnectAsync("connection,SyncTimeout=10000,ConnectTimeout=10000");
//connection = ConnectionMultiplexer.Connect("connection,SyncTimeout=10000,ConnectTimeout=10000");
}

cache = connection.GetDatabase();

string sandpKey = message.Properties["sandp"].ToString();
string dateKey = message.Properties["date"].ToString();
string symbolclassKey = message.Properties["symbolclass"].ToString();
string stockdataKey = message.Properties["stockdata"].ToString();
string stockcomparedataKey = message.Properties["stockcomparedata"].ToString();

var sandpTask = cache.GetAsync<List<StockData>>(sandpKey);
var dateTask = cache.GetAsync<DateTime>(dateKey);
var symbolinfoTask = cache.GetAsync<SymbolInfo>(symbolclassKey);
var stockdataTask = cache.GetAsync<List<StockData>>(stockdataKey);
var stockcomparedataTask = cache.GetAsync<List<StockMarketCompare>>(stockcomparedataKey);

await Task.WhenAll(sandpTask, dateTask, symbolinfoTask,
stockdataTask, stockcomparedataTask);

List<StockData> sandp = sandpTask.Result;
DateTime date = dateTask.Result;
SymbolInfo symbolinfo = symbolinfoTask.Result;
List<StockData> stockdata = stockdataTask.Result;
List<StockMarketCompare> stockcomparedata = stockcomparedataTask.Result;

StockRating rating = performCalculations(symbolinfo, date, sandp, stockdata, stockcomparedata);

if (rating != null)
{
saveToTable(rating);
if (message.LockedUntilUtc.Minute <= 1)
{
await message.RenewLockAsync();
}
await message.CompleteAsync(); // getting exception here
}
else
{
Console.WriteLine("Message " + message.MessageId + " Completed!");
await message.CompleteAsync();
}
}
}
catch (TimeoutException time)
{
Console.WriteLine(time.Message);
}
catch (MessageLockLostException locks)
{
Console.WriteLine(locks.Message);
}
catch (RedisConnectionException redis)
{
Console.WriteLine("Start the redis server service!");
}
catch (MessagingCommunicationException communication)
{
Console.WriteLine(communication.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}

更新:我检查锁定到期之前的时间,如果需要,我会调用锁定更新,但它更新锁定时没有错误,但我仍然收到此异常。

timeLeft = message.LockedUntilUtc - DateTime.UtcNow;
if (timeLeft.TotalMinutes <= 2)
{
//Console.WriteLine("Renewed lock! " + ((TimeSpan)(message.LockedUntilUtc - DateTime.UtcNow)).TotalMinutes);
message.RenewLock();
}

catch (MessageLockLostException locks)
{
Console.WriteLine("Delivery Count: " + message.DeliveryCount);
Console.WriteLine("Enqueued Time: " + message.EnqueuedTimeUtc);
Console.WriteLine("Expires Time: " + message.ExpiresAtUtc);
Console.WriteLine("Locked Until Time: " + message.LockedUntilUtc);
Console.WriteLine("Scheduled Enqueue Time: " + message.ScheduledEnqueueTimeUtc);
Console.WriteLine("Current Time: " + DateTime.UtcNow);
Console.WriteLine("Time Left: " + timeLeft);
}

到目前为止,我所知道的是,我的代码运行良好一段时间,并且更新锁被调用并工作,但我仍然收到锁异常,并且在该异常中,我输出了 timeleft,它不断增加时间差,如下所示代码的运行让我相信锁过期之前的时间不会以某种方式改变?

最佳答案

我花了几个小时试图理解为什么我收到 MessageLockLostException。我的原因是由于AutoComplete默认为 true。

如果您要调用 messsage.Complete() (或 CompleteAsync()),那么您应该实例化一个 OnMessageOptions 对象,将 AutoComplete 设置为 false,并将其传递到 OnMessage 调用中。

var options = new OnMessageOptions();
options.AutoComplete = false;

client.OnMessage(processCalculations, options);

关于c# - 提供的锁无效。锁已过期,或者消息已从队列中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28127001/

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