批量监听器工厂:batchFactory
从1.1版本开始,你可以使用@KafkaListener 的方式批量拉取消息。自定义方式配置Kafka监听器工厂后,我们就可以通过工厂创建对应的批量监听器了。下面是一个配置样例:
方式一:
@Bean
public KafkaListenerContainerFactory<?, ?> batchFactory() {
ConcurrentKafkaListenerContainerFactory<Integer, String> factory =
new ConcurrentKafkaListenerContainerFactory<>();
// 注意,这里是一个获取配置的方法,需要自行实现。
factory.setConsumerFactory(consumerFactory());
factory.setBatchListener(true); // <<<<<<<<<<<<<<<<<<<<<<<<<
return factory;
}
方式二:
@Bean public KafkaListenerContainerFactory<?, ?> batchFactory(
KafkaProperties kafkaProperties) {
ConcurrentKafkaListenerContainerFactory<Integer, String> factory =
new ConcurrentKafkaListenerContainerFactory<>();
// 注意,这里使用的是yml里面的Consumer配置。
factory.setConsumerFactory(kafkaProperties.buildConsumerProperties());
factory.setBatchListener(true); // <<<<<<<<<<<<<<<<<<<<<<<<<
return factory;
}
批量监听器:batchListener
下面这个例子是接收批量消息的样例。
@KafkaListener(id = "list", topics = "myTopic", containerFactory = "batchFactory") public void listen(List<String> list) { ... }
获取header信息
topic, partition, offset等等header数据也可以在listener中获取到。下面是样例:
@KafkaListener(id = "list", topics = "myTopic", containerFactory = "batchFactory") public void listen(List<String> list, @Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) List<Integer> keys, @Header(KafkaHeaders.RECEIVED_PARTITION_ID) List<Integer> partitions, @Header(KafkaHeaders.RECEIVED_TOPIC) List<String> topics, @Header(KafkaHeaders.OFFSET) List<Long> offsets) { ... }
手动控制偏移量
Alternatively, you can receive a List
of Message<?>
objects with each offset and other details in each message, but it must be the only parameter (aside from optional Acknowledgment
, when using manual commits, and/or Consumer<?, ?>
parameters) defined on the method. The following example shows how to do so:
或者,在方法上你可以接收一个List<Message<?>)类型的参数【使用手动提交方式时,方法可以加上Acknowledgment
参数、Consumer<?, ?>参数[这个参数可选]。其他情况方法中只能有List参数。通过这个参数你可以操作每条数据的详细内容,包括是否提交偏移量等。】。下面是样例:
@KafkaListener(id = "listMsg", topics = "myTopic", containerFactory = "batchFactory") public void listen14(List<Message<?>> list) { ... } @KafkaListener(id = "listMsgAck", topics = "myTopic", containerFactory = "batchFactory") public void listen15(List<Message<?>> list, Acknowledgment ack) { ... } @KafkaListener(id = "listMsgAckConsumer", topics = "myTopic", containerFactory = "batchFactory") public void listen16(List<Message<?>> list, Acknowledgment ack, Consumer<?, ?> consumer) { ... }
消息转换
不包含以下情况:
如果BatchMessagingMessageConverter配置了RecordMessageConverter,你还可以给参数添加一个泛型自动进行数据转换。有关更多信息,请参见使用批处理侦听器的数据转换。
你还可以是使用List<ConsumerRecord<?,?>>作为批处理函数的参数【使用手动提交方式时,方法可以加上Acknowledgment
参数,其他情况方法中只能有List参数。】。下面是样例:
@KafkaListener(id = "listCRs", topics = "myTopic", containerFactory = "batchFactory") public void listen(List<ConsumerRecord<Integer, String>> list) { ... } @KafkaListener(id = "listCRsAck", topics = "myTopic", containerFactory = "batchFactory") public void listen(List<ConsumerRecord<Integer, String>> list, Acknowledgment ack) { ... }
获取poll()返回的原始对象ConsumerRecords
从2.2版本开始,listener可以接收ConsumerRecords<?, ?>作为返回对象,这个返回对象是poll()方法返回的。可以访问一些额外方法。下面是例子:
@KafkaListener(id = "pollResults", topics = "myTopic", containerFactory = "batchFactory") public void pollResults(ConsumerRecords<?, ?> records) { ... }