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.

KafkaSubscribe.cs 3.0KB

6 meses atrás
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using Confluent.Kafka;
  2. using Microsoft.Extensions.Options;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Reflection.PortableExecutable;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using TelpoPush.Common;
  11. using TelpoPush.Models.Config;
  12. using TelpoPush.Service.Mq.Kafka;
  13. namespace TelpoPush.Worker.ThirdSsl.Handlers
  14. {
  15. public class KafkaSubscribe
  16. {
  17. private readonly ILogger<KafkaSubscribe> _logger;
  18. private readonly IHostEnvironment _env;
  19. private readonly IKafkaService _kafkaService;
  20. private readonly ThirdSslProcess _thirdSslProcess;
  21. private readonly ServiceConfig _configService;
  22. public KafkaSubscribe(
  23. ILogger<KafkaSubscribe> logger, IHostEnvironment env,
  24. IKafkaService kafkaService,
  25. ThirdSslProcess thirdSslProcess)
  26. {
  27. _logger = logger;
  28. _env = env;
  29. _kafkaService = kafkaService;
  30. _thirdSslProcess = thirdSslProcess;
  31. }
  32. public async Task SubscribeAsync()
  33. {
  34. //string msg = "{\"MessageTime\":\"2024-05-13 10:29:09.407\",\"TopicName\":\"topic.push.telpo.zkheartrate\",\"MessageId\":\"1715567350000\",\"IMEI\":\"868437060014409\",\"Content\":\"{\\\"heartRates\\\":{\\\"imei\\\":\\\"868437060014409\\\",\\\"agencyid\\\":\\\"77d562ac-dd78-40ea-af6d-224ada9d70dc\\\",\\\"data\\\":[{\\\"time\\\":1715567340,\\\"value\\\":172,\\\"isAnomaly\\\":1},{\\\"time\\\":1715567342,\\\"value\\\":174,\\\"isAnomaly\\\":1},{\\\"time\\\":1715567344,\\\"value\\\":174,\\\"isAnomaly\\\":1},{\\\"time\\\":1715567346,\\\"value\\\":174,\\\"isAnomaly\\\":1},{\\\"time\\\":1715567348,\\\"value\\\":174,\\\"isAnomaly\\\":1},{\\\"time\\\":1715567350,\\\"value\\\":174,\\\"isAnomaly\\\":1}]}}\"}";
  35. //await _thirdSslProcess.SendSslThird(msg, "", null);
  36. //string msg2 = "{\"MessageTime\":\"2024-05-13 14:10:43.814\",\"TopicName\":\"topic.push.telpo.zkheartrate\",\"MessageId\":\"1715580644000\",\"IMEI\":\"868437060014409\",\"Content\":\"{\\\"anomalyCancel\\\":{\\\"imei\\\":\\\"868437060014409\\\",\\\"agencyid\\\":\\\"77d562ac-dd78-40ea-af6d-224ada9d70dc\\\",\\\"time\\\":1715580644}}\"}";
  37. //await _thirdSslProcess.SendSslThird(msg2, "", null);
  38. //await _kafkaService.SubscribeAsync(DoReceive, CancellationToken.None);
  39. LimitedConcurrencyLevelTaskScheduler lcts = new LimitedConcurrencyLevelTaskScheduler(5);
  40. TaskFactory factory = new TaskFactory(lcts);
  41. try
  42. {
  43. await factory.StartNew(() =>
  44. {
  45. _kafkaService.SubscribeAsync(DoReceive, CancellationToken.None);
  46. });
  47. }
  48. catch (Exception ex)
  49. {
  50. _logger.LogError($"Subscribe 处理Kafka数据发生异常 {ex.Message}|{ex.Source}|{ex.StackTrace}");
  51. }
  52. }
  53. async void DoReceive(string topic, string message, Headers headers)
  54. {
  55. await _thirdSslProcess.SendSslThird(message, topic, headers);
  56. }
  57. }
  58. }