import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
-import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
-import org.springframework.kafka.config.KafkaListenerContainerFactory;
-import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.listener.CommonContainerStoppingErrorHandler;
-import org.springframework.kafka.listener.CommonErrorHandler;
import java.util.function.Consumer;
};
}
- @Bean
- public KafkaListenerContainerFactory<?> batchFactory(
- ConsumerFactory<String, Long> consumerFactory,
- CommonErrorHandler errorHandler)
- {
- ConcurrentKafkaListenerContainerFactory<String, Long> factory =
- new ConcurrentKafkaListenerContainerFactory<>();
-
- factory.setConsumerFactory(consumerFactory);
- factory.setCommonErrorHandler(errorHandler);
- factory.setBatchListener(true);
-
- return factory;
- }
-
@Bean
public CommonContainerStoppingErrorHandler errorHandler()
{
import org.springframework.kafka.config.KafkaListenerEndpointRegistry;
import org.springframework.stereotype.Component;
-import java.util.List;
import java.util.function.Consumer;
id = "${consumer.client-id}",
idIsGroup = false,
topics = "${consumer.topic}",
- containerFactory = "batchFactory",
autoStartup = "false")
- public void receive(List<ConsumerRecord<K, V>> records)
+ public void receive(ConsumerRecord<K, V> record)
{
- // Do something with the data...
- log.info("{} - Received {} messages", id, records.size());
- for (ConsumerRecord<K, V> record : records)
- {
- log.info(
- "{} - {}: {}/{} - {}={}",
- id,
- record.offset(),
- record.topic(),
- record.partition(),
- record.key(),
- record.value()
- );
+ log.info(
+ "{} - {}: {}/{} - {}={}",
+ id,
+ record.offset(),
+ record.topic(),
+ record.partition(),
+ record.key(),
+ record.value()
+ );
- handler.accept(record);
+ handler.accept(record);
- consumed++;
- }
+ consumed++;
}