|
- using Confluent.Kafka;
- using TelpoPush.WanJiaAn.Worker.Common;
- using TelpoPush.WanJiaAn.Worker.Service.Mq;
-
- namespace TelpoPush.WanJiaAn.Worker.Handlers
- {
- public class KafkaSubscribe
- {
- private readonly ILogger<KafkaSubscribe> _logger;
- private readonly IHostEnvironment _env;
- private readonly IKafkaService _kafkaService;
- private readonly WanJiaAnProcess _WanJiaAnProcess;
-
-
- public KafkaSubscribe(
- ILogger<KafkaSubscribe> logger, IHostEnvironment env,
- IKafkaService kafkaService,
- WanJiaAnProcess WanJiaAnProcess)
- {
- _logger = logger;
- _env = env;
- _kafkaService = kafkaService;
- _WanJiaAnProcess = WanJiaAnProcess;
- }
- public async Task SubscribeAsync()
- {
- #if DEBUG
-
-
-
-
-
-
-
-
-
- #else
-
- LimitedConcurrencyLevelTaskScheduler lcts = new LimitedConcurrencyLevelTaskScheduler(5);
- TaskFactory factory = new TaskFactory(lcts);
- try
- {
- await factory.StartNew(async () =>
- {
- await _kafkaService.SubscribeAsync(DoReceive, CancellationToken.None);
- });
- }
- catch (Exception ex)
- {
- _logger.LogError($"Subscribe 处理Kafka数据发生异常 {ex.Message}|{ex.Source}|{ex.StackTrace}");
- }
- #endif
- }
- async void DoReceive(string topic, string message, Headers headers)
- {
- await _WanJiaAnProcess.SendWanJiaAn(message, topic, headers);
- }
- }
- }
|