1.zookeeper的watches
1) can be understood as a monitor, all the read operations in ZooKeeper - the getData () , the getChildren () and EXISTS () - can be set to monitor selected
Range 2) watches a
Create an event, delete event, change event, child event
3) Delete watches
We can delete registered on znode watch by calling removeWatches
2.watches sample (java)
public class ZKDemo implements Watcher {
private static CountDownLatch countDownLatch = new CountDownLatch(1);
private static ZooKeeper zooKeeper;
public static void main(String[] args) throws Exception {
connetionZK("");
close();
}
public static void connetionZK(String zk) throws IOException, KeeperException, InterruptedException {
//zookeeper的ip:端口
String path = "192.168.10.150:2181";
zooKeeper = new ZooKeeper(path, 20*1000, new ZKDemo());
//创建节点并给值
// createZnode(zooKeeper, "/test", "test1".getBytes());
//子节点列表更改
// createZnodeChild(zooKeeper, "/test", "test2".getBytes());
// modify the node data
CreateZnodeAndChanged // (ZooKeeper, "/ the Test", "Test3" .getBytes ());
// create and delete nodes
// createZnodeAndDelete (ZooKeeper, "/ the Test", "Test4" .getBytes ());
// data node under delete watch
// createZnodeAndRMDataWatch (ZooKeeper, "/ the Test", "test5" .getBytes ());
// delete watch the next child node
// createZnodeAndRMChildWatch (zooKeeper, "/ test ", "test6" .getBytes ());
countDownLatch.await (); // program continues blocking
}
/ **
* Create a node value and a
* @param ZooKeeper
* @param path
* @param Data
* /
public static void createZnode (the ZooKeeper ZooKeeper, String path, byte [] Data) {
// create a path node, the node value, Watch, type
try {
zooKeeper.exists (path, true); // default watcher, here refers to the default setting when the ZooKeeper new new (path, * 20 is 1000, new new ZKDemo ()) Watcher
zooKeeper.create (path, Data, ZooDefs.Ids. OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); //
} the catch (KeeperException E) {
e.printStackTrace ();
} the catch (InterruptedException E) {
e.printStackTrace ();
}
}
/ **
* list of child nodes changes
* @param ZooKeeper
* path @param
* @param Data
* /
public static void createZnodeChild (the ZooKeeper ZooKeeper, String path, byte [] Data) {
// create a path node, the node value, Watch, type
try {
zooKeeper.exists (path, true); // default watcher, here refers to the default setting when the ZooKeeper new new (path, * 20 is 1000, new new ZKDemo ()) Watcher
zooKeeper.create (path, Data, ZooDefs.Ids. OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); //
zooKeeper.getChildren (path, to true);
zooKeeper.create (path + "/ the child1", "Child" .getBytes (), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); //
} the catch (KeeperException E) {
e.printStackTrace ();
} the catch (InterruptedException E) {
e.printStackTrace ();
}
}
/ **
* modify data node
* @param ZooKeeper
* @param path
* @param data
* /
static void createZnodeAndChanged public (the ZooKeeper ZooKeeper, String path, byte [] Data) {
// create a path node, the node value, Watch, the type of
the try {
zooKeeper.exists (path, to true); // default Watcher, where the default It refers to new ZooKeeper (path, 20 * 1000 , new ZKDemo ()) watcher set when
zooKeeper.create (path, Data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); //
zooKeeper.exists (path, to true);
ZooKeeper .setData (path, "the Changed" .getBytes (), -1);
} the catch (KeeperException E) {
e.printStackTrace ();
} the catch (InterruptedException E) {
e.printStackTrace ();
}
}
/ **
* Create and delete nodes
* @param ZooKeeper
* @param path
@Param Data *
* /
public static void createZnodeAndDelete (the ZooKeeper ZooKeeper, String path, byte [] Data) {
// create a path node, the node value, Watch, the type of
the try {
zooKeeper.exists (path, to true); // Use default the watcher, here refers to the default setting when the watcher the ZooKeeper new new (path, * 20 is 1000, new new ZKDemo ())
zooKeeper.create (path, Data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); //
zooKeeper.exists ( path, to true);
zooKeeper.delete (path, -1);
} the catch (KeeperException E) {
e.printStackTrace ();
} the catch (InterruptedException E) {
e.printStackTrace ();
}
}
/ **
* data node under delete Watch
* @param ZooKeeper
@Param path *
* @param Data
* /
public static void createZnodeAndRMDataWatch (the ZooKeeper ZooKeeper, String path, byte [] Data) {
// create a path node, the node value, Watch, the type of
the try {
zooKeeper.exists (path, to true); // default watcher, here refers to the default setting when the watcher the ZooKeeper new new (path, * 20 is 1000, new new ZKDemo ())
zooKeeper.create (path, Data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); //
zooKeeper.setData (path, "Watch1" .getBytes (), -1);
zKDemo zkDemo new new zKDemo = ();
byte [] bytes = zooKeeper.getData (path, zkDemo, new new Stat ());
System.out.println (new new String (bytes));
zooKeeper.removeWatches (path, zkDemo, WatcherType.Data, to true);
} the catch (KeeperException E) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 子节点下删除watch
* @param zooKeeper
* @param path
* @param data
*/
public static void createZnodeAndRMChildWatch(ZooKeeper zooKeeper,String path,byte [] data) {
//创建节点 路径,节点值,Watch,类型
try {
ZKDemo zkDemo = new ZKDemo();
zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);//
zooKeeper.create(path+"/child2", "child2".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);//
zooKeeper.setData(path, data, -1);
zooKeeper.getChildren(path, zkDemo);
zooKeeper.removeWatches(path, zkDemo, WatcherType.Children, true);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 关闭zk
*/
public static void close() {
try {
if (zooKeeper != null) {
zooKeeper.close();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Process void public (WatchedEvent Event) {
countDownLatch.countDown ();
IF (Event.KeeperState.SyncConnected event.getState == ()) {// connected state
System.out.println ( "connected state");
} the else IF ( Event.KeeperState.Disconnected == event.getState ()) {// OFF state
System.out.println ( "OFF ZooKeeper");
} the else IF (Event.KeeperState.AuthFailed event.getState == ()) { authentication failed //
System.out.println ( "authentication failure");
} the else IF (Event.KeeperState.ConnectedReadOnly event.getState == ()) {// read-only connection
System.out.println ( "connection read-only ");
} the else IF (Event.KeeperState.SaslAuthenticated event.getState == ()) {// perform licensing rights sasl ZooKeeper operation
System.out.println (" ZooKeeper operations performed sasl authorization authority ");
} else if (Event.KeeperState.Expired == event.getState()) {//服务器失效
System.out.println("服务器失效");
} else if (Event.KeeperState.Closed == event.getState()) {//客户端关闭
System.out.println("客户端关闭");
} else {
System.out.println("未知");
}
System.out.println("**************************************");
if (Event.EventType.None == event.getType()) {
System.out.println(event.getType());
} else if (Event.EventType.NodeCreated == event.getType()) {
System.out.println(event.getType());
} else if (Event.EventType.NodeDeleted == event.getType()) {
System.out.println(event.getType());
} else if (Event.EventType.NodeDataChanged == event.getType()) {
System.out.println(event.getType());
} else if (Event.EventType.NodeChildrenChanged == event.getType()) {
System.out.println(event.getType());
} else if (Event.EventType.DataWatchRemoved == event.getType()) {
System.out.println(event.getType());
} else if (Event.EventType.ChildWatchRemoved == event.getType()) {
System.out.println(event.getType());
} else {
System.out.println("未知");
}
System.out.println("######################################");
}
}