implemented rabbitmq message broker support (publisher)
This commit is contained in:
@@ -6,6 +6,8 @@ public static class ConfigurationEnvExtensions
|
||||
{
|
||||
private const string DbConnStringKey = "DB_CONN_STRING";
|
||||
|
||||
private const string RabbitMqAmqpUriKey = "RABBITMQ_AMQP_URI";
|
||||
|
||||
private const string JwtIssuer = "JWT_ISSUER";
|
||||
private const string JwtAudience = "JWT_AUDIENCE";
|
||||
private const string JwtKey = "JWT_KEY";
|
||||
@@ -18,6 +20,11 @@ public static class ConfigurationEnvExtensions
|
||||
return configuration.GetEnvForSure(DbConnStringKey);
|
||||
}
|
||||
|
||||
public string GetEnvRabbitMqAmqpUri()
|
||||
{
|
||||
return configuration.GetEnvForSure(RabbitMqAmqpUriKey);
|
||||
}
|
||||
|
||||
public string GetEnvJwtIssuer()
|
||||
{
|
||||
return configuration.GetEnvForSure(JwtIssuer);
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
using RabbitMQ.Client;
|
||||
|
||||
namespace AipsCore.Infrastructure.MessageBroking.RabbitMQ;
|
||||
|
||||
public interface IRabbitMqConnection
|
||||
{
|
||||
Task<IChannel> CreateChannelAsync(CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using AipsCore.Infrastructure.DI.Configuration;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using RabbitMQ.Client;
|
||||
|
||||
namespace AipsCore.Infrastructure.MessageBroking.RabbitMQ;
|
||||
|
||||
public class RabbitMqConnection : IRabbitMqConnection
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private IConnection? _connection;
|
||||
|
||||
public RabbitMqConnection(IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public async Task<IChannel> CreateChannelAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_connection is null)
|
||||
{
|
||||
throw new InvalidOperationException($"RabbitMQ connection not created for {_configuration.GetEnvRabbitMqAmqpUri()}");
|
||||
}
|
||||
|
||||
return await _connection.CreateChannelAsync(null, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task CreateConnectionAsync()
|
||||
{
|
||||
var factory = CreateConnectionFactory();
|
||||
|
||||
_connection = await factory.CreateConnectionAsync();
|
||||
}
|
||||
|
||||
private IConnectionFactory CreateConnectionFactory()
|
||||
{
|
||||
var factory = new ConnectionFactory
|
||||
{
|
||||
Uri = new Uri(_configuration.GetEnvRabbitMqAmqpUri())
|
||||
};
|
||||
|
||||
return factory;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using AipsCore.Application.Abstract;
|
||||
using RabbitMQ.Client;
|
||||
|
||||
namespace AipsCore.Infrastructure.MessageBroking.RabbitMQ;
|
||||
|
||||
public class RabbitMqPublisher : IMessagePublisher
|
||||
{
|
||||
private readonly IRabbitMqConnection _connection;
|
||||
|
||||
public RabbitMqPublisher(IRabbitMqConnection connection)
|
||||
{
|
||||
_connection = connection;
|
||||
}
|
||||
|
||||
public async Task PublishAsync<T>(string exchange, string routeKey, T message, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var channel = await _connection.CreateChannelAsync(cancellationToken);
|
||||
|
||||
await channel.ExchangeDeclareAsync(exchange, ExchangeType.Topic, durable: true, cancellationToken: cancellationToken);
|
||||
|
||||
var bytes = Serialize(message);
|
||||
await channel.BasicPublishAsync(exchange, routeKey, bytes, cancellationToken);
|
||||
|
||||
await channel.CloseAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private byte[] Serialize<T>(T message)
|
||||
{
|
||||
return JsonSerializer.SerializeToUtf8Bytes(message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user