From cff93be36adad681a01a0d6d04a3228566e1a107 Mon Sep 17 00:00:00 2001 From: Mikhail Trubnikov Date: Thu, 21 Sep 2023 16:38:38 +1000 Subject: [PATCH] mq dispose --- .../Services/Implementations/MqService.cs | 19 ++++++++++++++++--- PARR.BLL/Services/Interfaces/IMqService.cs | 2 +- PARR.GeneratorTemplates/GeneratorTemplate.cs | 9 ++++++--- .../appsettings.json | 2 +- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/PARR.BLL/Services/Implementations/MqService.cs b/PARR.BLL/Services/Implementations/MqService.cs index 081f142e..12720100 100644 --- a/PARR.BLL/Services/Implementations/MqService.cs +++ b/PARR.BLL/Services/Implementations/MqService.cs @@ -12,6 +12,9 @@ namespace PARR.BLL.Services.Implementations { private readonly ILogger logger; + private IConnection? connection; + private IModel? channel; + public event ReceivedHandler? Received; public MqService(ILogger logger) @@ -88,7 +91,6 @@ namespace PARR.BLL.Services.Implementations } - //TODO: rename ListenQueue public void InitConsumer(IMqSettings mqSettings) { logger.LogInformation($"Устанавливаю соединение с RabbitMQ: {mqSettings.HostName}, {mqSettings.QueueName}"); @@ -104,8 +106,11 @@ namespace PARR.BLL.Services.Implementations try { - using var connection = facory.CreateConnection(); - using var channel = connection.CreateModel(); + //using var connection = facory.CreateConnection(); + //using var channel = connection.CreateModel(); + + connection = facory.CreateConnection(); + channel = connection.CreateModel(); var args = new Dictionary { { "x-queue-mode", "lazy" } }; @@ -142,5 +147,13 @@ namespace PARR.BLL.Services.Implementations } } + public void Dispose() + { + channel?.Close(); + connection?.Close(); + + channel?.Dispose(); + connection?.Dispose(); + } } } diff --git a/PARR.BLL/Services/Interfaces/IMqService.cs b/PARR.BLL/Services/Interfaces/IMqService.cs index 6f7cc5ba..3db517c6 100644 --- a/PARR.BLL/Services/Interfaces/IMqService.cs +++ b/PARR.BLL/Services/Interfaces/IMqService.cs @@ -5,7 +5,7 @@ namespace PARR.BLL.Services.Interfaces { public delegate void ReceivedHandler(string msg); - public interface IMqService + public interface IMqService : IDisposable { event ReceivedHandler? Received; diff --git a/PARR.GeneratorTemplates/GeneratorTemplate.cs b/PARR.GeneratorTemplates/GeneratorTemplate.cs index 147eef2f..c767e305 100644 --- a/PARR.GeneratorTemplates/GeneratorTemplate.cs +++ b/PARR.GeneratorTemplates/GeneratorTemplate.cs @@ -23,17 +23,20 @@ namespace PARR.GeneratorTemplates public void Start() { - mqService.InitConsumer(mqSettings); - mqService.Received += (msg) => { logger.LogInformation($"Привет откуда надо!!! ${msg}"); + //todo: идем дальеш! }; + + mqService.InitConsumer(mqSettings); + + } public void Stop() { - + mqService.Dispose(); } } } diff --git a/PARR.GeneratorTemplatesWorker/appsettings.json b/PARR.GeneratorTemplatesWorker/appsettings.json index a28563b8..c4246afa 100644 --- a/PARR.GeneratorTemplatesWorker/appsettings.json +++ b/PARR.GeneratorTemplatesWorker/appsettings.json @@ -24,7 +24,7 @@ ] }, "MqSettings": { - "HostName": "parr-rabbitmq", + "HostName": "10.99.253.216", "QueueName": "parr-generate-templates", "User": "generate_templates_worker", "Password": "Dhgdf%d&609sssa"