Java tutorial
/* * Copyright (C) 2014- now() The ZooKeeper-2015 Authors * * https://github.com/sdcuike * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.doctor.zookeeper.api; import java.io.IOException; import java.util.concurrent.CountDownLatch; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.Watcher.Event.KeeperState; import org.apache.zookeeper.ZooKeeper; /** * @author doctor * * @time 2015823 ?11:53:56 */ public class Practice implements Watcher { private static CountDownLatch countDownLatch = new CountDownLatch(1); @Override public void process(WatchedEvent event) { System.out.println("receive WatchedEvent " + event); if (KeeperState.SyncConnected == event.getState()) { countDownLatch.countDown(); } } public static void main(String[] args) throws IOException { String connectString = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183"; ZooKeeper zooKeeper = new ZooKeeper(connectString, 5000, new Practice()); System.out.println("zooKeeper connected state " + zooKeeper.getState()); try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } } }