mercredi 6 novembre 2019

Creating a class for RabbitMQ Client Connection

I want to create a class, kind of like a wrapper for a RabbitMQ connection, to publish messages to an exchange. This will be a class library used by another system.

My question is pretty straightforward: How do I go about closing and opening the connection?

My initial idea is something like this:

public class RabbitConnection
{
    private readonly IConnection conn;

    public RabbitConnection() {
        try {
         var factory = new ConnectionFactory() {...}
         this.conn = factory.CreateConnection();
        }
        catch {
         ...
        }
    }

    ...

    public void Publish<T>(T @event) where T : class {
        using (var channel = conn.CreateModel()) {
            ...
            channel.BasicPublish(...);
        }
    }
}

Is this the best way if it isn't what do I search to find the best pattern?

Thanks is advance!

Aucun commentaire:

Enregistrer un commentaire