liveBook · Manning

Predestroy annotation

As explained in Kafka Streams in Action, Second Edition

PreDestroy is a lifecycle annotation that marks a method to be invoked before a managed object is destroyed.

The `@PreDestroy` annotation marks a method that the Spring container invokes before tearing down a managed component. It provides a lifecycle hook for releasing resources during application shutdown. section 12.3

In a manually managed Kafka Streams application, the annotated method closes the `KafkaStreams` instance after the application has been started through an earlier lifecycle method. section 12.3

Closing Kafka Streams during shutdown

Annotate a teardown method and give Kafka Streams time to close cleanly: `@PreDestroy public void tearDown() { kafkaStreams.close(Duration.ofSeconds(10)); }`.

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains the lifecycle role and demonstrates Kafka Streams closure, but does not detail annotation processing internals.
Examples
Uses a `tearDown` method that closes Kafka Streams with a ten-second duration.
What it assumes
Assumes familiarity with Spring-managed components and Kafka Streams objects, while illustrating the shutdown callback directly.
What you can do after
Operate Kafka Streams through a managed shutdown hook that closes the running instance cleanly.
How it connects
Connects managed components with Spring lifecycle callbacks, Kafka Streams, and resource cleanup.

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.