1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
@Test public void testNodeCache() { CuratorFramework client = CuratorClientFactory.createSimple(connectionString); String workerPath = "/test/CRUD/node-1"; try { client.start(); if (client.checkExists().forPath(workerPath) == null) { client.create().creatingParentsIfNeeded().forPath(workerPath); }
NodeCache nodeCache = new NodeCache(client, workerPath, false); nodeCache.getListenable().addListener(() -> { ChildData childData = nodeCache.getCurrentData(); log.info("ZNode节点状态改变, path={}", childData.getPath()); log.info("ZNode节点状态改变, data={}", new String(childData.getData(), StandardCharsets.UTF_8)); log.info("ZNode节点状态改变, stat={}", childData.getStat()); }); nodeCache.start();
client.setData().forPath(workerPath, "第一次更改内容".getBytes()); Thread.sleep(1000);
client.setData().forPath(workerPath, "第二次更改内容".getBytes()); Thread.sleep(1000);
client.setData().forPath(workerPath, "第三次更改内容".getBytes()); Thread.sleep(1000); } catch (Exception e) { log.error("创建NodeCache监听失败, path={}", workerPath); } }
@Test public void testPathChildrenCache() { CuratorFramework client = CuratorClientFactory.createSimple(connectionString); String zkPath = "/test/CRUD"; try { client.start(); if (client.checkExists().forPath(zkPath) == null) { client.create().creatingParentsIfNeeded().forPath(zkPath); }
PathChildrenCache cache = new PathChildrenCache(client, zkPath, true); cache.getListenable().addListener((c, event) -> { log.info("Callback - event:{}", event); }); cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE); Thread.sleep(1000);
for (int i = 2; i < 5; i++) { client.create() .withMode(CreateMode.PERSISTENT) .forPath(zkPath + "/node-" + i, ("数据"+i).getBytes() ); } Thread.sleep(1000);
for (int i = 2; i < 5; i++) { client.delete().forPath(zkPath + "/node-" + i); } LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(10)); } catch (Exception e) { log.error("PathCache监听失败, path={}", zkPath, e); } }
@Test public void testTreeCache() { CuratorFramework client = CuratorClientFactory.createSimple(connectionString); String workerPath = "/test/CRUD"; try { client.start(); if (client.checkExists().forPath(workerPath) == null) { client.create().creatingParentsIfNeeded().forPath(workerPath); }
TreeCache treeCache = new TreeCache(client, workerPath); treeCache.getListenable().addListener((c, event) -> { ChildData data = event.getData(); if (data == null) { log.info("数据为空:{}", event); return; } switch (event.getType()) { case NODE_ADDED: log.info("[TreeCache]节点增加, path={}, data={}", data.getPath(), new String(data.getData(), StandardCharsets.UTF_8)); break; case NODE_UPDATED: log.info("[TreeCache]节点更新, path={}, data={}", data.getPath(), new String(data.getData(), StandardCharsets.UTF_8)); break; case NODE_REMOVED: log.info("[TreeCache]节点删除, path={}, data={}", data.getPath(), new String(data.getData(), StandardCharsets.UTF_8)); break; default: break; } });
treeCache.start(); Thread.sleep(1000);
for (int i = 2; i < 5; i++) { client.create() .withMode(CreateMode.PERSISTENT) .forPath(workerPath + "/node-" + i, ("数据"+i).getBytes() ); } Thread.sleep(1000);
for (int i = 2; i < 5; i++) { client.delete().forPath(workerPath + "/node-" + i); } Thread.sleep(1000);
client.delete().forPath(workerPath); LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(10)); } catch (Exception e) { log.error("TreeCache监听失败, path={}", workerPath, e); } }
|