2024-10-28 16:23:39 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts.Interfaces;
|
|
|
|
|
|
using MassTransit;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts.Models;
|
2024-11-08 13:50:47 +08:00
|
|
|
|
using JiShe.CollectBus.MongoDB;
|
2024-10-28 16:23:39 +08:00
|
|
|
|
|
|
|
|
|
|
namespace JiShe.CollectBus.RabbitMQ.Consumers
|
|
|
|
|
|
{
|
2024-11-08 13:50:47 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Batch 一次最多 100 个,最多 10 个并发批次
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class MessageReceivedConsumer(
|
|
|
|
|
|
ILogger<MessageReceivedConsumer> logger,
|
|
|
|
|
|
IServiceProvider serviceProvider,
|
|
|
|
|
|
IMongoRepository<MessageReceivedEvent> mongoReceivedRepository)
|
|
|
|
|
|
: IConsumer<Batch<MessageReceivedEvent>>
|
2024-10-28 16:23:39 +08:00
|
|
|
|
{
|
2024-11-08 13:50:47 +08:00
|
|
|
|
public async Task Consume(ConsumeContext<Batch<MessageReceivedEvent>> context)
|
2024-10-28 16:23:39 +08:00
|
|
|
|
{
|
|
|
|
|
|
const string protocolType = "StandardProtocol";
|
2024-11-08 13:50:47 +08:00
|
|
|
|
var protocolPlugin = serviceProvider.GetKeyedService<IProtocolPlugin>(protocolType);
|
2024-10-28 16:23:39 +08:00
|
|
|
|
if (protocolPlugin == null)
|
|
|
|
|
|
{
|
2024-11-08 13:50:47 +08:00
|
|
|
|
logger.LogError("协议不存在!");
|
2024-10-28 16:23:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-11-08 13:50:47 +08:00
|
|
|
|
var list = new List<MessageReceivedEvent>();
|
|
|
|
|
|
foreach (var contextItem in context.Message)
|
|
|
|
|
|
{
|
|
|
|
|
|
await protocolPlugin.AnalyzeAsync(contextItem.Message);
|
|
|
|
|
|
list.Add(contextItem.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
await mongoReceivedRepository.InsertManyAsync(list);
|
2024-10-28 16:23:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|