KafkaConsumerConfig.java 2.47 KB
package cn.com.glsx.kafka.config;

import cn.com.glsx.kafka.queue.consumer.BaseStationConsumer;
import cn.com.glsx.kafka.queue.consumer.DeviceStateConsumer;
import cn.com.glsx.kafka.queue.consumer.GpsAndBsConsumer;
import cn.com.glsx.kafka.queue.consumer.GpsConsumer;
import kafka.consumer.ConsumerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author fengzhi
 * @time 2020/12/9
 * @function kafka的消费者配置
 */
@Configuration
public class KafkaConsumerConfig {

    @Resource(name = "ddboxConsumerConfig")
    private ConsumerConfig ddboxConsumerConfig;

    @Resource(name = "gpsConsumerConfig")
    private ConsumerConfig gpsConsumerConfig;

    @Value("${consumer.nThreads}")
    private Integer nThreads;

    @Bean(initMethod = "init", destroyMethod = "destroy")
    GpsConsumer gpsConsumer(@Value("${mq.kafka.device.gps.topic}") String topic) {
        GpsConsumer gpsConsumer = new GpsConsumer();
        gpsConsumer.setnThreads(nThreads);
        gpsConsumer.setConsumerConfig(gpsConsumerConfig);
        gpsConsumer.setTopic(topic);
        return gpsConsumer;
    }

    @Bean(initMethod = "init", destroyMethod = "destroy")
    BaseStationConsumer baseStationConsumer(@Value("${mq.kafka.device.bs.topic}") String topic) {
        BaseStationConsumer baseStationConsumer = new BaseStationConsumer();
        baseStationConsumer.setnThreads(nThreads);
        baseStationConsumer.setConsumerConfig(gpsConsumerConfig);
        baseStationConsumer.setTopic(topic);
        return baseStationConsumer;
    }

    @Bean(initMethod = "init", destroyMethod = "destroy")
    GpsAndBsConsumer gpsAndBsConsumer(@Value("${mq.kafka.device.gpsandbs.topic}") String topic) {
        GpsAndBsConsumer gpsAndBsConsumer = new GpsAndBsConsumer();
        gpsAndBsConsumer.setnThreads(nThreads);
        gpsAndBsConsumer.setConsumerConfig(gpsConsumerConfig);
        gpsAndBsConsumer.setTopic(topic);
        return gpsAndBsConsumer;
    }

    @Bean(initMethod = "init", destroyMethod = "destroy")
    DeviceStateConsumer stateConsumer(@Value("${mq.kafka.device.state.topic}") String topic) {
        DeviceStateConsumer stateConsumer = new DeviceStateConsumer();
        stateConsumer.setnThreads(nThreads);
        stateConsumer.setConsumerConfig(gpsConsumerConfig);
        stateConsumer.setTopic(topic);
        return stateConsumer;
    }

}