implemented rabbitmq message broker support (publisher)
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="10.0.2" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="10.0.2" />
|
||||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.0" />
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.0" />
|
||||||
|
<PackageReference Include="RabbitMQ.Client" Version="7.2.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
|
namespace AipsCore.Application.Abstract;
|
||||||
|
|
||||||
|
public interface IMessagePublisher
|
||||||
|
{
|
||||||
|
Task PublishAsync<T>(string exchange, string routeKey, T message, CancellationToken cancellationToken = default);
|
||||||
|
}
|
||||||
@@ -6,6 +6,8 @@ public static class ConfigurationEnvExtensions
|
|||||||
{
|
{
|
||||||
private const string DbConnStringKey = "DB_CONN_STRING";
|
private const string DbConnStringKey = "DB_CONN_STRING";
|
||||||
|
|
||||||
|
private const string RabbitMqAmqpUriKey = "RABBITMQ_AMQP_URI";
|
||||||
|
|
||||||
private const string JwtIssuer = "JWT_ISSUER";
|
private const string JwtIssuer = "JWT_ISSUER";
|
||||||
private const string JwtAudience = "JWT_AUDIENCE";
|
private const string JwtAudience = "JWT_AUDIENCE";
|
||||||
private const string JwtKey = "JWT_KEY";
|
private const string JwtKey = "JWT_KEY";
|
||||||
@@ -18,6 +20,11 @@ public static class ConfigurationEnvExtensions
|
|||||||
return configuration.GetEnvForSure(DbConnStringKey);
|
return configuration.GetEnvForSure(DbConnStringKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string GetEnvRabbitMqAmqpUri()
|
||||||
|
{
|
||||||
|
return configuration.GetEnvForSure(RabbitMqAmqpUriKey);
|
||||||
|
}
|
||||||
|
|
||||||
public string GetEnvJwtIssuer()
|
public string GetEnvJwtIssuer()
|
||||||
{
|
{
|
||||||
return configuration.GetEnvForSure(JwtIssuer);
|
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