azure - MaxAutoLockRenewalDuration 不适用于 Azure 服务总线
问题描述
我创建了下面的示例应用程序来测试“MaxAutoLockRenewalDuration”功能。
这是我的测试场景,
- 使用相同的代码创建了两个控制台应用程序。
- 在队列中添加一条消息
- 运行接收消息的控制台 App1
- 运行接收相同消息的控制台 App2。
使用下面的代码,我将 MaxAutoLockRenewalDuration 设置为 10 分钟。
根据我对“MaxAutoLockRenewalDuration”的理解,它应该自动更新锁定直到 10 分钟,并且第二个控制台应用程序不应该收到相同的消息。
public class Program
{
static string connectionString = "***";
static string queueName = "firstqueue";
static async Task Main(string[] args)
{
try
{
//uncomment below if you would like to add message to queue
//await CreateMessage(queueName, "Message 1 to test 'MaxAutoLockRenewalDuration'");
await ReceiveMessagesAsync();
}
catch (Exception ex)
{
throw;
}
Console.ReadKey();
}
private static async Task CreateMessage(string queueName, string textMessage)
{
// create a Service Bus client
await using (ServiceBusClient client = new ServiceBusClient(connectionString))
{
// create a sender for the queue
ServiceBusSender sender = client.CreateSender(queueName);
// create a message that we can send
ServiceBusMessage message = new ServiceBusMessage(textMessage);
// send the message
await sender.SendMessageAsync(message);
Console.WriteLine($"Sent a single message to the queue: {queueName}");
}
}
// handle received messages
static async Task MessageHandler(ProcessMessageEventArgs args)
{
string body = args.Message.Body.ToString();
Console.WriteLine($"Received: {body}");
System.Threading.Thread.Sleep(TimeSpan.FromMinutes(5));
// complete the message. messages is deleted from the queue.
await args.CompleteMessageAsync(args.Message);
}
// handle any errors when receiving messages
static Task ErrorHandler(ProcessErrorEventArgs args)
{
Console.WriteLine(args.Exception.ToString());
return Task.CompletedTask;
}
static async Task ReceiveMessagesAsync()
{
var processorOptions = new ServiceBusProcessorOptions
{
AutoCompleteMessages = false,
MaxConcurrentCalls = 1,
MaxAutoLockRenewalDuration = TimeSpan.FromMinutes(10),
ReceiveMode = ServiceBusReceiveMode.PeekLock,
PrefetchCount = 1
};
await using (ServiceBusClient client = new ServiceBusClient(connectionString))
{
// create a processor that we can use to process the messages
ServiceBusProcessor processor = client.CreateProcessor(queueName, processorOptions);
// add handler to process messages
processor.ProcessMessageAsync += MessageHandler;
// add handler to process any errors
processor.ProcessErrorAsync += ErrorHandler;
// start processing
await processor.StartProcessingAsync();
Console.WriteLine("Wait for a minute and then press any key to end the processing");
Console.ReadKey();
// stop processing
Console.WriteLine("\nStopping the receiver...");
await processor.StopProcessingAsync();
Console.WriteLine("Stopped receiving messages");
}
}
static async Task ReceiveMessagesAsync(string queueName)
{
await using (ServiceBusClient client = new ServiceBusClient(connectionString))
{
// create a processor that we can use to process the messages
ServiceBusProcessor processor = client.CreateProcessor(queueName, new ServiceBusProcessorOptions());
// add handler to process messages
processor.ProcessMessageAsync += MessageHandler;
// add handler to process any errors
processor.ProcessErrorAsync += ErrorHandler;
// start processing
await processor.StartProcessingAsync();
Console.WriteLine("Wait for a minute and then press any key to end the processing");
Console.ReadKey();
// stop processing
Console.WriteLine("\nStopping the receiver...");
await processor.StopProcessingAsync();
Console.WriteLine("Stopped receiving messages");
}
}
}
解决方案
推荐阅读
- google-apps-script - 是否可以将数据范围从一个电子表格复制到另一个电子表格?
- java - 创建对象将最后创建的对象的值分配给java中Arraylist中的每个其他对象
- rust - 使用 std::iter::Iterator::reduce 进行求和的正确语法是什么?
- c# - 当用户输入与字符串匹配的关键字时,将执行 Else 语句
- python - python中的Psychopy:按下键时有两个答案,释放键时有第二个答案
- ios - 使用 textField 以编程方式加载键盘并使用 swift 发送按钮
- php - 如何记录通过php销毁会话的时间
- d3.js - ObservableHQ 中的 FileAttachment - 如果在 2 个单元格或 1 个单元格内,则行为不同:TypeError:reading.map 不是函数
- r - 使用R的宽格式数据帧到长格式数据帧
- php - 错误 [RuntimeException] 无法扫描“数据库/种子”中的类