feat: Add RabbitMQ
This commit is contained in:
parent
d3034c1a06
commit
8902080336
15 changed files with 2292 additions and 2069 deletions
|
|
@ -25,6 +25,7 @@
|
|||
<PackageReference Include="Oracle.EntityFrameworkCore" Version="7.21.9" />
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" />
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql.Design" Version="1.1.2" />
|
||||
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using BMA.EHR.Application.Common.Interfaces;
|
||||
using BMA.EHR.Application.Repositories;
|
||||
using BMA.EHR.Infrastructure.MessageQueue;
|
||||
using BMA.EHR.Infrastructure.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
|
@ -9,6 +10,15 @@ namespace BMA.EHR.Infrastructure
|
|||
{
|
||||
public static class InfrastructureServiceRegistration
|
||||
{
|
||||
public static IServiceCollection AddMessageQueue(this IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<RabbitMQConnection>();
|
||||
services.AddTransient<RabbitMQProducer>();
|
||||
services.AddTransient<RabbitMQConsumer>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddLeavePersistence(this IServiceCollection services,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
|
|
|
|||
45
BMA.EHR.Infrastructure/MessageQueue/RabbitMQConnection.cs
Normal file
45
BMA.EHR.Infrastructure/MessageQueue/RabbitMQConnection.cs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using RabbitMQ.Client;
|
||||
|
||||
namespace BMA.EHR.Infrastructure.MessageQueue
|
||||
{
|
||||
public class RabbitMQConnection
|
||||
{
|
||||
private readonly IConnection _connection;
|
||||
private readonly IModel _channel;
|
||||
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="configuration"></param>
|
||||
public RabbitMQConnection(IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
var hostName = _configuration["RabbitMQ:URL"];
|
||||
var userName = _configuration["RabbitMQ:UserName"];
|
||||
var password = _configuration["RabbitMQ:Password"];
|
||||
|
||||
|
||||
var factory = new ConnectionFactory() { HostName = hostName, UserName = userName, Password = password };
|
||||
_connection = factory.CreateConnection();
|
||||
_channel = _connection.CreateModel();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IModel GetChannel() => _channel;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_channel?.Close();
|
||||
_connection?.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
49
BMA.EHR.Infrastructure/MessageQueue/RabbitMQConsumer.cs
Normal file
49
BMA.EHR.Infrastructure/MessageQueue/RabbitMQConsumer.cs
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
using System.Text;
|
||||
using RabbitMQ.Client.Events;
|
||||
|
||||
namespace BMA.EHR.Infrastructure.MessageQueue
|
||||
{
|
||||
public class RabbitMQConsumer
|
||||
{
|
||||
private readonly RabbitMQConnection _connection;
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="connection"></param>
|
||||
public RabbitMQConsumer(RabbitMQConnection connection)
|
||||
{
|
||||
_connection = connection;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public void StartReceiving()
|
||||
{
|
||||
var channel = _connection.GetChannel();
|
||||
channel.QueueDeclare(queue: "myqueue",
|
||||
durable: false,
|
||||
exclusive: false,
|
||||
autoDelete: false,
|
||||
arguments: null);
|
||||
|
||||
var consumer = new EventingBasicConsumer(channel);
|
||||
consumer.Received += (model, ea) =>
|
||||
{
|
||||
var body = ea.Body.ToArray();
|
||||
var message = Encoding.UTF8.GetString(body);
|
||||
Console.WriteLine(" [x] Received {0}", message);
|
||||
};
|
||||
|
||||
channel.BasicConsume(queue: "myqueue",
|
||||
autoAck: true,
|
||||
consumer: consumer,
|
||||
consumerTag: "",
|
||||
noLocal: false,
|
||||
exclusive: false,
|
||||
arguments: null);
|
||||
}
|
||||
}
|
||||
}
|
||||
40
BMA.EHR.Infrastructure/MessageQueue/RabbitMQProducer.cs
Normal file
40
BMA.EHR.Infrastructure/MessageQueue/RabbitMQProducer.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
using System.Text;
|
||||
|
||||
namespace BMA.EHR.Infrastructure.MessageQueue
|
||||
{
|
||||
public class RabbitMQProducer
|
||||
{
|
||||
private readonly RabbitMQConnection _connection;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="connection"></param>
|
||||
public RabbitMQProducer(RabbitMQConnection connection)
|
||||
{
|
||||
_connection = connection;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
public void SendMessage(string message)
|
||||
{
|
||||
var channel = _connection.GetChannel();
|
||||
channel.QueueDeclare(queue: "myqueue",
|
||||
durable: false,
|
||||
exclusive: false,
|
||||
autoDelete: false,
|
||||
arguments: null);
|
||||
|
||||
var body = Encoding.UTF8.GetBytes(message);
|
||||
|
||||
channel.BasicPublish(exchange: "",
|
||||
routingKey: "myqueue",
|
||||
mandatory: false,
|
||||
basicProperties: null,
|
||||
body: body);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue