liveBook · Manning

KafkaStreamsCustomizer

As explained in Kafka Streams in Action, Second Edition

KafkaStreamsCustomizer is a Spring Kafka callback component that customizes a KafkaStreams instance before it starts.

KafkaStreamsCustomizer is a Spring Kafka callback that exposes the KafkaStreams instance before startup, letting an application apply settings such as a StateListener. section 12.3

Use it when the managed Kafka Streams instance must provide runtime visibility:

  • Define a KafkaStreamsCustomizer bean and configure the KafkaStreams instance through a Java lambda. section 12.3
  • Attach that customizer to the StreamsBuilderFactoryBean with a StreamsBuilderFactoryBeanCustomizer bean. section 12.3
  • Register a StateListener that logs active task topic-partition assignments when the instance reaches RUNNING. section 12.3

Observing a running Kafka Streams instance

A KafkaStreamsCustomizer calls setStateListener and checks for KafkaStreams.State.RUNNING; metadataForLocalThreads() then supplies thread names and active tasks for logging. 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
    }
    });
}

Inspect the complete lambda-based customizer that installs the StateListener. section 12.3

Listing 12.20 Creating a customizer to apply KafkaStreams object settings
@Bean
StreamsBuilderFactoryBeanCustomizer kafkaStreamsCustomizer() {    #1
  return  streamsFactoryBean ->
 streamsFactoryBean
     .setKafkaStreamsCustomizer(getKafkaStreamsCustomizer());    #2
}

Follow the factory-bean bridge that applies the KafkaStreamsCustomizer. section 12.3

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Shows bean definitions, lambda configuration, factory-bean integration, and running-state task inspection without broader KafkaStreams internals.
Examples
Uses a StateListener to log running-state transitions, thread names, active tasks, and topic-partition assignments.
What it assumes
Assumes Spring configuration and Kafka Streams usage while demonstrating the required customizer and factory-bean wiring.
What you can do after
Operate/configure Spring Kafka Streams by injecting a callback that observes state and inspects local thread metadata before processing starts.
How it connects
Connects KafkaStreams with StateListener, StreamsBuilderFactoryBean, and StreamsBuilderFactoryBeanCustomizer.

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.