liveBook · Manning

@PostConstruct

As explained in Kafka Streams in Action, Second Edition

PostConstruct is a lifecycle annotation that marks a method to be invoked after an object has been constructed and its dependencies injected.

@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

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Defines the invocation point and demonstrates startup internals, while deferring broader lifecycle choices to application requirements.
Examples
Uses KafkaStreamsContainer.init to create, monitor, and start a KafkaStreams instance.
What it assumes
Assumes Spring-managed components, constructor injection, Kafka Streams topology, and application configuration.
What you can do after
Operate a Kafka Streams application with automatic startup and coordinated shutdown.
How it connects
Connects dependency injection, Kafka Streams, topology construction, and @PreDestroy shutdown.

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.