首页 > 解决方案 > MaxAutoLockRenewalDuration 不适用于 Azure 服务总线

问题描述

我创建了下面的示例应用程序来测试“MaxAutoLockRenewalDuration”功能。

这是我的测试场景,

  1. 使用相同的代码创建了两个控制台应用程序。
  2. 在队列中添加一条消息
  3. 运行接收消息的控制台 App1
  4. 运行接收相同消息的控制台 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");
        }
    }
}

标签: azureazureservicebusazure-servicebus-queues

解决方案


我将您的代码与Azure.Messaging.ServiceBus版本“7.1.1”和“7.1.2”一起使用,“MaxAutoLockRenewalDuration”功能运行良好。

这是我的步骤:

1.向队列发送消息。

2.RunConsole App1接收消息

3.Console App1继续运行,然后运行Console App2,那里没有收到任何消息。这是屏幕截图:

在此处输入图像描述

但是如果你先运行Console App1它接收到消息,然后关闭Console App1-> 然后运行Console App2,你可以在Console App2. 这是意料之中的。


推荐阅读