管理多个队列/主题客户端? [英] Managing multiple queue/topic clients?

查看:115
本文介绍了管理多个队列/主题客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的挑战是我试图创建一个用于处理队列和主题的单一外观,同时保持SendPublish

The challenge here is that I'm trying to create a single facade for dealing with queues and topics while maintaining semantics of Send vs. Publish

例如:

public interface IServiceBus
{
    Task Send<T>(T message, string destination, SendOptions options = null);
    Task Publish<T>(T message, string topic, SendOptions options = null);
}

Send()会将消息发送到队列,而Publish()会将消息发布到主题.因此,我需要有一个IQueueClientITopicClient的实例才能使这些发生.我会将它们作为依赖项注入到我的IServiceBus实现中,并相应地调用它们.

Send() would send a message to a queue and Publish() would publish a message to a topic. So I would need to have an instance of IQueueClient and ITopicClient to make these happen; I would inject these to my IServiceBus implementation as dependencies and call them accordingly.

问题在于,在更新客户端时,QueueClient和TopicClient要求您指定它们的目的地,这使我无法将其用作我的IServiceBus实现的参数.

The problem is that the QueueClient and TopicClients require that you specify their destinations when you're newing up the clients which prevents me from allowing that as parameters for my IServiceBus implementation.

我可以在创建消息时创建一个客户端,但这效率极低.我至少寻找了一个可以充当客户端工厂的连接管理器,但MessagingFactory似乎不在此SDK(Microsoft.Azure.ServiceBus 3.4.0)中.

I could just create a client on at the time the message is being created, but that's super inefficient. I looked around for at least a connection manager that could act as a factory for the clients but MessagingFactory doesn't seem to be in this SDK (Microsoft.Azure.ServiceBus 3.4.0).

所以问题是 -是否有某种我可以使用的工厂,可以让我按需创建适当的客户,而效率却可以与重用客户所获得的效率相同? -是否应该使用某种替代或替代客户端对象来实现此效果?这两个客户真的很有限.

So the questions are - Is there some sort of factory out there that I can use that would allow me to create the proper clients on demand with the same efficiency one would gain by reusing the clients? - Is there some sort of override or alternative client object I should be using to achieve this effect? These two clients are really limiting.

推荐答案

我终于能够遇到遇到类似问题的人.事实证明,他们删除了MessagingFactory,但是使连接可重用.每个客户端都有一个接受连接的构造函数重载,因此我将连接注册为单例,并注入该连接而不是客户端,然后仅根据需要创建客户端.

I was finally able to come across someone that had a similar issue. It turns out that they removed the MessagingFactory, but made the connection reusable. Each client has a constructor overload that takes the connection, so I registered the connection as a singleton and inject that instead of the client, and then just create the clients on demand.

请参阅: https://github.com/Azure/azure -service-bus-dotnet/issues/556

我的解决方案看起来像这样(为简洁起见,省略了完整的实现)

My solution looked a little like this (full implementation omitted for brevity)

public class AzureServiceBus : IServiceBus
{
    public AzureServiceBus(ServiceBusConnection connection, string replyTo)
    {
        _connection = connection;
        _replyTo = replyTo;
        _retryPolicy = new RetryExponential(
            TimeSpan.FromSeconds(1),
            TimeSpan.FromMinutes(1),
            10);
    }

    public async Task Send<T>(T message, string destination)
    {
        var client = new QueueClient(_connection, destination, ReceiveMode.PeekLock, _retryPolicy);

        // ... do work
    }

    public async Task Publish<T>(T message, string topic, SendOptions options = null)
    {
        var client = new TopicClient(_connection, topic, _retryPolicy);

        // ... do work
    }
}

这篇关于管理多个队列/主题客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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