Skip to content

Instantly share code, notes, and snippets.

@itzg
Last active March 28, 2022 17:06
Show Gist options
  • Save itzg/7b18adb6af017c65d0eec5ddbacca0ba to your computer and use it in GitHub Desktop.
Save itzg/7b18adb6af017c65d0eec5ddbacca0ba to your computer and use it in GitHub Desktop.
Spring Boot health indicator that leverage auto-configured Kafka Streams to expose the streams readiness
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.KafkaStreams.State;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.kafka.core.StreamsBuilderFactoryBean;
import org.springframework.stereotype.Component;
@Component
public class KafkaStreamsHealthIndicator implements HealthIndicator {
private final StreamsBuilderFactoryBean defaultKafkaStreamsBuilder;
@Autowired
public KafkaStreamsHealthIndicator(
@Qualifier("defaultKafkaStreamsBuilder") StreamsBuilderFactoryBean defaultKafkaStreamsBuilder) {
this.defaultKafkaStreamsBuilder = defaultKafkaStreamsBuilder;
}
@Override
public Health health() {
final KafkaStreams kafkaStreams = defaultKafkaStreamsBuilder.getKafkaStreams();
if (kafkaStreams == null) {
return Health.down().build();
}
else {
final State state = kafkaStreams.state();
if (state == State.RUNNING) {
return Health.up().build();
}
else {
return Health.down()
.withDetail("state", state)
.build();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment