简体   繁体   中英

How to overload a KafkaListener method in Spring Boot

I have been using Spring Kafka for sometime now but only recently have run across a need to "overload" a single Kafka topic. Consider the below code.

@KafkaListener(topics = "my-topic")
public void handleAsString(@Payload @Valid String message) {
    ...
}

@KafkaListener(topics = "my-topic")
public void handleAsUser(@Payload @Valid User user) {
    ...
}

Is this best achieved using @KafkaHandler? I have only been successful in both methods executing when the topic is received but the desire is to treat this like a standard overloaded method.

Generally, a golden thumb of rule with Kafka is to use one topic for one type of data stream. So in case you have different types of data coming in through the same stream maybe you'd want to rethink the approach and split the different types of messages to different Kafka Topics and write separate consumers for them.

If you had to do it in one topic, I'd say receive the message as string and then deserialize it based on certain criteria like existence of a key per say.

Assume two messages:

  • "Hey There!" (String message)
  • "{"id": 1, "name": \\"john\\", "age": 26}" (Serialized User Message)

Below is a sample

// maybe make a bean of this
private final ObjectMapper mapper = new ObjectMapper();

@KafkaListener(topics = "my-topic")
public void handleAsUser(@Payload String message) throws IOException {

    if (message.contains("age")){

        User userMessage = mapper.readValue(message, User.class);

        // do something when a user message is received
        return;          
    }
    System.out.println("The message is of type String");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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