Skip to content

Enabling lazy initialization breaks Kafka Listeners #2919

@PatrikScully

Description

@PatrikScully

I've been trying to optimize startup times by enabling spring.main.lazy-initialization. However, after making this change, I've encountered an issue with Kafka listeners. Specifically, the listeners stop receiving messages.

I attempted to explicitly set the @Lazy(false) annotation on the Kafka listener components, this hasn't resolved the problem. While it appears that the listener beans are created at startup (as expected with @Lazy(false)), they still do not receive any messages from Kafka.

I use Spring Boot 3.2.2 with Spring Cloud Stream 4.1.0 and I configure my Kafka listeners from the property yml like this:

spring:
  cloud:
    config:
      allowOverride: true
      overrideNone: true
      overrideSystemProperties: false
    function:
      definition: >-
        listenExample;
    stream:
      bindings:
        listenExample-in-0:
          destination: example-topic
          group: example-group
          consumer:
            use-native-decoding: true
      kafka:
        bindings:
          listenExample-in-0:
            consumer:
              enableDlq: true
              dlqName: example-error-topic
              dlqPartitions: 1

The component class:

@Component("listenExample")
@Lazy(false)
@RequiredArgsConstructor
@Transactional
public class ExampleListener implements Consumer<Message<String>> {

    @Override
    public void accept(Message<String> message) {
        // do something
    }
}

The expected behavior could be the possibility of loading these stream components in a lazy initialization environment using a config property, for example.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions