Manually starting Kafka Streams processors

Spring Cloud Stream Kafka Streams binder offers an abstraction called StreamsBuilderFactoryManager on top of the StreamsBuilderFactoryBean from Spring for Apache Kafka. This manager API is used for controlling the multiple StreamsBuilderFactoryBean per processor in a binder based application. Therefore, when using the binder, if you manually want to control the auto starting of the various StreamsBuilderFactoryBean objects in the application, you need to use StreamsBuilderFactoryManager. You can use the property spring.kafka.streams.auto-startup and set this to false in order to turn off auto starting of the processors. Then, in the application, you can use something as below to start the processors using StreamsBuilderFactoryManager.

@Bean
public ApplicationRunner runner(StreamsBuilderFactoryManager sbfm) {
    return args -> {
        sbfm.start();
    };
}

This feature is handy, when you want your application to start in the main thread and let Kafka Streams processors start separately. For example, when you have a large state store that needs to be restored, if the processors are started normally as is the default case, this may block your application to start. If you are using some sort of liveness probe mechanism (for example on Kubernetes), it may think that the application is down and attempt a restart. In order to correct this, you can set spring.kafka.streams.auto-startup to false and follow the approach above.

Keep in mind that, when using the Spring Cloud Stream binder, you are not directly dealing with StreamsBuilderFactoryBean from Spring for Apache Kafka, rather StreamsBuilderFactoryManager, as the StreamsBuilderFactoryBean objects are internally managed by the binder.