Azure Service TopicClient线程安全吗?可重复使用的? [英] Is Azure Service TopicClient Thread Safe & Reusable?

查看:89
本文介绍了Azure Service TopicClient线程安全吗?可重复使用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);

topicClient可以转换为Singleton&吗?在多个线程之间重用?

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

推荐答案

topicClient可以转换为Singleton&吗?跨多个重用 线程?

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

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

Yes, that's the recommended practice. From Best Practices for performance improvements using Service Bus Messaging

Service Bus客户端对象(例如QueueClient或MessageSender)是 通过MessagingFactory对象创建,该对象还提供 连接的内部管理. 您不应关闭消息传递 发送工厂后,工厂或队列,主题和订阅客户端 消息,然后在发送下一条消息时重新创建它们. 关闭消息传递工厂将删除与服务总线的连接 服务,并在重新创建 工厂.建立连接是您的一项昂贵的操作 通过将相同的工厂对象和客户端对象重复用于多个对象,可以避免 操作..您可以安全地使用QueueClient对象进行发送 来自并发异步操作和多个线程的消息.

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.

这篇关于Azure Service TopicClient线程安全吗?可重复使用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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