Skip to content

Enabling lazy initialization breaks Kafka Streams #3065

@mdaepp

Description

@mdaepp

Describe the issue
Kafka Streams are not initialized when spring.main.lazy-initialization is set to true.
The same issue has already been fixed for regular Kafka listeners (see Additional Context below).

To remedy the problem, the following auto-configuration classes should be annotated with @Lazy(false):

  • org.springframework.cloud.stream.binder.kafka.streams.ExtendedBindingHandlerMappingsProviderAutoConfiguration
  • org.springframework.cloud.stream.binder.kafka.streams.function.KafkaStreamsFunctionAutoConfiguration.

Workaround
The following workaround may be applied in a @Configuration class (which must be annotated with @Lazy(false)):

  @Bean
  public BeanFactoryPostProcessor ensureEagerKafkaStreamsInitializationPostProcessor() {
    return beanFactory -> {
      for (String beanDefinitionName : beanFactory.getBeanDefinitionNames()) {
        if (beanDefinitionName.startsWith("kafkaStreamsExtendedPropertiesDefaultMappingsProvider") ||
            beanDefinitionName.startsWith("kafkaStreamsFunctionBeanPostProcessor") ||
            beanDefinitionName.startsWith("kafkaStreamsFunctionProcessorInvoker")) {
          beanFactory.getBeanDefinition(beanDefinitionName).setLazyInit(false);
        }
      }
    };
  }

Version of the framework
Spring Boot 3.4.1
Spring Cloud Stream 4.2.0

Expected behavior
Kafka Streams are eagerly intialized even if spring.main.lazy-initialization is set to true.

Additional context
This issue has already been already fixed for regular listeners with #2919.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions