gpt4 book ai didi

multithreading - Azure 服务 TopicClient 线程安全且可重用吗?

转载 作者:行者123 更新时间:2023-12-05 00:15:04 26 4
gpt4 key购买 nike

我们正在使用 Azure SDK 将消息发布到服务总线。在 Web API 调用中,我们对每个传入请求重复执行这些任务

MessagingFactory factory = MessagingFactory.CreateFromConnectionString(conStr);
factory.RetryPolicy = new RetryExponential(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2), 3);
var namespaceManager = NamespaceManager.CreateFromConnectionString(conStr);

TopicClient topicClient = factory.CreateTopicClient(topicName);

Can topicClient be converted into Singleton & reused across multiple threads?

最佳答案

Can topicClient be converted into Singleton & reused across multiple threads?

是的,这是推荐的做法。来自 Best Practices for performance improvements using Service Bus Messaging

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 you can avoid by re-using the same factory and client objects for multiple operations. You can safely use the QueueClient object for sending messages from concurrent asynchronous operations and multiple threads.

关于multithreading - Azure 服务 TopicClient 线程安全且可重用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45595039/

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