-
Notifications
You must be signed in to change notification settings - Fork 3
QuickStart
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.
Follow these steps to start using NanoRabbit:
- Setup Custom RabbitHelper
- Setup Custom Producer
- Setup Custom Consumer
- Add MessageHandler to Consumer
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
}
}
});
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");
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);
});
}
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"
}
}
});