zookeeper watches

发布时间:2020-03-30 11:11:11 作者:陈翰林
来源:网络 阅读:431

  Watches

所有的读操作(getData(),getChildren(),exists())都可以设置一个watch作为附加的操作

 

Watch的关键特性有

Watch当数据变化时,只会被触发一次,之后就会被移除,当数据再次变化时,不会再触发此watch

通知客户端是异步的,客户端只会在它收到watch event时才会知道znode改变.通知客户端的过程可能会发生异常,或者网络延迟,但zookeeper会保证通知的一致性

getData(),exists()设置数据监控,getChildren()设置孩子监控,

setData()会触发数据监控,成功create()会触发创建的znode数据监控和父zonde的孩子监控,成功的delete()zonde的数据监控和孩子监控、还有父zonde的孩子监控

  Zookeeper watches一些保证

GetDate,当删除这个节点的时候,监听对象只会被删除事件触发一次

 

 

以下是我根据官网例子简化的实例,大家可以动手实践以下,理解一下zookeeper的监听器的操作

package personal.zk;

import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;

/**
 * comments()
 *
 * @author:czh ,mail:453775810@qq.com
 * @date 2015/11/4
 */
@Slf4j
public class Executor implementsWatcher,Runnable,AsyncCallback.StatCallback {
    Integer
mutex=0;
    String
znode;

    ZooKeeper
zk;

    String
filename;

    String
exec[];


   
public Executor(StringhostPort, String znode) throws KeeperException, IOException,InterruptedException {
       
this.znode=znode;
       
zk = new ZooKeeper(hostPort, 3000, this);
       
zk.exists(znode,true, this, null);
    }

    
/**
     * @param
args
    
*/
   
public static void main(String[] args) {
        String hostPort =
"192.168.83.3:2181";
//       String hostPort="172.26.7.23:2181";
       
String znode = "/dubbo";
       
try {
           
new Executor(hostPort,znode).run();
        }
catch (Exceptione) {
            e.printStackTrace();
        }
    }

   
/***************************************************************************
     * We do process any eventsourselves, we just need to forward them on.
     *
//     * @seeorg.apache.zookeeper.Watcher#process(org.apache.zookeeper.proto.WatcherEvent)
     */
   
public void process(WatchedEventevent) {
       
log.info("event.getPath():" + event.getPath() + "event.getState():" + event.getState() + "event.getType():" + event.getType());
    }

   
public void run(){
       
try {
           
synchronized (mutex) {
               
while (true) {
                   
mutex.wait();
                }
            }
        }
catch (InterruptedExceptione) {
            e.printStackTrace();
        }
    }

   
/**
     * @param
rc
    
* @param path
    
* @param ctx
    
* @param stat
    
*/
   
@Override
   
public void proce***esult(int rc, String path, Object ctx, Stat stat) {
       
log.info("proce***esult===========begin");
       
boolean exists;
       
log.info(String.valueOf(rc));
       
switch (rc) {
           
case KeeperException.Code.Ok:
               
log.info("监听器设置成功");
               
break;
           
case KeeperException.Code.NoNode:
                
log.info("znode["+znode+"]不存在");
               
break;
           
case KeeperException.Code.SessionExpired:
           
case KeeperException.Code.NoAuth:
               
return;
           
default:
               
// Retry errors
               
zk.exists(znode, true, this, null);
               
return;
        }
       
log.info("proce***esult===========end");
    }
}

 

 


推荐阅读:
  1. Zookeeper详解(三):Zookeeper中的Znod
  2. Zookeeper教程从入门到精通

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

watches zookeeper he

上一篇:什么是云服务器?相比传统物理主机有哪些优势

下一篇:关于云服务器的优点和缺点介绍

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》