The RabbitMq Connector Middleware is a powerful tool for integrating RabbitMQ messaging into your application. It provides a simple and efficient way to send and receive messages using RabbitMQ as the messaging broker.
- Easy setup and configuration
- Publish and consume messages with ease
- Support for message acknowledgements and rejections
- Error handling and retry mechanisms
- Extensible and customizable
To install the RabbitMq Connector Middleware, you can use NuGet:
Create a config file(appSettings.json) or create a new instance of RabbitMqConfiguration class.
{
"RabbitMqConfiguration": {
"Host": "127.0.0.1",
"Port": 5672,
"Username": "rabbitmq",
"Password": "rabbitmq",
"ConsumerQueueName": "TestAppExchange",
"ConsumerExchangeName": "TestAppQueue",
"PublisherQueueName": "PublishAppExchange",
"PublisherExchangeName": "PublishAppQueue",
"ClusterUrls": ["127.0.0.1"],
"Heartbeat": 15,
"RetryCount": 5,
"ConnectionName": "TestApp"
}
}
var rabbitMqConfig = new RabbitMqConfiguration
{
Host = "127.0.0.1",
Port = 5672,
Username = "guest",
Password = "guest",
Heartbeat = 30,
ClusterUrls = ["127.0.0.1"],
ConnectionName = "TestApp",
ConsumerExchangeName = "TestAppExchange",
ConsumerQueueName = "TestAppQueue",
PublisherExchangeName = "PublishAppExchange",
PublisherQueueName = "PublishAppQueue",
RetryCount = 3
};
Then create a consumer class or publisher class based on RabbitMqPublisherBase<T>
T is model class of message exchange.
public class ServerMessageQueuePublisher : RabbitMqPublisherBase<UserMessage>
{
public ServerMessageQueuePublisher(RabbitMqConfiguration rabbitMqConfigOptions,
ILogger<RabbitMqPublisherBase<UserMessage>> logger, IRabbitMqService rabbitMQPersistentConnection)
: base(logger, rabbitMQPersistentConnection, rabbitMqConfigOptions.PublisherQueueName,
rabbitMqConfigOptions.PublisherExchangeName)
{
}
}
///message exchange class
public class UserMessage
{
public string? UserId { get; set; }
public string? UserName { get; set; }
public string? Message { get; set; }
}
public class ServerMessageQueueConsumer : RabbitMqSubscriberBase<UserMessage>
{
public ServerMessageQueueConsumer(RabbitMqConfiguration rabbitMqConfigOptions,
ILogger<RabbitMqSubscriberBase<UserMessage>> logger, IRabbitMqService rabbitMQPersistentConnection)
: base(logger, rabbitMQPersistentConnection, rabbitMqConfigOptions.ConsumerQueueName,
rabbitMqConfigOptions.ConsumerExchangeName)
{
}
public async override Task HandleMessage(UserMessage message)
{
Console.WriteLine($"Message Received: {message.Message}");
}
}
Add methods below into the Startup.cs.
private void AddRabbitMq(IServiceCollection services, IConfiguration configuration)
{
var rabbitMqConfig = configuration.GetSection("RabbitMqConfiguration").Get<RabbitMqConfiguration>();
services.Configure<RabbitMqConfiguration>(configuration.GetSection("RabbitMqConfiguration"));
services.AddSingleton<RabbitMqConfiguration>(rabbitMqConfig);
services.AddSingleton<IRabbitMqService, RabbitMqService>(sp =>
{
var logger = sp.GetRequiredService<ILogger<RabbitMqService>>();
var config = sp.GetRequiredService<IOptions<RabbitMqConfiguration>>().Value;
return new RabbitMqService(config, logger);
});
}
private void AddRabbitMqPublisher(IServiceCollection services)
{
services.AddScoped<ServerMessageQueuePublisher>();
}
private void AddRabbitMqConsumer(IServiceCollection services)
{
services.AddHostedService<ServerMessageQueueConsumer>();
}
AddRabbitMq(context.Services, configuration);
AddRabbitMqPublisher(context.Services);
AddRabbitMqConsumer(context.Services);
app.UseRabbitMqConnector();
- Consumer : In ServerMessageQueueConsumer class has coming with an abstract method named : HandleMessage. you can use this method to process messages from queue.
public async override Task HandleMessage(UserMessage message)
{
Console.WriteLine($"Message Received: {message.Message}");
}
- Publisher : Use DI for use the publisher class.
private readonly ServerMessageQueuePublisher _serverMessageQueuePublisher;
public Dashboard(ServerMessageQueuePublisher serverMessageQueuePublisher)
{
_serverMessageQueuePublisher = serverMessageQueuePublisher;
}
///you can use this part any of your code.
var message = new UserMessage();
message = new UserMessage
{
UserId = "1", // "1" is the admin user id, it should be fetched from the current user's id
UserName = "admin",
Message = "Dashboard page is visited"
};
await _serverMessageQueuePublisher.SendAsync(message);
Thank you.