Spring @KafkaListener为每个侦听器提供单独的过滤逻辑
我需要为侦听器工厂生成的每个侦听器定义一个自定义筛选策略。 目前,我正在使用Spring @KafkaListener为每个侦听器提供单独的过滤逻辑,spring,apache-kafka,spring-kafka,Spring,Apache Kafka,Spring Kafka,我需要为侦听器工厂生成的每个侦听器定义一个自定义筛选策略。 目前,我正在使用RecordFilterStrategy来实现这一点: @Bean ConcurrentKafkaListenerContainerFactory<String, GenericRecord> kafkaListenerContainerFactoryProject() { ConcurrentKafkaListenerContainerFactory<String, GenericRecord
RecordFilterStrategy
来实现这一点:
@Bean
ConcurrentKafkaListenerContainerFactory<String, GenericRecord> kafkaListenerContainerFactoryProject() {
ConcurrentKafkaListenerContainerFactory<String, GenericRecord> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
factory.setRecordFilterStrategy(new RecordFilterStrategy<String, GenericRecord>() {
@Override
public boolean filter(ConsumerRecord<String, GenericRecord> consumerRecord) {
return true;
}
});
return factory;
}
《春天卡夫卡》有什么工具可以做到这一点吗?或者我需要自己写这样的逻辑
提前谢谢 不,你没有。您只需要一组带有特定
RecordFilterStrategy
的ConcurrentKafkAlisterContainerFactory
bean。那么您的@KafkaListener
应该只指定它们基于哪个工厂:
/**
* The bean name of the {@link org.springframework.kafka.config.KafkaListenerContainerFactory}
* to use to create the message listener container responsible to serve this endpoint.
* <p>If not specified, the default container factory is used, if any.
* @return the container factory bean name.
*/
String containerFactory() default "";
/**
*{@link org.springframework.kafka.config.KafkaListenerContainerFactory}的bean名称
*用于创建负责服务于此端点的消息侦听器容器。
*如果未指定,则使用默认容器工厂(如果有)。
*@返回容器工厂bean名称。
*/
字符串containerFactory()默认为“”;
单个过滤器也可以根据记录.topic()
@ArtemBilan@GaryRussell更改其逻辑谢谢您的回答。但是为每个监听器定义新的ConcurrentKafkListenerContainerFactory
对我来说不是一个非常灵活的机制。实际上,我找到了一种为每个侦听器定义单独过滤器的方法,但它需要为每个创建的消息侦听器在setContainerCustomizer
中使用反射机制和侦听器id。而且,我不知道这样一条直截了当的道路会有什么后果。
/**
* The bean name of the {@link org.springframework.kafka.config.KafkaListenerContainerFactory}
* to use to create the message listener container responsible to serve this endpoint.
* <p>If not specified, the default container factory is used, if any.
* @return the container factory bean name.
*/
String containerFactory() default "";