檢測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