《一》节点改变事件的监听

 public class CauratorClientTest {

     //链接地址
private static String zkhost="172.19.27.246:2181";
//sessionTimeoutMs会话超时时间,单位为毫秒。默认是60000ms
private static int sessionTimeoutMs=5000;
//connectionTimeoutMs连接创建超时时间,单位毫秒,默认15000ms
private static int connectionTimeOutMs=3000;
//重连策略
private static int maxRetries;
// zookeeper连接间隔时间基数
private static int baseSleepTimeMs=1000;
//系统域dev,qa,pro
private static String domain="dev"; /**
* 数据节点被改变的事件。能监听节点存储的数据发生变化,和节点被删除的事件。
* 节点被删除后,会调用回调方法。
*/
public static void testNodeChangeEvent() throws Exception{
CuratorFramework client = CuratorFrameworkFactory.newClient(zkhost,sessionTimeoutMs,connectionTimeOutMs, new ExponentialBackoffRetry(baseSleepTimeMs,maxRetries));
client.start(); String path=client.create().creatingParentContainersIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/dev/sxf/cd","sxf".getBytes()); NodeCache nodeCache=new NodeCache(client,path); /**
* 注册数据节点中存储的数据被改变的事件
*/
nodeCache.getListenable().addListener(new NodeCacheListener() {
/**
* 数据节点变化事件
*/
@Override
public void nodeChanged() throws Exception { ChildData data=nodeCache.getCurrentData();
if(data==null){
System.out.println("节点被删除");
return;
} Stat stat=data.getStat(); int a=stat.getNumChildren(); System.out.println("子节点的个数为==>"+a); System.out.println("节点数据被改变改变后的数值为==>"+new String(nodeCache.getCurrentData().getData())); }
}); nodeCache.start(); //当前线程休眠几秒
Thread.sleep(10000L); client.setData().forPath("/dev/sxf/cd","中国人民解放军".getBytes()); //当前线程休眠几秒
Thread.sleep(10000L);
client.setData().forPath("/dev/sxf/cd","第二次改变".getBytes()); //当前线程休眠几秒
Thread.sleep(10000L);
client.delete().forPath("/dev/sxf/cd"); Thread.sleep(100000L); } }

Zookeeper之Curator(1)客户端对节点的一些监控事件的api使用的更多相关文章

  1. ZooKeeper与Curator注册和监控

    Curator提供了对zookeeper客户端的封装,并监控连接状态和会话session,特别是会话session过期后,curator能够重新连接zookeeper,并且创建一个新的session. ...

  2. ZooKeeper和Curator相关经验总结

    一.关于ZooKeeper的watch用法,需要注意 详细说明如下: ZooKeeper Watches All of the read operations in ZooKeeper - getDa ...

  3. Zookeeper框架Curator使用

    本文参考自https://blog.csdn.net/wo541075754/article/details/69138878?utm_source=gold_browser_extension ht ...

  4. zookeeper系列(三)zookeeper的使用--开源客户端

    作者:leesf    掌控之中,才会成功:掌控之外,注定失败, 原创博客地址:http://www.cnblogs.com/leesf456/ 奇文共欣赏,大家共同学习进步. 一.前言 上一篇博客已 ...

  5. ZooKeeper 之 zkCli.sh客户端的命令使用

    zkCli.sh的使用 ZooKeeper服务器简历客户端 ./zkCli.sh -timeout 0 -r -server ip:port ./zkCli.sh -timeout 5000 -ser ...

  6. zookeeper之二 zkCli客户端命令

    ZooKeeper命令行界面(CLI)用于与ZooKeeper集合进行交互以进行开发.它有助于调试和解决不同的选项.要执行ZooKeeper CLI操作,首先打开ZooKeeper服务器(“bin/z ...

  7. zookeeper图形化的客户端工具

    追加一个zookeeper图形化的客户端工具: 1.zookeeper图像化客户端工具的下载地址:https://issues.apache.org/jira/secure/attachment/12 ...

  8. zookeeper系列(一)zookeeper图形化的客户端工具

    追加一个zookeeper图形化的客户端工具: 1.zookeeper图像化客户端工具的下载地址:https://issues.apache.org/jira/secure/attachment/12 ...

  9. jQuery之防止冒泡事件,冒泡事件就是点击子节点,会向上触发父节点,祖先节点的点击事件。

    冒泡事件就是点击子节点,会向上触发父节点,祖先节点的点击事件. 下面是html代码部分: <body> <div id="content"> 外层div元素 ...

随机推荐

  1. apply的理解和数组降维

    func.apply(thisObj, [argArray] ); apply方法用来改变函数执行时的this指向,后面的参数是一个类数组对象,可以是数组,arguments,甚至一个有length属 ...

  2. [转]Android 如何根据网络地址获取网络图片方法

    http://blog.csdn.net/xiazdong/article/details/7724103 目录(?)[-] h2pre namecode classhtml stylefont-we ...

  3. shell运用

    在shell脚本一 中,我讨论了shell脚本的语法规范,shell脚本的变量,以及shell脚本的测试语句. 仅仅懂得这些只能写简单的脚本,在简单的脚本中各条语句按顺序执行,从而实现自动化的管理,顺 ...

  4. 关闭nginx日志

    在nginx.conf中将 access_log /dev/null; error_log /dev/null;

  5. bzo4802 欧拉函数 miller_rabin pollard_rho

    欧拉函数 Time Limit: 5 Sec  Memory Limit: 256 MBSubmit: 1112  Solved: 418[Submit][Status][Discuss] Descr ...

  6. session 超时设置

    Java Web开发Session超时设置 博客分类: Java Web 在Java Web开发中,Session为我们提供了很多方便,Session是由浏览器和服务器之间维护的.Session超时理 ...

  7. transition和animation概况

    有人可能会有疑问,CSS3动画不是只有animation一个属性吗?怎么又和转化(transform)和过渡(transition)扯上关系了,其实并非如此,转化(transform)属性让动画的变换 ...

  8. Merge Query

    1. Oracle: "MERGE into MHGROUP.proj_access m using dual on " + "(PRJ_ID = '" + W ...

  9. 使用shell脚本往文件中加一列

    上午大学同学问了我一个脚本的问题,大概需求就是看到所有端口的开启情况,还要知道每个端口的应用程序路径,而且要和之前的数据齐平,就是再加一列数据.我腚眼一看,非常容易嘛,但由于当时忙,所以就说中午给他发 ...

  10. gradle web项目启动报错: java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener

    严重: Error configuring application listener of class org.springframework.web.util.IntrospectorCleanup ...