/* 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");
}

相关文章:

  • 2021-10-03
  • 2021-11-20
  • 2021-12-09
  • 2021-09-27
  • 2022-01-01
  • 2021-12-23
猜你喜欢
  • 2021-11-19
  • 2021-11-18
  • 2021-11-03
  • 2021-11-06
  • 2021-12-09
  • 2021-11-20
  • 2021-11-28
  • 2021-11-12
相关资源
相似解决方案