zookeeper --- (监听机制示例)
package com..zookeeper.GWdemo; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import java.io.IOException; /** *官网示例: Watcher 监听机制 */ public class WatcherDemo implements Watcher { static ZooKeeper zooKeeper; private static Stat stat = new Stat(); static { try { zooKeeper = new ZooKeeper("127.0.0.1:2181", 10000,new WatcherDemo()); } catch (IOException e) { e.printStackTrace(); } } @Override public void process(WatchedEvent event) { System.out.println("eventType:"+event.getType()); if(event.getType()==Event.EventType.NodeDataChanged){ try { zooKeeper.exists(event.getPath(),true); //输出更改之后的值 byte [] bytes = zooKeeper.getData(event.getPath(),true,stat); System.out.println(new String(bytes)); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) throws IOException, KeeperException, InterruptedException { String path="/watcher"; //没有 /watcher 则创建 并赋值为0 if(zooKeeper.exists(path,false)==null) { zooKeeper.create("/watcher", "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } Thread.sleep(1000); System.out.println("-----------"); //true表示使用zookeeper实例中配置的watcher Stat stat=zooKeeper.exists(path,true); System.in.read(); } }
更改节点数据值为 500000 监听结果:
package com..zookeeper; import java.util.concurrent.CountDownLatch; import org.apache.zookeeper.*; import org.apache.zookeeper.Watcher.Event.EventType; import org.apache.zookeeper.Watcher.Event.KeeperState; import org.apache.zookeeper.data.Stat; /** * 简单理解*/ public class Demo1 { private static CountDownLatch connectedSemaphore = new CountDownLatch(1); private static ZooKeeper zk = null; private static Stat stat = new Stat(); public static void main(String[] args) throws Exception { //zookeeper配置数据存放路径 String path = "/watcher"; //连接zookeeper并且注册一个默认的监听器 zk = new ZooKeeper("127.0.0.1:2181", 10000, new Watcher() { @Override public void process(WatchedEvent event) { System.out.println("事件类型为:" + event.getType()); System.out.println("事件发生的路径:" + event.getPath()); System.out.println("通知状态为:" +event.getState()); if (KeeperState.SyncConnected == event.getState()) { //zk连接成功通知事件 if (EventType.None == event.getType() && null == event.getPath()) { connectedSemaphore.countDown(); } else if (event.getType() == EventType.NodeDataChanged) { //zk目录节点数据变化通知事件 try { System.out.println("配置已修改,新值为--->:" + new String(zk.getData(event.getPath(), true, stat))); } catch (Exception e) { } } } } }); //等待zk连接成功的通知 connectedSemaphore.await(); //没有 /watcher 则创建 并赋值为0 if(zk.exists(path,false)==null) { zk.create("/watcher", "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } // 取出子目录节点列表 System.out.println("子目录节点列表--->"+zk.getChildren(path,true)); //获取path目录节点的配置数据,并注册默认的监听器 System.out.println("path目录的值--->:"+new String(zk.getData(path, true, stat))); Thread.sleep(Integer.MAX_VALUE); } }
更改节点数据值为 500000 监听结果: