简体   繁体   English

如果kafka中不存在属性中的kafka主题名称,如何中断启动spring-boot应用程序?

[英]How can I interrupt startup spring-boot application if kafka topic name from properties does not exist in kafka?

I try to check Kafka topics on startup spring-boot application. 我尝试在启动spring-boot应用程序上检查Kafka主题。 I want to throw an exception and interrupt startup. 我想抛出异常并中断启动。 It is my config: 这是我的配置:

@Slf4j
@Configuration
public class KafkaTopicConfig implements ApplicationRunner {

    private final KafkaAdmin kafkaAdmin;
    private final TopicProperties topicProperties;

    public KafkaTopicConfig(KafkaAdmin kafkaAdmin, TopicProperties topicProperties) {
        this.kafkaAdmin = kafkaAdmin;
        this.topicProperties = topicProperties;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        AdminClient admin = AdminClient.create(kafkaAdmin.getConfig());

        ListTopicsResult listTopicsResult = admin.listTopics();
        listTopicsResult.names().whenComplete((existTopics, throwable) -> {
            log.info("TOPICS LOAD: {}", existTopics.size());
            topicProperties.getTopics().forEach((s, topic) -> {
                if (!existTopics.contains(topic))
                    throw new IllegalStateException("Topic with name: " + topic + " not found in kafka.");
            });
        });
    }
}

But after throws throw new IllegalStateException("Topic with name: " + topic + " not found in kafka."); 但抛出后抛出throw new IllegalStateException("Topic with name: " + topic + " not found in kafka."); this exception ignored and application continue works. 此异常被忽略,应用程序继续工作。

而不是ApplicationRunner ,使用SmartLifecycle autoStartup=true实现SmartLifecycle并将您的逻辑放在start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Kafka 模板 - 在 Spring 启动时连接到 Kafka 主题 - Spring Kafka Template - Connect to Kafka Topic on Spring Boot Startup 如何删除 Kafka(Spring-boot)中的标头? - How do I remove the header in a Kafka (Spring-boot)? 多个消费者如何在spring boot Kafka中收听多个主题? - How multiple consumer can listen to multiple topic in spring boot Kafka? 我们如何手动重置通过 Spring Boot Java 应用程序消耗的 kafka 主题的偏移量? - How can we manually reset the offset of a kafka topic which is consumed through a spring boot java application? 如何在 Spring Boot 中反序列化 Kafka 主题中的 Json 字符串 - How to deserialize Json string from Kafka topic in spring boot 如何从 Spring Boot 开始阅读 Kafka Topic - How to read Kafka Topic from beginning with Spring Boot 如何中止 Spring-Boot 启动? - How can I abort Spring-Boot startup? 如何在 application.properties 中设置 default-key/value-serde - Spring Boot Kafka Streams - How can I set default-key/value-serde in application.properties - Spring Boot Kafka Streams 如何停止 @Bean 注释的 Kstream kafka 消费者并继续运行 spring-boot 应用程序 - How to stop a @Bean annotated Kstream kafka consumer and continue to run spring-boot application Spring-Boot和Kafka:如何处理不可用的经纪人? - Spring-Boot and Kafka : How to handle broker not available?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM