简介

Curator的NodeCache允许你监听一个节点,当节点数据更改或者节点被删除的时候将会触发监听。

官方文档:http://curator.apache.org/curator-recipes/node-cache.html

javaDoc:http://curator.apache.org/apidocs/org/apache/curator/framework/recipes/cache/NodeCache.html

代码示例

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.retry.ExponentialBackoffRetry; public class NodeCacheDemo {
private static CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(3000, 1));
private static String path = "/nodeCache/test/0001";
static {
client.start();
} public static void main(String[] args) throws Exception {
if (client.checkExists().forPath(path) == null) {
System.out.println("not exist");
client.create().creatingParentsIfNeeded().forPath(path);
}
System.out.println("created");
NodeCache nodeCache = new NodeCache(client, path, false);
nodeCache.getListenable().addListener(() -> System.out.println("nodeChanged"));
System.out.println("add listener");
nodeCache.start(true);
System.out.println("cache started");
client.setData().forPath(path, "lay".getBytes());
System.out.println("set data");
client.delete().forPath(path);
System.out.println("deleted");
Thread.sleep(3000);
nodeCache.close();
System.out.println("listener closed");
Thread.sleep(50000);
client.close();
}
}

最新文章

  1. javase-常用三种遍历方法
  2. 寒假学习计划(c++作业2)
  3. JAVA - HashMap和HashTable
  4. Drainage Ditches(dinic)
  5. 关于强制类型转换(c语言)
  6. FindStringExact
  7. 11.在Global的Application_Error处理错误示例
  8. java J2EE学习入门
  9. Android Gson深入分析
  10. 《Intel汇编第5版》 汇编调用子过程
  11. ASP.NET MVC 使用Uploadify实现多文件异步无刷新上传
  12. private static final 修饰符
  13. Tree Cutting POJ - 2378 (树形DP)
  14. web.html
  15. 洛谷.2197.nim游戏(博弈论 Nim)
  16. MATLAB 移动复制文件
  17. Spring基础(3) : 静态工厂和实例工厂创建bean
  18. 005.iSCSI客户端配置示例-Windows
  19. SpringMVC方法传递集合数组
  20. 【Bootloader】探究bootloader,分析u-boot源码

热门文章

  1. Eclipse (JavaEE版)中修改web项目的访问路径
  2. POST与GET请求的区别
  3. Backtracking-401. Binary Watch
  4. vue复选框选中值获取
  5. 移动端页面模板viewport
  6. XSS、CSRF与验证码等等
  7. Java之集合(十二)TreeMap
  8. KMP-字符串模式匹配(c++/python实现)
  9. ActiveMQ学习--001--ActiveMQ和消息中间件
  10. Android六大基本布局