gpt4 book ai didi

c# - Azure 服务总线客户端连接持久性

转载 作者:可可西里 更新时间:2023-11-01 08:44:19 24 4
gpt4 key购买 nike

下面是我们将在辅助角色中使用的 Azure 服务总线代码的基本包装。这个 ServiceBusClient 将在每次运行辅助角色时实例化;然后用于访问队列,直到没有剩余的项目可供枚举。

public class ServiceBusClient : IDisposable, IServiceBusClient
{
private const int DEFAULT_WAIT_TIME_IN_SECONDS = 120;

private const string SERVICE_BUS_CONNECTION_STRING_KEY = "service.bus.connection.string";

private readonly MessagingFactory _messagingFactory;

private readonly NamespaceManager _namespaceManager;

private readonly QueueClient _queueClient;

private readonly ISettingsManager _settingsManager;

public ServiceBusClient(ISettingsManager settingsManager, string queueName)
{
_settingsManager = settingsManager;

var connectionString = _settingsManager.GetSetting<string>(SERVICE_BUS_CONNECTION_STRING_KEY);

_namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
_messagingFactory = MessagingFactory.CreateFromConnectionString(connectionString);

_queueClient = GetOrCreateQueue(queueName);
}

public void Dispose()
{
_messagingFactory.Close();
}

public BrokeredMessage ReceiveTopMessage()
{
return _queueClient.Receive(TimeSpan.FromSeconds(DEFAULT_WAIT_TIME_IN_SECONDS));
}

public void SendMessage(object bodyObject)
{
var message = new BrokeredMessage(bodyObject);

_queueClient.Send(message);
}

private QueueClient GetOrCreateQueue(string queueName)
{
var queue = !_namespaceManager.QueueExists(queueName)
? _namespaceManager.CreateQueue(queueName)
: _namespaceManager.GetQueue(queueName);

return _messagingFactory.CreateQueueClient(queue.Path, ReceiveMode.PeekLock);
}

}

如您所见,我在构造函数内初始化了 NamespaceManagerMessagingFactoryQueueClient:然后在调用 SendMessage 时重用它们()ReceiveTopMessage(),并使用 Dispose() 方法关闭连接。

我的问题是我使用的方法是否安全;将保持 QueueClient 的单个实例打开,同时辅助角色枚举队列上的所有消息(该进程可以使连接保持打开状态相当长一段时间,并且在调用 之间等待很长时间>ReceiveTopMessage()) 一致工作,不会出现暂时性问题,或者每次打开和关闭连接是否谨慎?

顺便说一句; Microsoft 服务总线代码中如何进行 transient 故障处理?是默认执行还是我们需要实现 Transient Fault Handling Framework

最佳答案

QueueClient class使用由用于创建它的 MessagingFactory 对象管理的连接。建议对多个请求重用同一个客户端对象。如 Best Practices for Performance Improvements Using Service Bus Brokered Messaging 中所述:

The Service Bus enables clients to send and receive messages via two protocols: the Service Bus client protocol, and HTTP. The Service Bus client protocol is more efficient, because it maintains the connection to the Service Bus service as long as the message factory exists. It also implements batching and prefetching. The Service Bus client protocol is available for .NET applications using the .NET managed API. (...) Service Bus client objects, such as QueueClient or MessageSender, are created through a MessagingFactory object, which also provides internal management of connections. You should not close messaging factories or queue, topic, and subscription clients after you send a message, and then re-create them when you send the next message. Closing a messaging factory deletes the connection to the Service Bus service, and a new connection is established when recreating the factory. Establishing a connection is an expensive operation that can be avoided by re-using the same factory and client objects for multiple operations. (...) All clients (senders in addition to receivers) that are created by the same factory share one TCP connection.

关于 transient 故障处理,QueueClient 有一个 RetryPolicy property决定是否应重试请求。还有Transient Fault Handling Application Block ,它取代了 transient 故障处理框架。

关于消息接收循环,Implementing Reliable Message Receive Loops中有指导。 。 Microsoft 认识到很难实现编写良好、有弹性的消息接收循环,因此他们引入了 Event-Driven Message Programing Model作为替代方案。

关于c# - Azure 服务总线客户端连接持久性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17588565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com