Apache kafka Spring集成和Kafka:如何基于消息头过滤消息

Apache kafka Spring集成和Kafka:如何基于消息头过滤消息,apache-kafka,spring-integration,spring-kafka,Apache Kafka,Spring Integration,Spring Kafka,我有一个基于这个问题的问题: 我想使用Spring Integration DSL按kafka消费者记录标题进行过滤 目前我有以下流程: @Bean IntegrationFlow readTicketsFlow(KafkaProperties kafkaProperties, ObjectMapper jacksonObjectMapper, EventService

我有一个基于这个问题的问题:

我想使用Spring Integration DSL按kafka消费者记录标题进行过滤

目前我有以下流程:

@Bean
IntegrationFlow readTicketsFlow(KafkaProperties kafkaProperties,
                                ObjectMapper jacksonObjectMapper,
                                EventService<Ticket> service) {
    Map<String, Object> consumerProperties = kafkaProperties.buildConsumerProperties();
    DefaultKafkaConsumerFactory<String, String> consumerFactory = new DefaultKafkaConsumerFactory<>(consumerProperties);

    return IntegrationFlows.from(
            Kafka.messageDrivenChannelAdapter(
                    consumerFactory, TICKET_TOPIC))
            .transform(fromJson(Ticket.class, new Jackson2JsonObjectMapper(jacksonObjectMapper)))
            .handle(service)
            .get();
}
如何在此流中注册org.springframework.kafka.listener.adapter.RecordFilterStrategy?

您只需将.filter元素添加到流中即可

.filter("!'bar'.equals(headers['foo'])")
将过滤掉忽略任何标题为foo等于bar的消息

注意,SpringKafka的RecordFilterStrategy与SpringIntegrationFilters相反

公共接口记录过滤器策略{ /** *如果应丢弃记录,则返回true。 *@param consumer记录该记录。 *@return true放弃。 */ 布尔过滤器消费记录消费记录; } 如果过滤器返回false,Spring集成过滤器将丢弃消息

编辑

也可以向通道适配器添加RecordFilterStrategy

返回积分流 .from kafka.messagedrivenchanneladapter消费者工厂,测试主题1 .recordFilterStrategyrecord->{ Header Header=record.headers.lastHeaderfoo; 返回标头!=null?新建Stringheader.value.EqualBar:false; } ...
我想知道卡夫卡的RecordFilterStrategyRecordFilterStrategyRecordFilterStrategy选项有什么问题;对忘了那件事: