zookeeper 创建连接 (zookeeper API四种创建连接)
/**
* zookeeper 创建连接有四种方式
*
* @author Administrator
*
*/
public class Create implements Watcher {
CountDownLatch CountDownLatch = new CountDownLatch(1);
/**
* 1. ZooKeeper.ZooKeeper(String connectString, int sessionTimeout, Watcher
* watcher)
*
* @throws IOException
* @throws InterruptedException
*/
@Test
public void testCreate1() throws IOException, InterruptedException {
ZooKeeper zk = new ZooKeeper(Connection.CONNECTION, 5000, new Create());
System.out.println(zk);
CountDownLatch.await();
}
/**
* 2. ZooKeeper(String connectString, int sessionTimeout, Watcher watcher,
* boolean canBeReadOnly) watcher)
*
* @throws IOException
* @throws InterruptedException
*/
@Test
public void testCreate2() throws IOException, InterruptedException {
ZooKeeper zk = new ZooKeeper(Connection.CONNECTION, 5000, new Create(), true);
System.out.println(zk);
CountDownLatch.await();
}
/**
* 3. ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, long
* sessionId, byte[] sessionPasswd)
*
* @throws IOException
* @throws InterruptedException
*/
@Test
public void testCreate3() throws IOException, InterruptedException {
ZooKeeper zk = new ZooKeeper(Connection.CONNECTION, 5000, new Create(), 111l, "root".getBytes());
System.out.println(zk);
CountDownLatch.await();
}
/**
* 4. ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, long
* sessionId, byte[] sessionPasswd, boolean canBeReadOnly)
*
* @throws IOException
* @throws InterruptedException
*/
@Test
public void testCreate4() throws IOException, InterruptedException {
ZooKeeper zk = new ZooKeeper(Connection.CONNECTION, 5000, new Create(), 111l, "root".getBytes(), true);
System.out.println(zk);
CountDownLatch.await();
}
@Override
public void process(WatchedEvent event) {
System.out.println("processing.............");
System.out.println(event);
CountDownLatch.countDown();
}
}