-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Expected Behavior
Should fail bean registration when no method listeners are registered.
Current Behavior
@Component
@KafkaListener
class MyListener {
// There is no @KafkaHandler.
public void listen1(String message) { ... }
// There is no @KafkaHandler.
public void listen2(String message) { ... }
}If a user registers a @KafkaListener as bean but there are no methods annotated with @KafkaHandler,
spring-kafka creates MultiMethodKafkaListenerEndpoint without methods silently.
Later, After KafkaConsumer polls records, spring-kafka realizes that there are no available methods and just throws a runtime error.
In this case, user only becomes aware of the problem at runtime, even though the application is already malfunctioning.
I think It would be better to throw an IllegalStateException immediately when no methods annotated with @KafkaHandler are found so that the KafkaListener bean fails to be registered, following the fast-fail principle.
- As-Is
- There are no actions at all
- To-Be
- Throws an
IllegalStateExceptionimmediately when no methods annotated with @KafkaHandler are found
- Throws an
Context
When I was writing test code for my PR, I encountered a situation where I only realized at runtime that the listener had not been properly registered.
Since I was not very familiar with this behavior, it took me some time to debug the issue.
I believe that new users of Spring Kafka might experience a similar problem.
If a KafkaListener is declared at the class level but no listener methods can be found,
I think it would be better to fail the Spring Boot application startup from a fast-fail perspective.