We wanted to consume the records after a certain interval (e.g. every 5 minutes). Consumer properties are standard:
@Bean
public KafkaListenerContainerFactor
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