Filter messages before deserialization based on headers

喜欢而已 提交于 2020-03-24 00:00:35

问题


Sometimes messages can be filtered out before the deserialization based on header values . Are there any existing patterns for this scenario using spring kafka. I am thinking implementing similar to ErrorHandlingDeserializer in addition to delegate take filter predicate also as property. Any suggestions? thanks.


回答1:


Yes, you can use the same technique used by the ErrorHandlingDeserializer2 (which replaces the ErrorHandlingDeserializer) to return a "marker" object instead of doing the deserialization, then add a RecordFilterStrategy, that filters records with such objects, to the listener (container factory when using @KafkaListener or use a filtering adapter for an explicit listener).

EDIT

Spring Boot and adding a filter...

@Bean
public ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(
        ConcurrentKafkaListenerContainerFactoryConfigurer configurer,
        ConsumerFactory<Object, Object> kafkaConsumerFactory) {
    ConcurrentKafkaListenerContainerFactory<Object, Object> factory = new ConcurrentKafkaListenerContainerFactory<>();
    configurer.configure(factory, kafkaConsumerFactory);
    kafkaConsumerFactory.setsetRecordFilterStrategy(myFilter());
    return factory;
}


来源:https://stackoverflow.com/questions/53843328/filter-messages-before-deserialization-based-on-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!