【发布时间】:2015-01-24 15:23:24
【问题描述】:
我正在使用 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 并且它不断增加时间差代码运行让我相信直到锁定到期的时间没有以某种方式改变?
【问题讨论】:
-
你解决过这个问题吗?那是什么?
-
@FyodorSoikin 不,我最终不得不放弃并采用不同的方法来做同样的事情。据我所知,这是 api 中的一个错误,但微软没有人回复我的帖子
-
您的任何个人消息的处理时间是否可能超过 60 秒?
-
@DalSoft 我会在密集计算完成后和处理消息之前更新锁
-
@user3610374 您是否尝试过将 LockDuration 设置为 5 分钟(这是最大设置),默认为 60 秒,如果在锁定更新前超过 60 秒,则会引发此异常。
标签: c# azure distributed azureservicebus