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

时间:2011-07-13 17:00:13

标签: c# methods rabbitmq amqp

我有一个使用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()
    {
        //
    }   
}

1 个答案:

答案 0 :(得分:2)

您是否考虑过将Func<Connection>传递给构造函数?换句话说,您可以在需要时获取连接的方式

(显然你需要在某处中传递连接信息,除非你要静态访问它......不寒而栗。)

编辑:好的,我不熟悉RabbitMQ,但假设它有某种实现Connection的{​​{1}}类,它看起来像这样:

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)); 类。当然,RabbitMQ可能有一些示例代码 - 你应该首先查阅它和他们的文档,因为我真的不知道我在说什么:)