08_zookeeper的客户端工具curator_watcher

【使用usingWatcher()】

//两个usingWatcher的传入参数不同
public interface Watchable<T> {
    T watched();

    T usingWatcher(Watcher var1);

    T usingWatcher(CuratorWatcher var1);
}

[提前写好两个实现类]

//实现原生的watcher接口实现
class MyWatcher implements Watcher{
    @Override
    public void process(WatchedEvent watchedEvent) {
        System.err.println("Watcher事件触发:"+watchedEvent.getPath());
    }
}

//实现curator的CuratorWatcher接口
class MyCuratorWatcher implements CuratorWatcher{
    @Override
    public void process(WatchedEvent watchedEvent) throws Exception {
        System.err.println("CuratorWatcher事件触发:"+watchedEvent.getPath());
    }
}

 【原生Watcher代码示例】

//watcher事件,当使用usingWatcher的时候,监听只会触发一次,监听完毕后销毁
client.getData()
        .usingWatcher(new MyWatcher())
        .forPath(nodePath);

System.in.read();  //暂停下线程

【运行结果】

【CuratorWatcher事件代码示例】

//watcher事件,当使用usingWatcher的时候,监听只会触发一次,监听完毕后销毁
client.getData()
        .usingWatcher(new MyCuratorWatcher())
        .forPath(nodePath);

System.in.read();  //暂停下线程

【运行结果】

与上面一致。

 

posted @ 2018-09-24 00:05  HigginCui  阅读(421)  评论(0编辑  收藏  举报