Reputation: 506
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.
Upvotes: 2
Views: 661
Reputation: 5276
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:
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");
}
Upvotes: 3