检测Kafka服务器是否正在运行


/* from: https://stackoverflow.com/questions/37920923/how-to-check-whether-kafka-server-is-running */

 

使用Kafka的AdminClient

 

Properties properties = new Properties();
properties.put("bootstrap.servers", "localhost:9092");
properties.put("connections.max.idle.ms", 10000);
properties.put("request.timeout.ms", 5000);
try (AdminClient client = KafkaAdminClient.create(properties))
{
    ListTopicsResult topics = client.listTopics();
    Set<String> names = topics.names().get();
    System.out.println("connect to kafka cluster success");
    if (names.isEmpty())
    {
        // case: if no topic found.
    }
//            return true;
}
catch (InterruptedException | ExecutionException e)
{
    // Kafka is not available
    System.out.println("connect to kafka cluster failed");
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM