Search code examples
spring-kafka

Cannot get correct received topic when a message handled by @KafkaHandler(isDefault = true)


I want to get the topic for a message whose payload cannot be recognized by other KafkaHandler.

@KafkaListener(id="group-0", topics={"topic-0","topic-1"})
public class MultiHandler {

@KafkaHandler
public void fooMethod(Foo foo) {}

@KafkaHandler 
public void barMethod(Bar bar) {}

@KafkaHandler(isDefault = true)
public void unknownObjectMethod(
  @Header(KafkaHeaders.RECEIVED_TOPIC) String topic,
  @Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) String key,
  Object object) {}
}

My question is that if the payload cannot be handler by fooMethod() nor barMethod(), I cannot get the correct topic in unknownObjectMethod(). The value I get for the topic via the above code is the payload itself. I'm confused about why it happens. Please advise how can I get the correct topic in unknownOnjectMethod().

Thanks!


Solution

  • It's a bug; as a work around, use

    @KafkaHandler(isDefault = true)
    public void def(ConsumerRecord<?, ?> record) {
        ...
    }
    

    I opened an issue for it here.