服务总线会话ReceiveBatchAsync仅接收1条消息 [英] Service Bus Session ReceiveBatchAsync only receiving 1 message

查看:57
本文介绍了服务总线会话ReceiveBatchAsync仅接收1条消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用启用了会话的服务总线队列,并且正在发送具有相同SessionId的5条消息.我的接收代码使用 AcceptMessageSessionAsync 来获取会话锁定,以便它将接收该会话的所有消息.然后,它使用 session.ReceiveBatchAsync 尝试获取该会话的所有消息.但是,它似乎只获得第一个消息,然后再进行一次尝试时,它将获得所有其他消息.您应该可以看到,即使所有这些消息都是一次发送的,两个批次之间仍然有将近一分钟的间隔:

I'm using a Service Bus queue with Sessions enabled and I'm sending 5 messages with the same SessionId. My receiving code uses AcceptMessageSessionAsync to get a session lock so that it will receive all the messages for that session. It then uses session.ReceiveBatchAsync to try and get all the messages for the session. However, it only seems to get the first message, then when another attempt is made, it gets all the others. You should be able to see that there is a gap of almost a minute between the two batches even though all these messages were sent at once:

Session started:AE8DC914-8693-4110-8BAE-244E42A302D5
Message received:AE8DC914-8693-4110-8BAE-244E42A302D5_1_08:03:03.36523
Session started:AE8DC914-8693-4110-8BAE-244E42A302D5
Message received:AE8DC914-8693-4110-8BAE-244E42A302D5_2_08:03:04.22964
Message received:AE8DC914-8693-4110-8BAE-244E42A302D5_3_08:03:04.29515
Message received:AE8DC914-8693-4110-8BAE-244E42A302D5_4_08:03:04.33959
Message received:AE8DC914-8693-4110-8BAE-244E42A302D5_5_08:03:04.39587

我处理这些的代码是WebJob中的一个函数:

My code to process these is a function in a WebJob:

[NoAutomaticTrigger]
public static async Task MessageHandlingLoop(TextWriter log, CancellationToken cancellationToken)
{
    var connectionString = ConfigurationManager.ConnectionStrings["ServiceBusListen"].ConnectionString;
    var client = QueueClient.CreateFromConnectionString(connectionString, "myqueue");

    while (!cancellationToken.IsCancellationRequested)
    {
        MessageSession session = null;

        try
        {
            session = await client.AcceptMessageSessionAsync(TimeSpan.FromMinutes(1));

            log.WriteLine("Session started:" + session.SessionId);
            foreach (var msg in await session.ReceiveBatchAsync(100, TimeSpan.FromSeconds(5)))
            {
                log.WriteLine("Message received:" + msg.MessageId);
                msg.Complete();
            }
        }
        catch (TimeoutException)
        {
            log.WriteLine("Timeout occurred");
            await Task.Delay(5000, cancellationToken);
        }
        catch (Exception ex)
        {
            log.WriteLine("Error:" + ex);
        }
    }
}

这是通过以下方式从我的WebJob Main 中调用的:

This is called from my WebJob Main using:

JobHost host = new JobHost();
host.Start();
var task = host.CallAsync(typeof(Functions).GetMethod("MessageHandlingLoop"));
task.Wait();
host.Stop();

为什么在第一次调用 ReceiveBatchAsync 时没有收到所有消息?

Why don't I get all my messages in the first call of ReceiveBatchAsync?

推荐答案

在MSDN论坛中,希拉里·卡图罗·蒙格(Hillaryary Cituiro Monge)回答了此问题:

This was answered in the MSDN forum by Hillary Caituiro Monge: https://social.msdn.microsoft.com/Forums/azure/en-US/9a84f319-7bc6-4ff8-b142-4fc1d5f1e2fa/service-bus-session-receivebatchasync-only-receiving-1-message?forum=servbus

服务总线不保证您会收到您的邮件数即使您的队列中有多个批次,也要在接收批次中指定.有这样说,您可以更改代码以尝试获取100条消息第一次致电时,请记住,您的应用程序不应假定作为保证的行为.

Service Bus does not guarantee you will receive the message count you specify in receive batch even if your queue has them or more. Having say that, you can change your code to try to get the 100 messages in the first call, buy remember that your application should not assume that as a guaranteed behavior.

以下代码行varclient =QueueClient.CreateFromConnectionString(connectionString,"myqueue");
添加client.PrefetchCount = 100;

Below this line of code varclient = QueueClient.CreateFromConnectionString(connectionString, "myqueue");
add client.PrefetchCount = 100;

在任何时候都只收到1条消息的原因第一次通话是因为当您接受会话时,它也可能是收到1条预提取的消息.然后当您收到批次时,SB客户会给您1条消息.

The reason that you are getting only 1 message at all times in the first call is due to that when you accept a session it may be also getting 1 prefetched message with it. Then when you do receive batch, the SB client will give you that 1 message.

不幸的是,我发现设置 PrefetchCount 并没有影响,但是给出的仅接收一条消息的原因似乎很可能,因此我接受了它作为答案.

Unfortunately I found that setting the PrefetchCount didn't have an affect, but the reason given for only receiving one message seemed likely so I accepted it as the answer.

这篇关于服务总线会话ReceiveBatchAsync仅接收1条消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆