In the preface to the previous article, the ZooKeeper installation and running adopts the ZooKeeper Java API for connection. Java implements the creation of a class implementation interface Watcher. this interface specifies the public interface an event handler class must implement. A ZooKeeper client will get various events from the ZooKeepr server it connects. an application using such a client handles these events by registering a callback object with the client. the callback object is expected to be an instance of a class that implements Watcher interface. [java]/*** using actzookeeper. java * copyright (C) 2013 * created: cuiran 14:59:44 */package com. zoo. demo; import java. io. IOException; import java. util. concurrent. countDownLatch; import org. apache. commons. logging. log; import org. apache. commons. logging. logFactory; import org. apache. zookeeper. watchedEvent; import org. apache. zookeeper. watcher; import org. apache. zookeeper. zooKeeper; import org. apache. zookeeper. watcher. event. keeperState;/*** TODO * @ author cuiran * @ version TODO */public class implements actzookeeper implements Watcher {private static Log log Log = LogFactory. getLog (maid. class. getName (); // cache time private static final int SESSION_TIME = 2000; protected ZooKeeper zooKeeper; protected CountDownLatch countDownLatch = new CountDownLatch (1); public void connect (String hosts) throws IOException, InterruptedException {zooKeeper = new ZooKeeper (hosts, SESSION_TIME, this); countDownLatch. await ();}/* (non-Javadoc) * @ see org. apache. zookeeper. watcher # process (org. apache. zookeeper. watchedEvent) * // @ Override public void process (WatchedEvent event) {// TODO Auto-generated method stub if (event. getState () = KeeperState. syncConnected) {countDownLatch. countDown () ;}} public void close () throws InterruptedException {zooKeeper. close () ;}} then write some operations and the main method [java]/*** ZooKeeperOperator. java * copyright (C) 2013 * created: cuiran 15:03:40 */package com. zoo. demo; import java. util. arrays; import java. util. list; import org. apache. commons. logging. log; import org. apache. commons. logging. logFactory; import org. apache. zookeeper. createMode; import org. apache. zookeeper. keeperException; import org. apache. zookeeper. zooDefs. ids;/**** TODO * @ author cuiran * @ version TODO */public class ZooKeeperOperator extends actzookeeper {private static Log log Log = LogFactory. getLog (ZooKeeperOperator. class. getName ();/***** <B> function: </B> creates a persistent znode. for example, when/parent/child is created, there is no/parent. you cannot use * @ author cuiran * @ createDate 2013-01-16 15:08:38 * @ param path * @ param data * @ throws KeeperException * @ throws InterruptedException */public void create (String path, byte [] data) throws KeeperException, InterruptedException {/*** here, CreateMode is used to indicate that The znode will not be automatically deleted upon client's disconnect. * EPHEMERAL indicates that The znode will be deleted upon the client's disconnect. */this. zooKeeper. create (path, data, Ids. OPEN_ACL_UNSAFE, CreateMode. PERSISTENT);}/***** <B> function: </B> Get node information * @ author cuiran * @ createDate 2013-01-16 15:17:22 * @ param path * @ throws KeeperException * @ throws InterruptedException */public void getChild (String path) throws KeeperException, interruptedException {try {List <String> list = this. zooKeeper. getChildren (path, false); if (list. isEmpty () {log. debug (path + "no node in");} else {log. debug (path + "nodes exist"); for (String child: list) {log. debug ("node:" + child) ;}} catch (KeeperException. noNodeException e) {// TODO: handle exception throw e;} public byte [] getData (String path) throws KeeperException, InterruptedException {return this. zooKeeper. getData (path, false, null);} public static void main (String [] args) {try {ZooKeeperOperator zkoperator = new ZooKeeperOperator (); zkoperator. connect ("192.168.0.138"); byte [] data = new byte [] {'A', 'B', 'C', 'D'}; // zkoperator. create ("/root", null); // System. out. println (Arrays. toString (zkoperator. getData ("/root"); // zkoperator. create ("/root/child1", data); // System. out. println (Arrays. toString (zkoperator. getData ("/root/child1"); // zkoperator. create ("/root/child2", data); // System. out. println (Arrays. toString (zkoperator. getData ("/root/child2"); String zktest = "ZooKeeper Java API test"; zkoperator. create ("/root/child3", zktest. getBytes (); log. debug ("Get setting information:" + new String (zkoperator. getData ("/root/child3"); System. out. println ("Node Child Information:"); zkoperator. getChild ("/root"); zkoperator. close ();} catch (Exception e) {e. printStackTrace () ;}} final running result: [plain] 2013-01-16 15: 26: 04: INFO org. apache. zookeeper. zooKeeper-Client environment: user. name = Administrator www.2cto. com2013-01-16 15: 26: 04: INFO org. apache. zookeeper. zooKeeper-Client environment: user. home = C: \ Documents ents and Settings \ Administrator 2013-01-16 15: 26: 04: INFO org. apache. zookeeper. zooKeeper-Client environment: user. dir = D: \ workspace \ stormdemo1 2013-01-16 15: 26: 04: INFO org. apache. zookeeper. zooKeeper-Initiating client connection, connectString = 192.168.0.138 sessionTimeout = 2000 watcher = com. zoo. demo. zooKeeperOperator @ caw.2013-01-16 15: 26: 04: INFO org. apache. zookeeper. clientCnxn-Opening socket connection to server/192.168.0.138: 2181 2013-01-16 15: 26: 13: INFO org. apache. zookeeper. clientCnxn-Socket connection established to 192.168.0.138/192.168.0.138: 2181, initiating session 15: 26: 13: INFO org. apache. zookeeper. clientCnxn-Session establishment complete on server 192.168.0.138/192.168.0.138: 2181, sessionid = 0x13c3c5224cc0003, negotiated timeout = 4000 2013-01-16 15: 26: 13: DEBUG com. zoo. demo. zooKeeperOperator-get the configuration information: ZooKeeper's Java API test node Child Information: 2013-01-16 15: 26: 13: DEBUG com. zoo. demo. zooKeeperOperator-/root has nodes 2013-01-16 15: 26: 13: DEBUG com. zoo. demo. zooKeeperOperator-node: child1 2013-01-16 15: 26: 13: DEBUG com. zoo. demo. zooKeeperOperator-node: child3 2013-01-16 15: 26: 13: DEBUG com. zoo. demo. zooKeeperOperator-node: child2 2013-01-16 15: 26: 13: INFO org. apache. zookeeper. zooKeeper-Session: 0x13c3c5224cc0003 closed 2013-01-16 15: 26: 13: INFO org. apache. zookeeper. clientCnxn-EventThread shut down can be used to view the Chinese garbled characters: [java] String zktest = "ZooKeeper Java API test"; zkoperator. create ("/root/child5", zktest. getBytes ("UTF-8"); log. debug ("Get setting information:" + new String (zkoperator. getData ("/root/child5"), "UTF-8 "));