com.github.shoothzj.test.zookeeper.ZkUtil Maven / Gradle / Ivy
The newest version!
package com.github.shoothzj.test.zookeeper;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class ZkUtil {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ZkUtil.class);
public static List getZkStats(String host, int port) throws Exception {
Socket sock = new Socket(host, port);
BufferedReader reader = null;
try {
OutputStream outStream = sock.getOutputStream();
outStream.write("stat".getBytes(StandardCharsets.UTF_8));
outStream.flush();
reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
List res = new ArrayList<>();
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
res.add(line);
}
return res;
} finally {
sock.close();
if (reader != null) {
reader.close();
}
}
}
}