liveBook · Manning

StreamsBuilderFactoryBeanCustomizer

As explained in Kafka Streams in Action, Second Edition

StreamsBuilderFactoryBeanCustomizer is a Spring Kafka callback interface for customizing a StreamsBuilderFactoryBean.

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

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Defines the functional callback and two-bean customization chain, but defers broader KafkaStreams configuration details.
Examples
Uses a loan-processing application that logs RUNNING transitions, local thread metadata, active tasks, and assignments.
What it assumes
Assumes familiarity with Spring beans and @EnableKafkaStreams while showing the required KafkaStreamsCustomizer relationship.
What you can do after
Operate/configure it by wiring a KafkaStreamsCustomizer into a StreamsBuilderFactoryBeanCustomizer bean.
How it connects
Connects KafkaStreamsCustomizer, KafkaStreams, StateListener, StreamsBuilderFactoryBean, and @EnableKafkaStreams.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.