Search code examples
springspring-bootapache-kafkakafka-producer-apispring-kafka

Enforce Spring Boot Application to Fail When Cannot Connect to Kafka


I want to make my spring boot application fail on start if it cannot connect to the kafka broker. My application is only publishing messages to topics. I added this line to my properties file but no luck so far spring.kafka.admin.fail-fast=true.


Solution

  • fail-fast will only work if there is at least one NewTopic bean in the context (so the admin will try to check if the topic exists and create it if not).

    @SpringBootApplication
    public class So55177700Application {
    
        public static void main(String[] args) {
            SpringApplication.run(So55177700Application.class, args);
        }
    
        @Bean
        public NewTopic topic() {
            return new NewTopic("so55177700", 1, (short) 1);
        }
    
    }
    
    Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
    2019-03-15 09:42:49.555 ERROR 41793 --- [           main] o.s.boot.SpringApplication               : Application run failed
    
    java.lang.IllegalStateException: Could not configure topics