Springboot集成kafka,如何批量消费消息?其他教程均是采用spring-kafka的方式,即:
通过在配置class中setBatchListener(true)来实现
@Bean
public KafkaListenerContainerFactory<?> batchFactory() {
ConcurrentKafkaListenerContainerFactory<Integer, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
factory.setBatchListener(true); // <<<<<<<<<<<<<<<<<<<<<<<<< return factory;
}
但是实际似乎可以是在application.properties中通过设置:spring.kafka.listener.type=BATCH/SINGLE
spring.kafka.consumer.max-poll-records=5 #设置每次批量消费的消息条数
消费者代码:
@KafkaListener(id="C1",
topicPartitions= {@TopicPartition(topic = "acktest",partitions= "1")},
groupId = "group1")
public void receiveOne(List<ConsumerRecord<?, ?>> record,Consumer consumer) throws Exception {
//System.out.println("The message one is :" + record.offset() + "-" + record.partition() + "-" + record.value());
System.out.println("message size is " + record.size());
}