package org.ts.ddcs.kafka;

import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.worker.DataProcessHolder;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author dylan
 */
@Component
public class KafkaMessageListener {
//    @KafkaListener(topics = "${rabbit.config.queue.logMessage}")
    public void listen(List<ConsumerRecord<String, String>> records) throws Exception {
        try {
            for (ConsumerRecord<String, String> cr : records) {
                LogHelper.info("kafka " + cr.topic() + " " + cr.partition() + " " + cr.value());
                JSONObject body = JSONObject.parseObject(cr.value());
                DataProcessHolder.newInstance().dataProcess.submitMapTask(body);
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }
}
