定位推送服务
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

63 linhas
2.2KB

  1. using Confluent.Kafka;
  2. using TelpoPush.Position.Worker.Common;
  3. using TelpoPush.Position.Worker.Service.Mq;
  4. namespace TelpoPush.Position.Worker.Handlers
  5. {
  6. public class KafkaSubscribe
  7. {
  8. private readonly ILogger<KafkaSubscribe> _logger;
  9. private readonly IHostEnvironment _env;
  10. private readonly IKafkaService _kafkaService;
  11. private readonly PositionProcess _positionProcess;
  12. public KafkaSubscribe(
  13. ILogger<KafkaSubscribe> logger, IHostEnvironment env,
  14. IKafkaService kafkaService,
  15. PositionProcess positionProcess)
  16. {
  17. _logger = logger;
  18. _env = env;
  19. _kafkaService = kafkaService;
  20. _positionProcess = positionProcess;
  21. }
  22. public async Task SubscribeAsync()
  23. {
  24. #if DEBUG
  25. _logger.LogInformation("11312");
  26. var temp = new Headers();
  27. string topic = "topic.push.position";
  28. //temp.Add(new Header("DataType", new byte[] { 0, 0, 0, 0 }));
  29. //temp.Add(new Header("AlarmType", new byte[] { 2, 0, 0, 0 }));
  30. //string psych = "{\"messageId\":\"1790941606816612864\",\"topic\":\"topic.push.third\",\"time\":\"2024-05-16 11:05:27\",\"data\":{\"imei\":\"861281060093147\",\"atteryLowId\":\"861281060093147664577f9\",\"info\":\"设备电量低于15%\"}}";
  31. //await _positionProcess.SendPosition(psych, topic, temp);
  32. //// await _kafkaService.SubscribeAsync(DoReceive, CancellationToken.None);
  33. #else
  34. LimitedConcurrencyLevelTaskScheduler lcts = new LimitedConcurrencyLevelTaskScheduler(5);
  35. TaskFactory factory = new TaskFactory(lcts);
  36. try
  37. {
  38. await factory.StartNew(async () =>
  39. {
  40. await _kafkaService.SubscribeAsync(DoReceive, CancellationToken.None);
  41. });
  42. }
  43. catch (Exception ex)
  44. {
  45. _logger.LogError($"Subscribe 处理Kafka数据发生异常 {ex.Message}|{ex.Source}|{ex.StackTrace}");
  46. }
  47. #endif
  48. }
  49. async void DoReceive(string topic, string message, Headers headers)
  50. {
  51. await _positionProcess.SendPosition(message, topic, headers);
  52. }
  53. }
  54. }