package cn.wise.sc.energy.power.plant.common.kafka.service;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;

/**
 * @description: Kafka业务类
 * @author: qh
 * @create: 2020-05-18 13:48
 */
@Component
public class IndicatorService {

  private Logger LOG = LoggerFactory.getLogger(IndicatorService.class);

  private final KafkaTemplate<Integer, String> kafkaTemplate;

  public IndicatorService(KafkaTemplate<Integer, String> kafkaTemplate) {
    this.kafkaTemplate = kafkaTemplate;
  }

  /**
   * 推送消息
   *
   * @param topic topic
   * @param data 消息体
   */
  public void sendMessage(String topic, String data) {
    LOG.info("kafka sendMessage start");
    ListenableFuture<SendResult<Integer, String>> future = kafkaTemplate.send(topic, data);
    future.addCallback(
        new ListenableFutureCallback<SendResult<Integer, String>>() {
          @Override
          public void onFailure(Throwable ex) {
            LOG.error("kafka sendMessage error, ex = {}, topic = {}, data = {}", ex, topic, data);
          }

          @Override
          public void onSuccess(SendResult<Integer, String> result) {
            LOG.info("kafka sendMessage success topic = {}, data = {}", topic, data);
          }
        });
    LOG.info("kafka sendMessage end");
  }

  @KafkaListener(topics = "thermalpower_plant_1", groupId = "topicGroupId")
  public void processMessage(ConsumerRecord<Integer, String> record) {
    LOG.info("kafka processMessage start");
    LOG.info("processMessage, topic = {}, msg = {}", record.topic(), record.value());

    // do something ...

    LOG.info("kafka processMessage end");
  }
}
