Deserializing different JSON payload from same Kafka topic with Spring Kafka

后端 未结 1 690
猫巷女王i
猫巷女王i 2021-01-06 22:40

I\'m trying to deserialize different JSON payloads from the same Kafka topic. The other questions asked here, guided me to a first attempt, but I was not able to get it runn

1条回答
  •  失恋的感觉
    2021-01-06 22:59

    You can't do that; you have 2 different listener containers with listeners that expect different objects.

    For multiple listener methods that receive different types, you need to use @KafkaListener at the class level and @KafkaHandler at the method level.

    See @KafkaListener on a Class.

    When using @KafkaListener at the class-level, you specify @KafkaHandler at the method level. When messages are delivered, the converted message payload type is used to determine which method to call.

    @KafkaListener(id = "multi", topics = "myTopic")
    static class MultiListenerBean {
    
        @KafkaHandler
        public void listen(String foo) {
            ...
        }
    
        @KafkaHandler
        public void listen(Integer bar) {
            ...
        }
    
        @KafkaHandler(isDefault = true`)
        public void listenDefault(Object object) {
            ...
        }
    
    }
    

    The default method is optional and is used for unknown payload types.

    But this only works with a smart deserializer (that knows how to convert to different payloads).

    Or, you can add a RecordFilterStrategy to the listener container factory to skip the other records in each listener.

    0 讨论(0)
提交回复
热议问题