package org.dromara.web.task;

import jakarta.annotation.PostConstruct;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.dromara.web.utils.KafkaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SysKafkaData {

    @Value("${kafka.enabled}")
    private Boolean isEnable ;

    @Value("${kafka.topics}")
    private String topic;

    private final KafkaUtils kafkaUtils;

    @Autowired
    public SysKafkaData(KafkaUtils kafkaUtils) {
        this.kafkaUtils = kafkaUtils;
    }

    @PostConstruct
    public void init() {
        String listenerID = "kafka-listener-1";

        try {
            if(isEnable){
                //创建监听容器
                kafkaUtils.registerListenerContainer(listenerID, "test-consumer-group", new KafkaMessageConsumer(), KafkaMessageConsumer.class.getDeclaredMethod("consumerMessage", List.class), topic);
                kafkaUtils.setStateNormalListenerContainer(listenerID);
            }

        }catch(Exception e){
            e.printStackTrace();
        };

    }



}
