How to check whether Kafka Server is running?

后端 未结 9 1552
无人及你
无人及你 2020-12-08 03:46

I want to ensure whether kafka server is running or not before starting production and consumption jobs. It is in windows environment and here\'s my kafka server\'s code in

9条回答
  •  春和景丽
    2020-12-08 04:25

    I used the AdminClient api.

    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 names = topics.names().get();
        if (names.isEmpty())
        {
            // case: if no topic found.
        }
        return true;
    }
    catch (InterruptedException | ExecutionException e)
    {
        // Kafka is not available
    }
    

提交回复
热议问题