@PostConstruct marks an initialization method that Spring invokes after constructing a managed component and injecting its required dependencies. In a Kafka Streams application, it starts the configured stream runtime at the right lifecycle point. section 12.3
Starting Kafka Streams after injection
A KafkaStreamsContainer receives LoanApplicationTopology and KafkaStreamsConfiguration through its constructor. Spring then calls init(), which builds the topology, creates KafkaStreams with the application properties, registers a running-state listener, and starts processing. section 12.3
Listing 12.25 Creating and running Kafka Streams
@PostConstruct #1
public void init() {
Properties properties = appConfiguration.asProperties();
Topology topology = loanApplicationStream.topology(); #2
kafkaStreams = new KafkaStreams(topology, properties); #3
kafkaStreams.setStateListener((newState, oldState) -> {
if (newState == KafkaStreams.State.RUNNING) {
LOG.info("Streams now in running state");
kafkaStreams.metadataForLocalThreads().forEach(tm ->
LOG.info("{} assignments {}", tm.threadName(), tm.activeTasks()));
}
});
kafkaStreams.start(); #4
}
Inspect the complete initialization method, including topology construction, configuration conversion, state logging, and Kafka Streams startup. section 12.3
