StreamsBuilderFactoryBeanCustomizer is a functional Spring Kafka callback interface for configuring the KafkaStreams instance managed by a StreamsBuilderFactoryBean. In a loan-processing application, it can apply a KafkaStreamsCustomizer before startup. section 12.3
Use the customizer when the application needs to:
- Access the underlying KafkaStreams instance before it starts. section 12.3
- Install a StateListener that reacts when the stream reaches RUNNING. section 12.3
- Log active task and topic-partition assignment information through metadataForLocalThreads(). section 12.3
Listing 12.19 Kafka Streams customizer for setting a StateListener
@Bean
KafkaStreamsCustomizer getKafkaStreamsCustomizer() {
return kafkaStreams ->
kafkaStreams.setStateListener((newState, oldState) -> { #1
if (newState == KafkaStreams.State.RUNNING) {
LOG.info("Streams now in running state");
kafkaStreams.metadataForLocalThreads()
.forEach(tm -> LOG.info("{} active task info: {}",
tm.threadName(), tm.activeTasks())); #2
}
});
}
Define a KafkaStreamsCustomizer lambda to set a StateListener and inspect active tasks when Kafka Streams enters RUNNING. section 12.3
Listing 12.20 Creating a customizer to apply KafkaStreams object settings
@Bean
StreamsBuilderFactoryBeanCustomizer kafkaStreamsCustomizer() { #1
return streamsFactoryBean ->
streamsFactoryBean
.setKafkaStreamsCustomizer(getKafkaStreamsCustomizer()); #2
}
Expose a StreamsBuilderFactoryBeanCustomizer bean that applies the KafkaStreamsCustomizer to the factory bean. section 12.3
