Demi
Demi

Reputation: 328

Connecting to RabbitMQ, method level best practices?

I have a class that reads and writes messages using rabbitmq. I don't want to have connection related info in this class's constructor nor inside the read and write methods. Instead, I want to make a connection when I use the write and read methods. What is the best way/practice to go about tackling this issue?

//Edit 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()
    {
        //
    }   
}

Upvotes: 1

Views: 1838

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503180

Have you considered passing a Func<Connection> into the constructor? In other words, a way that you can get a connection when you want it?

(Obviously you need to pass the connection information in somewhere, unless you're going to access it statically... shudder.)

EDIT: Okay, I'm not familiar with RabbitMQ, but assuming it has some sort of Connection class which implements IDisposable, it would look something like this:

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

Then create it with something like:

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

Now if there's anything more "interesting" involved, you may want to write a separate ConnectionProvider class. Of course, RabbitMQ probably has some sample code - you should consult that and their documentation first, as I don't really know what I'm talking about :)

Upvotes: 2

Related Questions