首页 > 解决方案 > Azure 服务总线队列如何在 HTTPS 模式下向客户端传递消息

问题描述

我们在应用程序中为 Azure 服务总线队列使用 HTTPS 模式。

ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Https;

但我们不确定 Azure 服务总线如何在 HTTPS 模式下传递消息,如果服务总线客户端使用轮询,轮询到 Azure 服务总线队列的频率。

我们使用包:

Microsoft.ServiceBus;
Microsoft.ServiceBus.Messaging;

标签: c#azureazureservicebusazure-servicebus-queues

解决方案


我们似乎没有任何官方文档。但是,大多数服务总线客户端使用长轮询,这意味着它们打开到服务总线的连接并保持打开状态,直到它们接收到数据。如果收到消息,客户端会处理该消息并打开一个新连接。如果连接超时,客户端将在增量回退期后打开一个新连接。根据产品团队的说法,超时时间设置为 30 秒。

您可以使用此测试程序查看消息发送到队列后需要多长时间才能收到。当前设置为一次运行一条消息。通过使用批处理,总吞吐量可以比这个例子高得多。

在我的机器上,消息通常在被放入队列后 100 毫秒内被检索到。如果我将 sleepTime 设置为更大的间隔,则检索将花费稍长的时间,因此增量回退是有效的。如果音量较低,则接收消息可能需要更长的时间。

class Program
    {
        private static readonly string connectionString = "";
        private static readonly int sleepTime = 100;
       private static readonly int messageCount = 10;
        static void Main(string[] args)
        {
            ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Https;

            var client = QueueClient.CreateFromConnectionString(connectionString, "testqueue");
            client.PrefetchCount = 1;
            var timeCheck = DateTime.UtcNow;

            client.OnMessage((message) =>
            {
                var timing = (DateTime.UtcNow - message.EnqueuedTimeUtc).TotalMilliseconds;
                Console.WriteLine($"{message.GetBody<string>()}: {timing} milliseconds between between send and receipt.");
            });

            for (int i = 0; i < messageCount; i++)
            {
                client.Send(new BrokeredMessage($"Message {i}"));
                Console.WriteLine($"Message {i} sent");
                Thread.Sleep(sleepTime);
            }

            Console.WriteLine("Test Complete");

            Console.ReadLine();
        }
    }

推荐阅读