连接到RabbitMQ,方法级最佳实践

本文关键字:最佳 方法 RabbitMQ 连接 | 更新日期: 2023-09-27 18:02:31

我有一个使用rabbitmq读写消息的类。我不想在这个类的构造函数中或在读和写方法中拥有与连接相关的信息。相反,我希望在使用write和read方法时建立连接。解决这个问题的最佳方法/做法是什么?

//编辑1

public class Rabbit : IMessageBus
{   
    // Implementation of methods for Rabbit class go here
    private List<string> publishQ = new List<string>();
    private List<string> subscribeQ = new List<string>();
    ConnectionFactory factory = null;
    IConnection connection = null;
    IModel channel = null;  
    QueueingBasicConsumer consumer = null;  

    public void write ( Measurement m1 )
    {
        byte[] body = Measurement.AltSerialize( m1 );
        foreach (string queue in publishQ) 
        {
            channel.BasicPublish("", queue, null, body);
            Console.WriteLine("'n  [x] Sent to queue {0}.", queue);
        }
    }
    public void publish(string queueName)
    {   
        channel.QueueDeclare(queueName, true, false, false, null); //durable=true
        publishQ.Add(queueName); //and, add it the list of queue names to publish to
    }
    public Measurement read() 
    {       
        foreach (string queue in subscribeQ) 
        {
            channel.BasicConsume(queue, true, consumer);
        }
        System.Console.WriteLine(" [*] Waiting for messages." +
                                "To exit press CTRL+C");
        BasicDeliverEventArgs ea = 
            (BasicDeliverEventArgs)consumer.Queue.Dequeue();
        return Measurement.AltDeSerialize(ea.Body);
    }
    public void subscribe(string queueName)
    {
        channel.QueueDeclare(queueName, true, false, false, null);
        subscribeQ.Add(queueName);
    }
    public static string MsgSysName;
    public string MsgSys
    {
        get 
        { 
            return MsgSysName;
        }
        set
        {
            MsgSysName = value;
        }
    }
    public Rabbit(string _msgSys) //Constructor
    {   
        factory = new ConnectionFactory();
        factory.HostName = "localhost"; 
        connection = factory.CreateConnection();
        channel = connection.CreateModel();
        consumer = new QueueingBasicConsumer(channel);
        System.Console.WriteLine("'nMsgSys: RabbitMQ");
        MsgSys = _msgSys;
    }
    ~Rabbit()
    {
        //
    }   
}

连接到RabbitMQ,方法级最佳实践

您考虑过将Func<Connection>传递给构造函数吗?换句话说,当你想要连接时,你可以获得它的方式?

(显然,您需要在中传递连接信息,除非您要静态访问它…发抖。)

编辑:好吧,我不熟悉RabbitMQ,但假设它有某种Connection类实现IDisposable,它看起来像这样:

public class MessageHandler
{
    private readonly Func<Connection> connectionProvider;
    public MessageHander(Func<Connection> connectionProvider)
    {
        this.connectionProvider = connectionProvider;
    }
    public void WriteMessage(string message)
    {
        using (Connection connection = connectionProvider.Invoke())
        {
            connection.DoSomething(message);
        }
    }
}

然后创建如下内容:

MessageHandler handler = new MessageHandler(() => new Connection(server));

现在,如果有任何更"有趣"的涉及,你可能想写一个单独的ConnectionProvider类。当然,RabbitMQ可能有一些示例代码——你应该先参考一下它们的文档,因为我真的不知道我在说什么:)