org.apache.curator.framework.api.CuratorWatcher Java Examples
The following examples show how to use
org.apache.curator.framework.api.CuratorWatcher.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ZkRpcClientExecutor.java From hasting with MIT License | 6 votes |
private void watchWeight(final String application){ String applicationWeightsKey = ZKUtils.genApplicationWeightsKey(application); try{ zkclient.getData().usingWatcher(new CuratorWatcher() { @Override public void process(WatchedEvent watchedEvent) throws Exception { if(watchedEvent.getType()== Watcher.Event.EventType.NodeDataChanged){ //拿到权重列表 doGetWeights(application,true); } } }).inBackground().forPath(applicationWeightsKey); }catch(Exception e){ logger.error("[zookeeper] watch "+applicationWeightsKey,e); } }
Example #2
Source File: ChangesRegistry.java From nakadi with MIT License | 6 votes |
/** * Return list of changes in zookeeper. in case if {@code changeListener} is provided, sets it to react * on changelist changes * * @param changesListener Listener to set. * @return Current list of changes in zookeeper * @throws Exception In case if there is error in communicating with zookeeper. */ public List<Change> getCurrentChanges(@Nullable final Runnable changesListener) throws Exception { final List<String> children; if (null == changesListener) { children = zk.get().getChildren() .forPath(ET_CACHE_PATH); } else { children = zk.get().getChildren() .usingWatcher((CuratorWatcher) (e) -> changesListener.run()) .forPath(ET_CACHE_PATH); } final List<Change> changes = new ArrayList<>(); for (final String child : children) { final byte[] data = zk.get().getData().forPath(getPath(child)); changes.add(new Change(child, new String(data, Charsets.UTF_8), new Date())); } return changes; }
Example #3
Source File: ZookeeperClient.java From kafka-monitor with Apache License 2.0 | 6 votes |
/** * 监听该路径下得子节点 * * @param path 路径 * @param callback 监听回调 * @throws Exception */ public void watcherChildrenNode(final String path, final WatcherEventCallback callback) throws Exception { final List<String> oldChildren = this.getChildren(path); logger.debug("oldChildren------{}", oldChildren); CuratorWatcher watcher = new CuratorWatcher() { @Override public void process(WatchedEvent event) throws Exception { try { WatcherEvent watcherEvent = new WatcherEvent(); watcherEvent.setState(event.getState()); watcherEvent.setPath(event.getPath()); watcherEvent.setEventType(event.getType()); watcherEvent.setOldChildrenNode(oldChildren); callback.watchedEvent(watcherEvent); if (checkExists(path)) { watcherChildrenNode(path, callback); } } catch (Exception e) { logger.debug("{}", e.getMessage()); } } }; curator.getChildren().usingWatcher(watcher).forPath(path); }
Example #4
Source File: ZkRpcServer.java From hasting with MIT License | 6 votes |
/** * 监控限流配置 */ private void watchLimit(){ try{ zkclient.getData().usingWatcher(new CuratorWatcher() { @Override public void process(WatchedEvent watchedEvent) throws Exception { watchLimit(); fetchLimit(); } }).inBackground().forPath(ZKUtils.genLimitKey(this.getApplication())); }catch(Exception e){ if(e instanceof KeeperException.NoNodeException){ // limitCache.addOrUpdate(new ArrayList<LimitDefine>()); }else{ logger.error("fetch application request limit config failed",e); } } }
Example #5
Source File: SharedValue.java From curator with Apache License 2.0 | 5 votes |
@VisibleForTesting protected SharedValue(WatcherRemoveCuratorFramework client, String path, byte[] seedValue, CuratorWatcher watcher) { this.client = client; this.path = PathUtils.validatePath(path); this.seedValue = Arrays.copyOf(seedValue, seedValue.length); // inject watcher for testing this.watcher = watcher; currentValue = new AtomicReference<VersionedValue<byte[]>>(new VersionedValue<byte[]>(UNINITIALIZED_VERSION, Arrays.copyOf(seedValue, seedValue.length))); }
Example #6
Source File: AsyncRemoveWatchesBuilderImpl.java From curator with Apache License 2.0 | 5 votes |
@Override public AsyncPathable<AsyncStage<Void>> removing(CuratorWatcher watcher, Watcher.WatcherType watcherType) { this.curatorWatcher = Objects.requireNonNull(watcher, "watcher cannot be null"); this.watcherType = Objects.requireNonNull(watcherType, "watcherType cannot be null"); this.watcher = null; return this; }
Example #7
Source File: AsyncRemoveWatchesBuilderImpl.java From curator with Apache License 2.0 | 5 votes |
@Override public AsyncPathable<AsyncStage<Void>> removing(CuratorWatcher watcher, Watcher.WatcherType watcherType, Set<RemoveWatcherOption> options) { this.curatorWatcher = Objects.requireNonNull(watcher, "watcher cannot be null"); this.options = Objects.requireNonNull(options, "options cannot be null"); this.watcherType = Objects.requireNonNull(watcherType, "watcherType cannot be null"); this.watcher = null; return this; }
Example #8
Source File: AsyncRemoveWatchesBuilderImpl.java From curator with Apache License 2.0 | 5 votes |
@Override public AsyncPathable<AsyncStage<Void>> removing(CuratorWatcher watcher, Set<RemoveWatcherOption> options) { this.curatorWatcher = Objects.requireNonNull(watcher, "watcher cannot be null"); this.options = Objects.requireNonNull(options, "options cannot be null"); this.watcher = null; return this; }
Example #9
Source File: AsyncRemoveWatchesBuilderImpl.java From curator with Apache License 2.0 | 5 votes |
@Override public AsyncPathable<AsyncStage<Void>> removing(CuratorWatcher watcher) { this.curatorWatcher = Objects.requireNonNull(watcher, "watcher cannot be null"); this.watcher = null; return this; }
Example #10
Source File: TestWatchesBuilder.java From curator with Apache License 2.0 | 5 votes |
@Test public void testRemoveCuratorWatch() throws Exception { Timing timing = new Timing(); CuratorFrameworkImpl client = (CuratorFrameworkImpl)CuratorFrameworkFactory.builder(). connectString(server.getConnectString()). retryPolicy(new RetryOneTime(1)). build(); try { client.start(); final CountDownLatch removedLatch = new CountDownLatch(1); final String path = "/"; CuratorWatcher watcher = new CuratorWatcher() { @Override public void process(WatchedEvent event) throws Exception { if(event.getPath().equals(path) && event.getType() == EventType.DataWatchRemoved) { removedLatch.countDown(); } } }; client.checkExists().usingWatcher(watcher).forPath(path); client.watches().remove(watcher).forPath(path); Assert.assertTrue(timing.awaitLatch(removedLatch), "Timed out waiting for watch removal"); } finally { CloseableUtils.closeQuietly(client); } }
Example #11
Source File: NamespaceWatcher.java From curator with Apache License 2.0 | 5 votes |
NamespaceWatcher(CuratorFrameworkImpl client, CuratorWatcher curatorWatcher, String unfixedPath) { this.client = client; this.actualWatcher = null; this.curatorWatcher = curatorWatcher; this.unfixedPath = Preconditions.checkNotNull(unfixedPath, "unfixedPath cannot be null"); }
Example #12
Source File: Watching.java From curator with Apache License 2.0 | 5 votes |
public Watching(CuratorFrameworkImpl client, CuratorWatcher watcher) { this.client = client; this.watcher = null; this.curatorWatcher = watcher; this.watched = false; }
Example #13
Source File: ZookeeperCurator.java From micro-service with MIT License | 5 votes |
private static void process() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new RetryNTimes(10, 5000)); client.start(); // monitor child node List<String> children = client.getChildren().usingWatcher(new CuratorWatcher() { @Override public void process(WatchedEvent event) throws Exception { logger.info("monitor: {}", event); } }).forPath("/"); logger.info("children: {}", children); addListener(client); // create node String result = client.create().withMode(CreateMode.PERSISTENT).withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE) .forPath("/test", "Data".getBytes()); logger.info("result: {}", result); // set node client.setData().forPath("/test", "111".getBytes()); client.setData().forPath("/test", "222".getBytes()); // delete node logger.info("path: {}", client.checkExists().forPath("/test")); client.delete().withVersion(-1).forPath("/test"); logger.info("path: {}", client.checkExists().forPath("/test")); client.close(); logger.info("client close"); }
Example #14
Source File: ZKUtil.java From codes-scratch-zookeeper-netty with Apache License 2.0 | 5 votes |
public static List<String> getChilds(CuratorFramework client, String path, CuratorWatcher watcher) { try { if (watcher != null) { return (List) ((BackgroundPathable) client.getChildren().usingWatcher(watcher)).forPath(path); } return (List) client.getChildren().forPath(path); } catch (Exception e) { LOGGER.error("ZKUtil-->>getChilds(CuratorFramework client, String path, CuratorWatcher watcher) error,", e); } return null; }
Example #15
Source File: ZKUtil.java From codes-scratch-zookeeper-netty with Apache License 2.0 | 5 votes |
public static String getData(CuratorFramework client, String path, CuratorWatcher watcher) { try { if (client.checkExists().forPath(path) == null) { return null; } if (watcher != null) { return List2StringUtil .toString((byte[]) ((BackgroundPathable) client.getData().usingWatcher(watcher)).forPath(path)); } return List2StringUtil.toString((byte[]) client.getData().forPath(path)); } catch (Exception e) { LOGGER.error("ZKUtil-->>getData(CuratorFramework client, String path, CuratorWatcher watcher) error ", e); } return null; }
Example #16
Source File: ZKUtil.java From codes-scratch-zookeeper-netty with Apache License 2.0 | 5 votes |
public static boolean exists(CuratorFramework client, String path, CuratorWatcher watcher) { try { if (watcher != null) { return ((BackgroundPathable) client.checkExists().usingWatcher(watcher)).forPath(path) != null; } return client.checkExists().forPath(path) != null; } catch (Exception e) { LOGGER.error("ZKUtil-->>exists(CuratorFramework client, String path, CuratorWatcher watcher) error, ", e); } return false; }
Example #17
Source File: StorageService.java From nakadi with MIT License | 5 votes |
@PostConstruct private void watchDefaultStorage() { try { curator.getData().usingWatcher((CuratorWatcher) event -> { final byte[] defaultStorageId = curator.getData().forPath(ZK_TIMELINES_DEFAULT_STORAGE); if (defaultStorageId != null) { final Storage storage = getStorage(new String(defaultStorageId)); defaultStorage.setStorage(storage); } watchDefaultStorage(); }).forPath(ZK_TIMELINES_DEFAULT_STORAGE); } catch (final Exception e) { LOG.warn("Error while creating watcher for default storage updates {}", e.getMessage(), e); } }
Example #18
Source File: ServiceRegistryUpdater.java From ranger with Apache License 2.0 | 5 votes |
public void start() throws Exception { CuratorFramework curatorFramework = serviceRegistry.getService().getCuratorFramework(); if(!disableWatchers) { curatorFramework.getChildren() .usingWatcher(new CuratorWatcher() { @Override public void process(WatchedEvent event) throws Exception { switch (event.getType()) { case NodeChildrenChanged: { checkForUpdate(); break; } case None: case NodeCreated: case NodeDeleted: case NodeDataChanged: default: break; } } }) .forPath(PathBuilder.path(serviceRegistry.getService())); //Start watcher on service node } updateRegistry(); logger.info("Started polling zookeeper for changes for service:{}", serviceRegistry.getService().getServiceName()); }
Example #19
Source File: GetChildrenBuilderImpl.java From curator with Apache License 2.0 | 4 votes |
@Override public BackgroundPathable<List<String>> usingWatcher(CuratorWatcher watcher) { watching = new Watching(client, watcher); return this; }
Example #20
Source File: CuratorZookeeperClient.java From grpc-nebula-java with Apache License 2.0 | 4 votes |
public void removeTargetChildListener(String path, CuratorWatcher listener) { ((CuratorWatcherImpl) listener).unwatch(); }
Example #21
Source File: CuratorZookeeperClient.java From dubbo-2.6.5 with Apache License 2.0 | 4 votes |
@Override public CuratorWatcher createTargetChildListener(String path, ChildListener listener) { return new CuratorWatcherImpl(listener); }
Example #22
Source File: TestInterProcessSemaphore.java From curator with Apache License 2.0 | 4 votes |
@Test public void testNoOrphanedNodes() throws Exception { final Timing timing = new Timing(); final ExecutorService executor = Executors.newFixedThreadPool(1); CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); client.start(); try { final InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", 1); Lease lease = semaphore.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS); Assert.assertNotNull(lease); final List<String> childNodes = client.getChildren().forPath("/test/leases"); Assert.assertEquals(childNodes.size(), 1); final CountDownLatch nodeCreatedLatch = new CountDownLatch(1); client.getChildren().usingWatcher(new CuratorWatcher() { @Override public void process(WatchedEvent event) throws Exception { if ( event.getType() == Watcher.Event.EventType.NodeCreated ) { nodeCreatedLatch.countDown(); } } }).forPath("/test/leases"); final Future<Lease> leaseFuture = executor.submit(new Callable<Lease>() { @Override public Lease call() throws Exception { return semaphore.acquire(timing.forWaiting().multiple(2).seconds(), TimeUnit.SECONDS); } }); // wait for second lease to create its node timing.awaitLatch(nodeCreatedLatch); String newNode = null; for ( String c : client.getChildren().forPath("/test/leases") ) { if ( !childNodes.contains(c) ) { newNode = c; } } Assert.assertNotNull(newNode); // delete the ephemeral node to trigger a retry client.delete().forPath("/test/leases/" + newNode); // release first lease so second one can be acquired lease.close(); lease = leaseFuture.get(); Assert.assertNotNull(lease); lease.close(); Assert.assertEquals(client.getChildren().forPath("/test/leases").size(), 0); // no more lease exist. must be possible to acquire a new one Assert.assertNotNull(semaphore.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS)); } finally { executor.shutdownNow(); TestCleanState.closeAndTestClean(client); } }
Example #23
Source File: CuratorZookeeperClient.java From dubbo-2.6.5 with Apache License 2.0 | 4 votes |
@Override public void removeTargetChildListener(String path, CuratorWatcher listener) { ((CuratorWatcherImpl) listener).unwatch(); }
Example #24
Source File: AsyncWatchBuilderImpl.java From curator with Apache License 2.0 | 4 votes |
@Override public AsyncPathable<AsyncStage<Void>> usingWatcher(CuratorWatcher watcher) { watching = new Watching(client, watcher); return this; }
Example #25
Source File: TestInterProcessSemaphore.java From xian with Apache License 2.0 | 4 votes |
@Test public void testNoOrphanedNodes() throws Exception { final Timing timing = new Timing(); final ExecutorService executor = Executors.newFixedThreadPool(1); CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); client.start(); try { final InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", 1); Lease lease = semaphore.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS); Assert.assertNotNull(lease); final List<String> childNodes = client.getChildren().forPath("/test/leases"); Assert.assertEquals(childNodes.size(), 1); final CountDownLatch nodeCreatedLatch = new CountDownLatch(1); client.getChildren().usingWatcher(new CuratorWatcher() { @Override public void process(WatchedEvent event) throws Exception { if ( event.getType() == Watcher.Event.EventType.NodeCreated ) { nodeCreatedLatch.countDown(); } } }).forPath("/test/leases"); final Future<Lease> leaseFuture = executor.submit(new Callable<Lease>() { @Override public Lease call() throws Exception { return semaphore.acquire(timing.forWaiting().multiple(2).seconds(), TimeUnit.SECONDS); } }); // wait for second lease to create its node timing.awaitLatch(nodeCreatedLatch); String newNode = null; for ( String c : client.getChildren().forPath("/test/leases") ) { if ( !childNodes.contains(c) ) { newNode = c; } } Assert.assertNotNull(newNode); // delete the ephemeral node to trigger a retry client.delete().forPath("/test/leases/" + newNode); // release first lease so second one can be acquired lease.close(); lease = leaseFuture.get(); Assert.assertNotNull(lease); lease.close(); Assert.assertEquals(client.getChildren().forPath("/test/leases").size(), 0); // no more lease exist. must be possible to acquire a new one Assert.assertNotNull(semaphore.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS)); } finally { client.close(); executor.shutdownNow(); } }
Example #26
Source File: NamespaceWatcherMap.java From xian with Apache License 2.0 | 4 votes |
NamespaceWatcher getNamespaceWatcher(CuratorWatcher watcher) { return get(watcher, new NamespaceWatcher(client, watcher)); }
Example #27
Source File: Watching.java From xian with Apache License 2.0 | 4 votes |
Watching(CuratorFrameworkImpl client, CuratorWatcher watcher) { this.watcher = (watcher != null) ? client.getNamespaceWatcherMap().getNamespaceWatcher(watcher) : null; this.watched = false; }
Example #28
Source File: NamespaceWatcher.java From xian with Apache License 2.0 | 4 votes |
NamespaceWatcher(CuratorFrameworkImpl client, CuratorWatcher curatorWatcher) { this.client = client; this.actualWatcher = null; this.curatorWatcher = curatorWatcher; }
Example #29
Source File: GetChildrenBuilderImpl.java From xian with Apache License 2.0 | 4 votes |
@Override public BackgroundPathable<List<String>> usingWatcher(CuratorWatcher watcher) { watching = new Watching(client, watcher); return this; }
Example #30
Source File: WatchesBuilderImpl.java From curator with Apache License 2.0 | 4 votes |
public WatchesBuilderImpl(CuratorFrameworkImpl client, Watcher watcher, CuratorWatcher curatorWatcher, WatcherType watcherType, boolean guaranteed, boolean local, boolean quietly, Backgrounding backgrounding) { super(client, watcher, curatorWatcher, watcherType, guaranteed, local, quietly, backgrounding); }