You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

39 lines
1.0 KiB

  1. using Confluent.Kafka;
  2. using JT808.Gateway.Configs.Kafka;
  3. using JT808.Gateway.PubSub;
  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 class JT808MsgProducer : IJT808MsgProducer
  12. {
  13. public string TopicName { get; }
  14. private readonly IProducer<string, byte[]> producer;
  15. public JT808MsgProducer(
  16. IOptions<JT808MsgProducerConfig> producerConfigAccessor)
  17. {
  18. producer = new ProducerBuilder<string, byte[]>(producerConfigAccessor.Value).Build();
  19. TopicName = producerConfigAccessor.Value.TopicName;
  20. }
  21. public void Dispose()
  22. {
  23. producer.Dispose();
  24. }
  25. public async Task ProduceAsync(string terminalNo, byte[] data)
  26. {
  27. await producer.ProduceAsync(TopicName, new Message<string, byte[]>
  28. {
  29. Key = terminalNo,
  30. Value = data
  31. });
  32. }
  33. }
  34. }