How to check whether Kafka Server is running?

后端 未结 9 1563
无人及你
无人及你 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:30

    you can use below code to check for brokers available if server is running.

    import org.I0Itec.zkclient.ZkClient;
         public static boolean isBrokerRunning(){
            boolean flag = false;
            ZkClient zkClient = new ZkClient(endpoint.getZookeeperConnect(), 10000);//, kafka.utils.ZKStringSerializer$.MODULE$);
            if(zkClient!=null){
                int brokersCount = zkClient.countChildren(ZkUtils.BrokerIdsPath());
                if(brokersCount > 0){
                    logger.info("Following Broker(s) {} is/are available on Zookeeper.",zkClient.getChildren(ZkUtils.BrokerIdsPath()));
                    flag = true;    
                }
                else{
                    logger.error("ERROR:No Broker is available on Zookeeper.");
                }
                zkClient.close();
    
            }
            return flag;
        }
    

提交回复
热议问题