Skip to content

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

Closed
@chickenchickenlove

Description

@chickenchickenlove

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions