Spring @KafkaListener execute and poll records after certain interval

后端 未结 3 711
感动是毒
感动是毒 2020-12-17 04:16

We wanted to consume the records after a certain interval (e.g. every 5 minutes). Consumer properties are standard:

@Bean
public KafkaListenerContainerFactor         


        
3条回答
  •  暖寄归人
    2020-12-17 04:18

    If you want to control rate at which Kafka consumer using Spring @KafkaListener, please autowire KafkaListenerEndpointRegistry bean use in following way and access the required MessageListenerContainer. thereafter, you can use the pause() and resume() functionalities to control the required behaviour.

    @Autowired
    private KafkaListenerEndpointRegistry listener;
    
    @Autowired
    private Map> getTopicListenerMap(){
        List ids = new ArrayList<>(listener.getListenerContainerIds());
        Map> topicListenerMap = new HashMap<>();
        for(String topic: topics){
            topicListenerMap.put(topic, new HashSet<>());
        }
        for(String key: ids){
            for (String topic : listener.getListenerContainer(key).getContainerProperties().getTopics()){
                topicListenerMap.get(topic).add(key);
            }
        }
        return topicListenerMap;
    }
    
    @KafkaListener(topics = "topic", containerFactory = "smsListener")
    public void listenWithHeaders(@Payload List messageList, @Header(KafkaHeaders.RECEIVED_PARTITION_ID) List partitionList,
                                  @Header(KafkaHeaders.OFFSET) List offsetList) {
        try{
            LOG.info("Received message count: "+(messageList!=null ? messageList.size(): 0)+", offset start: "+offsetList.get(0)+", end: "+offsetList.get(offsetList.size()-1));
            pauseIfRequired(topic);
            for(int i=0; i

提交回复
热议问题