Skip to content
cgcel edited this page Sep 10, 2024 · 14 revisions

Catalog

Note

NanoRabbit is designed as a library depends on NAMING Connections, Producers, Consumers. So it's important to set a UNIQUE NAME for each Connections, Producers, Consumers.

How to use

Follow these steps to start using NanoRabbit:

  1. Setup Custom RabbitHelper
  2. Setup Custom Producer
  3. Setup Custom Consumer
  4. Add MessageHandler to Consumer

Simple Publish Messages

Register a RabbitMQ Producer in RabbitHelper by seting RabbitConfiguration, and configure Producers.

var rabbitHelper = new RabbitHelper(rabbitConfig: new RabbitConfiguration
{
    HostName = "localhost",
    Port = 5672,
    VirtualHost = "/",
    UserName = "admin",
    Password = "admin",
    Producers = new List<ProducerOptions> { new ProducerOptions {
            ProducerName = "FooProducer",
            ExchangeName = "amq.topic",
            RoutingKey = "foo.key",
            Type = ExchangeType.Topic
        }
    }
});

Multiple Producers

Adding more than one producers is available:

var rabbitHelper = new RabbitHelper(rabbitConfig: new RabbitConfiguration
{
    HostName = "localhost",
    Port = 5672,
    VirtualHost = "/",
    UserName = "admin",
    Password = "admin",
    Producers = new List<ProducerOptions> { 
        new ProducerOptions {
            ProducerName = "FooProducer",
            ExchangeName = "amq.topic",
            RoutingKey = "foo.key",
            Type = ExchangeType.Topic
        },
        new ProducerOptions {
            ProducerName = "BarProducer",
            ExchangeName = "amq.direct",
            RoutingKey = "bar.key",
            Type = ExchangeType.Direct
        }
    }
}, logger);

After adding producers in RabbitHelper, you can simply publish a message by calling Publish<T>(string producerName, T message).

rabbitHelper.Publish<string>("FooProducer", "Hello world from FooProducer!");
rabbitHelper.Publish<string>("BarProducer", "Hello world from BarProducer!");

Asynchronously publishing is also available:

await rabbitHelper.PublishAsync<string>("FooProducer", "Hello from NanoRabbit");

Simple Consume Messages

Register a RabbitMQ Consumer in RabbitHelper by seting RabbitConfiguration, and configure Consumers.

var rabbitHelper = new RabbitHelper(rabbitConfig: new RabbitConfiguration
{
    HostName = "localhost",
    Port = 5672,
    VirtualHost = "/",
    UserName = "admin",
    Password = "admin",
    Consumers = new List<ConsumerOptions> { new ConsumerOptions {
            ConsumerName= "FooConsumer",
            QueueName = "foo-queue"
        }
    }
}, logger);

After adding consumers in RabbitHelper, you can simply handle a message by calling AddConsumer(string consumerName, Action<string> onMessageReceived, int consumers = 1).

while (true)
{
    rabbitHelper.AddConsumer("FooConsumer", message =>
    {
        Console.WriteLine(message);
    });
}

Multiple Consumers

Adding more than one consumers is available:

var rabbitHelper = new RabbitHelper(rabbitConfig: new RabbitConfiguration
{
    HostName = "localhost",
    Port = 5672,
    VirtualHost = "/",
    UserName = "admin",
    Password = "admin",
    Consumers = new List<ConsumerOptions> { 
        new ConsumerOptions {
            ConsumerName= "FooConsumer",
            QueueName = "foo-queue"
        },
        new ConsumerOptions {
            ConsumerName= "BarConsumer",
            QueueName = "bar-queue"
        }
    }
});

Recommend