Azure Service Bus ReceiveBatch()的奇怪行为 [英] Odd Behavior of Azure Service Bus ReceiveBatch()

查看:73
本文介绍了Azure Service Bus ReceiveBatch()的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前正在使用Azure Service Bus主题,并遇到使用ReceiveBatch方法接收我的消息的问题.问题是预期结果实际上不是我得到的结果.这是基本的代码设置,使用案例如下:

Working with a Azure Service Bus Topic currently and running into an issue receiving my messages using ReceiveBatch method. The issue is that the expected results are not actually the results that I am getting. Here is the basic code setup, use cases are below:

    SubscriptionClient client = SubscriptionClient.CreateFromConnectionString(connectionString, convoTopic, subName);

    IEnumerable<BrokeredMessage> messageList = client.ReceiveBatch(100);
        foreach (BrokeredMessage message in messageList)
        {
            try
            {
                Console.WriteLine(message.GetBody<string>() + message.MessageId);

                message.Complete();
            }
            catch (Exception ex)
            {
                message.Abandon();
            }
        }

    client.Close();
    MessageBox.Show("Done");

  1. 使用上面的代码,如果我发送了4条消息,则在第一次运行时进行轮询,得到第一条消息.在第二次运行中,我得到了其他3个.我希望同时获得所有4个.它似乎总是在第一个民意测验中返回一个奇异值,然后在随后的民意测验中返回其他值. (与3和5相同的结果,在第二次尝试中我收到n条消息中的n-1条,在第一次尝试中我收到了1条消息.)

  1. Using the above code, if I send 4 messages, then poll on the first run through I get the first message. On the second run through I get the other 3. I'm expecting to get all 4 at the same time. It seems to always return a singular value on the first poll then the rest on subsequent polls. (same result with 3 and 5 where I get n-1 of n messages sent on the second try and 1 message on the first try).

如果我要接收0条消息,则该操作大约需要30-60秒才能获取messageList(计数为0).我需要它立即返回.

If I have 0 messages to receive, the operation takes between ~30-60 seconds to get the messageList (that has a 0 count). I need this to return instantly.

如果我将代码更改为IEnumerable<BrokeredMessage> messageList = client.ReceiveBatch(100, new Timespan(0,0,0));,则问题#2消失了,因为问题1仍然存在,我必须两次调用代码才能获取所有消息.

If I change the code to IEnumerable<BrokeredMessage> messageList = client.ReceiveBatch(100, new Timespan(0,0,0)); then issue #2 goes away because issue 1 still persists where I have to call the code twice to get all the messages.

我假设问题#2是由于我在#3中覆盖了默认超时值而引起的(尽管我发现这令人困惑,如果有一条消息,它会立即响应而无需等待默认时间).我不确定为什么我永远不会在单个ReceiveBatch中收到全部消息.

I'm assuming that issue #2 is because of a default timeout value which I overwrite in #3 (though I find it confusing that if a message is there it immediately responds without waiting the default time). I am not sure why I never receive the full amount of messages in a single ReceiveBatch however.

推荐答案

我让ReceiveBatch()正常工作的方法是做两件事.

The way I got ReceiveBatch() to work properly was to do two things.

  1. 在主题中禁用分区(我必须为此创建一个新主题,因为创建后无法切换该主题)
  2. 对这样创建的每个订阅启用批处理:
  3. 列表项

SubscriptionDescription sd = new SubscriptionDescription(topicName, orgSubName); sd.EnableBatchedOperations = true;

SubscriptionDescription sd = new SubscriptionDescription(topicName, orgSubName); sd.EnableBatchedOperations = true;

完成这两件事后,我可以使用IEnumerable<BrokeredMessage> messageList = client.ReceiveBatch(100, new TimeSpan(0,0,0));

After I did those two things, I was able to get the topics to work as intended using IEnumerable<BrokeredMessage> messageList = client.ReceiveBatch(100, new TimeSpan(0,0,0));

这篇关于Azure Service Bus ReceiveBatch()的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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