Search code examples
spring-kafkaspring-cloud-stream-binder-kafka

How to Grab Auto-Generated KafkaTemplate in Spring Cloud Stream?


A few examples for implementing HealthIndicator needs a KafkaTemplate. I don't actually manually create a KafkaTemplate but the HealthIndicator requires one. Is there a way to automatically grab the created KafkaTemplate (that uses the application.yml configurations)? This is versus manually creating duplicated configurations that already exist in the application.yml in a newly created consumerFactory.


Solution

  • See this answer.

    (S)he wanted to get access to the template's ProducerFactory but you can use the same technique to just get a reference to the template.

    That said, the binder comes with its own health indicator.

    https://github.com/spring-cloud/spring-cloud-stream-binder-kafka/blob/a7299df63f495af3a798637551c2179c947af9cf/spring-cloud-stream-binder-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/KafkaBinderHealthIndicator.java#L52