Is Azure Service TopicClient Thread Safe & Reusable?

旧巷老猫 提交于 2019-12-12 11:18:07

问题


We are using Azure SDK to publish message to Service Bus. In Web API call we're doing these tasks repeatedly for each incoming request

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?


回答1:


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

Yes, that's the recommended practice. From 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.



来源:https://stackoverflow.com/questions/45595039/is-azure-service-topicclient-thread-safe-reusable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!