EsppTemplateSync переделал на IMqService
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.BLL.Services.Interfaces;
|
||||
using PARR.EsppTemplateSync.Services;
|
||||
using PARR.EsppTemplateSync.Settings;
|
||||
using RabbitMQ.Client;
|
||||
using RabbitMQ.Client.Events;
|
||||
using System.Text;
|
||||
|
||||
namespace PARR.EsppTemplateSync
|
||||
{
|
||||
@@ -12,19 +10,19 @@ namespace PARR.EsppTemplateSync
|
||||
private readonly ILogger<TemplateMQSyncer> logger;
|
||||
private readonly IManager manager;
|
||||
private readonly GlobalSettings globalSettings;
|
||||
|
||||
private IConnection? connection;
|
||||
private IModel? channel;
|
||||
private readonly IMqService mqService;
|
||||
|
||||
public TemplateMQSyncer(
|
||||
ILogger<TemplateMQSyncer> logger,
|
||||
IManager manager,
|
||||
GlobalSettings globalSettings
|
||||
GlobalSettings globalSettings,
|
||||
IMqService mqService
|
||||
)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.manager = manager;
|
||||
this.globalSettings = globalSettings;
|
||||
this.mqService = mqService;
|
||||
|
||||
if (globalSettings.MqSettings == null)
|
||||
{
|
||||
@@ -35,70 +33,17 @@ namespace PARR.EsppTemplateSync
|
||||
|
||||
public void Start()
|
||||
{
|
||||
InitConnection();
|
||||
mqService.Received += async (msg) => await manager.ManageStringAsync(msg);
|
||||
mqService.InitConsumer(globalSettings!.MqSettings!);
|
||||
|
||||
logger.LogInformation($"Запущена проверка очереди {globalSettings.MqSettings!.QueueName}.");
|
||||
|
||||
GetMessages();//TODO переименовать в какой-нибудь AttachListener
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
channel?.Close();
|
||||
connection?.Close();
|
||||
mqService.Dispose();
|
||||
|
||||
logger.LogInformation($"=== === === Соединение с очередью {globalSettings.MqSettings!.QueueName} закрыто === === ===");
|
||||
}
|
||||
|
||||
private void InitConnection()
|
||||
{
|
||||
logger.LogInformation($"Устанавливаю соединение с RabbitMQ: {globalSettings.MqSettings!.HostName}");
|
||||
|
||||
var factory = new ConnectionFactory { HostName = globalSettings.MqSettings!.HostName };
|
||||
factory.UserName = globalSettings.MqSettings.UserName;
|
||||
factory.Password = globalSettings.MqSettings.Password;
|
||||
factory.AutomaticRecoveryEnabled = true;
|
||||
factory.DispatchConsumersAsync = true;
|
||||
|
||||
connection = factory.CreateConnection();
|
||||
channel = connection.CreateModel();
|
||||
|
||||
var args = new Dictionary<string, object>
|
||||
{
|
||||
{ "x-queue-mode", "lazy" }
|
||||
};
|
||||
|
||||
channel.QueueDeclare(queue: globalSettings.MqSettings.QueueName,
|
||||
durable: true,
|
||||
exclusive: false,
|
||||
autoDelete: false,
|
||||
arguments: args);
|
||||
|
||||
logger.LogInformation("Соединение с RabbitMQ установлено.");
|
||||
}
|
||||
|
||||
private void GetMessages()
|
||||
{
|
||||
if (channel == null || connection == null)
|
||||
{
|
||||
logger.LogError("Отсутствует соедниение с RabbitMQ.");
|
||||
return;
|
||||
}
|
||||
|
||||
var consumer = new AsyncEventingBasicConsumer(channel);
|
||||
|
||||
consumer.Received += async (ch, ea) =>
|
||||
{
|
||||
var content = Encoding.UTF8.GetString(ea.Body.ToArray());
|
||||
logger.LogDebug($"Получено сообщение: {content}");
|
||||
|
||||
await manager.ManageStringAsync(content);
|
||||
|
||||
channel.BasicAck(ea.DeliveryTag, false);
|
||||
await Task.Yield();
|
||||
};
|
||||
|
||||
channel.BasicConsume(globalSettings.MqSettings!.QueueName, false, consumer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user