feat(api, bll): IMqService - добавлен метод получения статистики по очереди. StatRabbitMqController - статистика по RabbitMq
This commit is contained in:
16
PARR.BLL/Domain/Mq/MqQueueCountResult.cs
Normal file
16
PARR.BLL/Domain/Mq/MqQueueCountResult.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace PARR.BLL.Domain.Mq
|
||||
{
|
||||
/// <summary>
|
||||
/// Результат запроса кол-ва сообщений в очереди RabbitMq
|
||||
/// </summary>
|
||||
public class MqQueueCountResult
|
||||
{
|
||||
public bool IsSuccess { get;set; }
|
||||
|
||||
public int Count { get; set; }
|
||||
|
||||
public int ConsumerCount { get; set; }
|
||||
|
||||
public Exception? Exception { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using PARR.BLL.Domain.Mq;
|
||||
using PARR.BLL.Services.Interfaces;
|
||||
using RabbitMQ.Client;
|
||||
using RabbitMQ.Client.Events;
|
||||
using System.Runtime;
|
||||
using System.Text;
|
||||
|
||||
namespace PARR.BLL.Services.Implementations
|
||||
@@ -154,6 +155,34 @@ namespace PARR.BLL.Services.Implementations
|
||||
}
|
||||
}
|
||||
|
||||
public MqQueueCountResult GetQueueCount(IMqSettings mqSettings, string queueName)
|
||||
{
|
||||
var factory = new ConnectionFactory
|
||||
{
|
||||
HostName = mqSettings.HostName,
|
||||
UserName = mqSettings.User,
|
||||
Password = mqSettings.Password,
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
using (var connection = factory.CreateConnection())
|
||||
using (var channel = connection.CreateModel())
|
||||
{
|
||||
var messageCount = channel.MessageCount(queueName);
|
||||
var consumerCount = channel.ConsumerCount(queueName);
|
||||
|
||||
return new MqQueueCountResult { Count = (int)messageCount, ConsumerCount = (int)consumerCount, IsSuccess = true };
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"Ошибка при запросе кол-ва сообщений в очереди {queueName}");
|
||||
|
||||
return new MqQueueCountResult { Count = 0, IsSuccess = false, Exception = ex };
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
channel?.Close();
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace PARR.BLL.Services.Interfaces
|
||||
|
||||
public interface IMqService : IDisposable
|
||||
{
|
||||
MqQueueCountResult GetQueueCount(IMqSettings mqSettings, string queueName);
|
||||
bool InitConsumer(IMqSettings mqSettings, MqMessageHandlerDelegate messageHandler);
|
||||
MqSendResult Send(IMqSettings mqSettings, string[] msgList);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user