No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

57 líneas
1.6 KiB

  1. using Confluent.Kafka;
  2. using JT808.Gateway.Configs.Kafka;
  3. using JT808.Gateway.Abstractions;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace JT808.Gateway.Kafka
  10. {
  11. public sealed class JT808SessionProducer : IJT808SessionProducer
  12. {
  13. private bool disposed = false;
  14. public string TopicName { get; }
  15. private readonly IProducer<string, string> producer;
  16. public JT808SessionProducer(
  17. IOptions<JT808SessionProducerConfig> producerConfigAccessor)
  18. {
  19. producer = new ProducerBuilder<string, string>(producerConfigAccessor.Value).Build();
  20. TopicName = producerConfigAccessor.Value.TopicName;
  21. }
  22. public async ValueTask ProduceAsync(string notice,string terminalNo)
  23. {
  24. if (disposed) return;
  25. await producer.ProduceAsync(TopicName, new Message<string, string>
  26. {
  27. Key = notice,
  28. Value = terminalNo
  29. });
  30. }
  31. private void Dispose(bool disposing)
  32. {
  33. if (disposed) return;
  34. if (disposing)
  35. {
  36. producer.Dispose();
  37. }
  38. disposed = true;
  39. }
  40. ~JT808SessionProducer()
  41. {
  42. Dispose(false);
  43. }
  44. public void Dispose()
  45. {
  46. //必须为true
  47. Dispose(true);
  48. //通知垃圾回收机制不再调用终结器(析构器)
  49. GC.SuppressFinalize(this);
  50. }
  51. }
  52. }