Skip to content

Should fail bean registration when no method listeners are registered. #3869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chickenchickenlove opened this issue Apr 26, 2025 · 1 comment · Fixed by #3870
Closed

Should fail bean registration when no method listeners are registered. #3869

chickenchickenlove opened this issue Apr 26, 2025 · 1 comment · Fixed by #3870

Comments

@chickenchickenlove
Copy link
Contributor

chickenchickenlove commented Apr 26, 2025

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 IllegalStateException immediately when no methods annotated with @KafkaHandler are found

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.

@artembilan
Copy link
Member

I find this as a bug.
Thank you for digging so deep!

spring-builds pushed a commit that referenced this issue Apr 29, 2025
Fixes: #3869
Issue link: #3869

* Update spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessor.java

Signed-off-by: ChickenchickenLove <[email protected]>
Co-authored-by: Artem Bilan <[email protected]>
(cherry picked from commit 6682837)
spring-builds pushed a commit that referenced this issue Apr 29, 2025
Fixes: #3869
Issue link: #3869

* Update spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessor.java

Signed-off-by: ChickenchickenLove <[email protected]>
Co-authored-by: Artem Bilan <[email protected]>
(cherry picked from commit 6682837)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants