pom.xml
4.0.0
Zookeeper
Zookeeper
1.0-SNAPSHOT
org.apache.hadoop
zookeeper
3.3.1
log4j
log4j
1.2.17
log4j 配置文件 log4j.properties
log4j.rootLogger=INFO, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%-5p: %c - %m%n
Zookeeper 实现类 BaseWatcher.java
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import java.util.concurrent.CountDownLatch;
/**
* Created by mao-siyu on 17-8-15.
*/
public class BaseWatcher implements Watcher {
public static CountDownLatch countDownLatch = new CountDownLatch(1);
@Override
public void process(WatchedEvent watchedEvent) {
if (Event.KeeperState.SyncConnected == watchedEvent.getState()) {
countDownLatch.countDown();
}
}
}
添加测试类 TestZookeeper.java
import org.apache.log4j.Logger;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
/**
* Created by mao-siyu on 17-8-15.
*/
public class TestZookeeper {
private static final Logger LOGGER = Logger.getLogger(TestZookeeper.class);
private static final String CONNECT_STRING = "10.32.156.68:2181";
private static final int SESSION_TIMEOUT = 5000;
public static void main(String[] args) throws Exception {
ZooKeeper zooKeeper = new ZooKeeper(CONNECT_STRING, SESSION_TIMEOUT, new BaseWatcher());
LOGGER.info("================ " + zooKeeper.getState());
BaseWatcher.countDownLatch.await();
}
}