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