package cn.com.tenlion.systembase.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @ClassName: ApiPublishConsumer
 * @Description: api发布消费者
 * @Author: wanggeng
 * @Date: 2021/8/22 9:31 下午
 * @Version: 1.0
 */
@Component
public class KafKaPublishConsumer implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher applicationEventPublisher;
    private MongoTemplate mongoTemplate;

    public KafKaPublishConsumer(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    @KafkaListener(topics = {"C0001", "C0008"})
    public void dataT(ConsumerRecord<?, ?> record) {
        JSONObject publishObject = JSON.parseObject(record.value().toString());
        System.out.println("收到消息:" + publishObject.toJSONString());
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

}
