检测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");
}
原文地址:https://www.cnblogs.com/jerryzh/p/12060035.html