Class Level @KafkaListener
Selection Criteria
Found class will be selected for further processing only if:
It has @KafkaHandler annotation on one of its methods
When you use @KafkaListener at the class-level, you must specify @KafkaHandler at the method level. If no @KafkaHandler on any methods of this class or its sub-classes, the framework will reject such a configuration.
Spring Documentation: Class level KafkaListener
Registration process
Operations registration process depends on @SendTo annotation usage.
With @SendTo
See Receive & Reply Operations
Without @SendTo
When listener is registered without @SendTo annotation, one-to-one operation will be created for each @KafkaHandler method.
@KafkaListener(topics = {"messages.blue"})
@KafkaListener(topics = {"messages.green", "messages.yellow"}, groupId = "group-id")
public class MultiMessagesListener {
@KafkaHandler
public void listen(Object message) {}
}The next operations will be created:
- receive
Objectfrommessages.blue - receive
Objectfrommessages.greenwith groupIdgroup-id - receive
Objectfrommessages.yellowwith groupIdgroup-id
When class will be ignored?
Here are the examples of classes which will be ignored:
Class without @KafkaHandler annotations
This class will be ignored because it has no @KafkaHandler annotation on any of its methods.
@KafkaListener(topics = {"messages.blue"})
@KafkaListener(topics = {"messages.green"})
public class MultiMessagesListener {
public void listen(Object message) {}
}Class with empty @KafkaListener annotation
This class will be ignored because it has empty @KafkaListener annotation.
@KafkaListener
public class MultiMessagesListener {
@KafkaHandler
public void listen(Object message) {}
}Class with unclear @KafkaHandler
When class contains @KafkaHandler with unclear message schema
@KafkaListener(topics = {"messages.green"})
public class MultiMessagesListener {
// (1)
@KafkaHandler
public void stringMessage() {}
// (2)
@KafkaHandler
public void integerMessage(Object obj1, Object obj2) {}
}- (1) - handler doesn't have any arguments
- (2) - handler has one or more arguments, so it's impossible to understand which argument is message and header, for example