简体   繁体   English

Spring kafka在一个使用者中使用多个Message类型

[英]Spring kafka Consume multiple Message types in one consumer

I have multiple producers that can send multiple type of event to one kafka topic. 我有多个生产者可以将多种类型的事件发送到一个kafka主题。

And I have a consumer that must consume all type of message. 我有一个消费者必须消费所有类型的消息。 with different logic for every type of message. 对每种类型的消息都有不同的逻辑。

    @KafkaListener(topics = "test", containerFactory = "kafkaListenerContainerFactory")
public void handleEvent(Message<EventOne> event) {
    logger.info("event={}", event);
}

But in this case all messages come to this method not only EventOne 但在这种情况下,所有消息都不仅仅是EventOne

If I implement two method (for every type of message) then all messages come to only one method. 如果我实现两个方法(对于每种类型的消息),那么所有消息只有一个方法。

If i implement listener like this: 如果我实现这样的监听器:

    @KafkaListener(topics = "test", containerFactory = "kafkaListenerContainerFactory")
public void handleEvent(Message<?> event) {
    logger.info("event={}", event);
}

then I get exception: org.springframework.kafka.KafkaListenerEndpointContainer#0-0-kafka-listener-1] ERROR org.springframework.kafka.listener.LoggingErrorHandler - Error while processing: ConsumerRecord java.lang.IllegalArgumentException: Unrecognized Type: [null] 然后我得到异常:org.springframework.kafka.KafkaListenerEndpointContainer#0-0-kafka-listener-1] ERROR org.springframework.kafka.listener.LoggingErrorHandler - 处理时出错:ConsumerRecord java.lang.IllegalArgumentException:Unrecognized Type:[null ]

Please tell how I can implement multiple type consumer? 请告诉我如何实现多种类型的消费者?

I found the soultion and it is very simple. 我找到了灵魂,这很简单。 So, it is not clear from the question, but I use jsonMessageConverter like this: 所以,从问题中不清楚,但我使用jsonMessageConverter是这样的:

    @Bean
ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
    ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory());
    factory.setMessageConverter(new StringJsonMessageConverter());
    return factory;
}

The Jackson library by default do not adds class information for JSON therefore it can't guess what type of i want to deserialize. 默认情况下,Jackson库不会为JSON添加类信息,因此无法猜测我想要反序列化的类型。 Solution is the annotation 解决方案是注释

@JsonTypeInfo(use= JsonTypeInfo.Id.CLASS, include= JsonTypeInfo.As.PROPERTY, property="class")

that adds classinformation to json string. 将类信息添加到json字符串。 On the consumer side I just write the base class of my event 在消费者方面,我只写了我的活动的基类

    @KafkaListener(topics = "test", containerFactory = "kafkaListenerContainerFactory")
public void handleEvent(BasicEvent event) {

    if (event instanceof EventOne) {
        logger.info("type={EventOne}, event={}", event);
    } else {
        logger.info("type={EventTwo}, event={}", event);
    }
}

Hope this info helps someone. 希望这些信息有助于某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM