org.I0Itec.zkclient.IZkChildListener Java Examples
The following examples show how to use
org.I0Itec.zkclient.IZkChildListener.
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: ServerManagerMonitor.java From javabase with Apache License 2.0 | 6 votes |
private static void monitor() { ZkClient zc=new ZkClient("localhost:2181",1000); //创建监控节点 if(!zc.exists("/monitor")) zc.create("/monitor",null, CreateMode.PERSISTENT); if(!zc.exists("/monitor/client")) zc.create("/monitor/client",null, CreateMode.PERSISTENT); zc.subscribeChildChanges("/monitor/client",new IZkChildListener(){ @Override public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { System.out.println("------------客户端发生变化---------childPath="+parentPath ); currentChilds.forEach((String childPath)->{ System.out.println("parentPath = [" + parentPath + "], currentChilds = [" + currentChilds + "]"); }); } }); }
Example #2
Source File: ZkClient.java From DDMQ with Apache License 2.0 | 6 votes |
private void processDataOrChildChange(WatchedEvent event) { final String path = event.getPath(); if (event.getType() == EventType.NodeChildrenChanged || event.getType() == EventType.NodeCreated || event.getType() == EventType.NodeDeleted) { Set<IZkChildListener> childListeners = _childListener.get(path); if (childListeners != null && !childListeners.isEmpty()) { fireChildChangedEvents(path, childListeners); } } if (event.getType() == EventType.NodeDataChanged || event.getType() == EventType.NodeDeleted || event.getType() == EventType.NodeCreated) { Set<IZkDataListener> listeners = _dataListener.get(path); if (listeners != null && !listeners.isEmpty()) { fireDataChangedEvents(event.getPath(), listeners); } } }
Example #3
Source File: ZookeeperRegistry.java From mango with Apache License 2.0 | 6 votes |
@Override protected void doUnsubscribe(URL url, NotifyListener listener) { try { clientLock.lock(); Map<NotifyListener, IZkChildListener> childChangeListeners = serviceListeners.get(url); if (childChangeListeners != null) { IZkChildListener zkChildListener = childChangeListeners.get(listener); if (zkChildListener != null) { zkClient.unsubscribeChildChanges(ZkUtils.toNodeTypePath(url, ZkNodeType.CLIENT), zkChildListener); childChangeListeners.remove(listener); } } } catch (Throwable e) { throw new RpcFrameworkException(String.format("Failed to unsubscribe service %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e); } finally { clientLock.unlock(); } }
Example #4
Source File: ZkAdapter.java From brooklin with BSD 2-Clause "Simplified" License | 6 votes |
private void waitForTaskRelease(DatastreamTask task, long timeoutMs, String lockPath) { // Latch == 1 means task is busy (still held by the previous owner) CountDownLatch busyLatch = new CountDownLatch(1); String lockNode = lockPath.substring(lockPath.lastIndexOf('/') + 1); String lockRootPath = KeyBuilder.datastreamTaskLockRoot(_cluster, task.getConnectorType()); if (_zkclient.exists(lockPath)) { IZkChildListener listener = (parentPath, currentChildren) -> { if (!currentChildren.contains(lockNode)) { busyLatch.countDown(); } }; try { _zkclient.subscribeChildChanges(lockRootPath, listener); busyLatch.await(timeoutMs, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { String errorMsg = "Unexpectedly interrupted during task acquire."; ErrorLogger.logAndThrowDatastreamRuntimeException(LOG, errorMsg, e); } finally { _zkclient.unsubscribeChildChanges(lockRootPath, listener); } } }
Example #5
Source File: ZkClient.java From DDMQ with Apache License 2.0 | 6 votes |
private void processDataOrChildChange(WatchedEvent event) { final String path = event.getPath(); if (event.getType() == EventType.NodeChildrenChanged || event.getType() == EventType.NodeCreated || event.getType() == EventType.NodeDeleted) { Set<IZkChildListener> childListeners = _childListener.get(path); if (childListeners != null && !childListeners.isEmpty()) { fireChildChangedEvents(path, childListeners); } } if (event.getType() == EventType.NodeDataChanged || event.getType() == EventType.NodeDeleted || event.getType() == EventType.NodeCreated) { Set<IZkDataListener> listeners = _dataListener.get(path); if (listeners != null && !listeners.isEmpty()) { fireDataChangedEvents(event.getPath(), listeners); } } }
Example #6
Source File: ZkImpl.java From strimzi-kafka-operator with Apache License 2.0 | 6 votes |
@Override public Zk unwatchChildren(String path) { workerPool().executeBlocking( future -> { try { IZkChildListener listener = childWatches.remove(path); if (listener != null) { zookeeper.unsubscribeChildChanges(path, listener); } future.complete(); } catch (Throwable t) { future.fail(t); } }, log("unwatchChildren")); return this; }
Example #7
Source File: KafkaNodeListener.java From kmanager with Apache License 2.0 | 6 votes |
public void startListener() { LOG.info("Starting Kafka ZK node listener..."); exec.execute(new Runnable() { @Override public void run() { ZKUtils.getZKClient().subscribeChildChanges(ZkUtils.BrokerIdsPath(), new IZkChildListener() { @Override public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { } }); } }); }
Example #8
Source File: ZkImpl.java From strimzi-kafka-operator with Apache License 2.0 | 6 votes |
@Override public Future<Zk> watchChildren(String path, Handler<AsyncResult<List<String>>> watcher) { Promise<Zk> result = Promise.promise(); workerPool().executeBlocking( future -> { try { IZkChildListener listener = (parentPath, currentChilds) -> watcher.handle(Future.succeededFuture(currentChilds)); childWatches.put(path, listener); zookeeper.subscribeChildChanges(path, listener); future.complete(); } catch (Throwable t) { future.fail(t); } }, ar -> { log("watchChildren").handle(ar); if (ar.succeeded()) { result.complete(this); } else { result.fail(ar.cause()); } }); return result.future(); }
Example #9
Source File: ZooKeeperRegistry.java From light-4j with Apache License 2.0 | 6 votes |
@Override protected void unsubscribeService(URL url, ServiceListener serviceListener) { try { clientLock.lock(); Map<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url); if (childChangeListeners != null) { IZkChildListener zkChildListener = childChangeListeners.get(serviceListener); if (zkChildListener != null) { client.unsubscribeChildChanges(ZkUtils.toNodeTypePath(url, ZkNodeType.CLIENT), zkChildListener); childChangeListeners.remove(serviceListener); } } } catch (Throwable e) { throw new FrameworkException(new Status(UNSUBSCRIBE_ZOOKEEPER_SERVICE_ERROR, url, getUrl(), e.getMessage()), e); } finally { clientLock.unlock(); } }
Example #10
Source File: ZkRegister.java From dubbo-postman with MIT License | 6 votes |
@Override public void pullData() { //第一次获取所有的子节点 List<String> dubboNodes = client.getChildren(dubboRoot); processDubboNodes(dubboNodes); //处理新增或者删除的节点 IZkChildListener listener = new IZkChildListener(){ @Override public void handleChildChange(String parentPath, List<String> currentChilds) { if(currentChilds == null || currentChilds.isEmpty()){ return; } logger.debug("dubbo目录下变更节点数量:"+currentChilds.size()); processDubboNodes(currentChilds); } }; client.subscribeChildChanges(dubboRoot,listener); }
Example #11
Source File: ZooKeeperRegistry.java From light-4j with Apache License 2.0 | 5 votes |
@Override protected void subscribeService(final URL url, final ServiceListener serviceListener) { try { clientLock.lock(); ConcurrentHashMap<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url); if (childChangeListeners == null) { serviceListeners.putIfAbsent(url, new ConcurrentHashMap<ServiceListener, IZkChildListener>()); childChangeListeners = serviceListeners.get(url); } IZkChildListener zkChildListener = childChangeListeners.get(serviceListener); if (zkChildListener == null) { childChangeListeners.putIfAbsent(serviceListener, new IZkChildListener() { @Override public void handleChildChange(String parentPath, List<String> currentChilds) { serviceListener.notifyService(url, getUrl(), nodeChildsToUrls(parentPath, currentChilds)); if(logger.isInfoEnabled()) logger.info(String.format("[ZooKeeperRegistry] service list change: path=%s, currentChilds=%s", parentPath, currentChilds.toString())); } }); zkChildListener = childChangeListeners.get(serviceListener); } // prevent old node unregistered removeNode(url, ZkNodeType.CLIENT); createNode(url, ZkNodeType.CLIENT); String serverTypePath = ZkUtils.toNodeTypePath(url, ZkNodeType.AVAILABLE_SERVER); client.subscribeChildChanges(serverTypePath, zkChildListener); if(logger.isInfoEnabled()) logger.info(String.format("[ZooKeeperRegistry] subscribe service: path=%s, info=%s", ZkUtils.toNodePath(url, ZkNodeType.AVAILABLE_SERVER), url.toFullStr())); } catch (Throwable e) { throw new FrameworkException(new Status(SUBSCRIBE_ZOOKEEPER_SERVICE_ERROR, url, getUrl(), e.getMessage()), e); } finally { clientLock.unlock(); } }
Example #12
Source File: ZkclientZookeeperClient.java From dubbox with Apache License 2.0 | 5 votes |
public IZkChildListener createTargetChildListener(String path, final ChildListener listener) { return new IZkChildListener() { public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { listener.childChanged(parentPath, currentChilds); } }; }
Example #13
Source File: ZkJobRegistry.java From jeesuite-libs with Apache License 2.0 | 5 votes |
/** * 订阅节点事件 * * @return */ private synchronized void regAndSubscribeNodeEvent() { // 创建node节点 zkClient.createEphemeral(nodeStateParentPath + "/" + JobContext.getContext().getNodeId()); // 订阅节点信息变化 zkClient.subscribeChildChanges(nodeStateParentPath, new IZkChildListener() { @Override public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { // if (currentChilds == null || !currentChilds.contains(JobContext.getContext().getNodeId())) { zkClient.createEphemeral(nodeStateParentPath + "/" + JobContext.getContext().getNodeId()); logger.info("Nodelist is empty~ node[{}] re-join task clusters", JobContext.getContext().getNodeId()); return; } logger.info(">>nodes changed ,nodes:{}", currentChilds); // 分配节点 rebalanceJobNode(currentChilds); // 刷新当前可用节点 JobContext.getContext().refreshNodes(currentChilds); } }); logger.info("subscribe nodes change event at path:{}", nodeStateParentPath); // 注册命令事件 registerCommondEvent(); logger.info("subscribe command event at path:{}", nodeStateParentPath + "/" + JobContext.getContext().getNodeId()); // 刷新节点列表 List<String> activeNodes = zkClient.getChildren(nodeStateParentPath); JobContext.getContext().refreshNodes(activeNodes); logger.info("current activeNodes:{}", activeNodes); }
Example #14
Source File: ZkclientZookeeperClient.java From dubbox-hystrix with Apache License 2.0 | 5 votes |
public IZkChildListener createTargetChildListener(String path, final ChildListener listener) { return new IZkChildListener() { public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { listener.childChanged(parentPath, currentChilds); } }; }
Example #15
Source File: ZKClient.java From ECFileCache with Apache License 2.0 | 5 votes |
public void registerChildChanges(final String path, final ZKChildListener listener) { String realPath = getRealPath(path); final IZkChildListener underlyingListener = new IZkChildListener() { @Override public void handleChildChange(String parentPath, List<String> currentChilds) { listener.onChanged(parentPath, currentChilds); } }; client.subscribeChildChanges(realPath, underlyingListener); }
Example #16
Source File: ZclientSub.java From javabase with Apache License 2.0 | 5 votes |
/** * @Description: zkClient主要做了两件事情。 * @see:一件是在session loss和session expire时自动创建新的ZooKeeper实例进行重连。 * @see:一件是将一次性watcher包装为持久watcher。 * @see:后者的具体做法是简单的在watcher回调中,重新读取数据的同时再注册相同的watcher实例。 */ private static void test() { final ZkClient zkClient4subChild = new ZkClient("localhost:2181"); zkClient4subChild.subscribeChildChanges("/serverroot", new IZkChildListener() { public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { System.out.println("=======test==="); for (String string : currentChilds) { System.out.print(zkClient4subChild.readData("/serverroot/" + string, false) + ";"); } } }); }
Example #17
Source File: ZkclientZookeeperClient.java From dubbox with Apache License 2.0 | 5 votes |
public IZkChildListener createTargetChildListener(String path, final ChildListener listener) { return new IZkChildListener() { public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { listener.childChanged(parentPath, currentChilds); } }; }
Example #18
Source File: ZookeeperRegistry.java From mango with Apache License 2.0 | 5 votes |
@Override protected void doSubscribe(final URL url, final NotifyListener listener) { try { clientLock.lock(); ConcurrentHashMap<NotifyListener, IZkChildListener> childChangeListeners = serviceListeners.get(url); if (childChangeListeners == null) { serviceListeners.putIfAbsent(url, new ConcurrentHashMap<NotifyListener, IZkChildListener>()); childChangeListeners = serviceListeners.get(url); } IZkChildListener zkChildListener = childChangeListeners.get(listener); if (zkChildListener == null) { childChangeListeners.putIfAbsent(listener, new IZkChildListener() { @Override public void handleChildChange(String parentPath, List<String> currentChilds) { listener.notify(getUrl(), childrenNodeToUrls(parentPath, currentChilds)); logger.info(String.format("[ZookeeperRegistry] service list change: path=%s, currentChilds=%s", parentPath, currentChilds.toString())); } }); zkChildListener = childChangeListeners.get(listener); } // 防止旧节点未正常注销 removeNode(url, ZkNodeType.CLIENT); createNode(url, ZkNodeType.CLIENT); String serverTypePath = ZkUtils.toNodeTypePath(url, ZkNodeType.SERVER); zkClient.subscribeChildChanges(serverTypePath, zkChildListener); logger.info(String.format("[ZookeeperRegistry] subscribe service: path=%s, info=%s", ZkUtils.toNodePath(url, ZkNodeType.SERVER), url.toFullUri())); } catch (Throwable e) { throw new RpcFrameworkException(String.format("Failed to subscribe %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e); } finally { clientLock.unlock(); } }
Example #19
Source File: ZkclientZookeeperClient.java From dubbox with Apache License 2.0 | 5 votes |
public IZkChildListener createTargetChildListener(String path, final ChildListener listener) { return new IZkChildListener() { public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { listener.childChanged(parentPath, currentChilds); } }; }
Example #20
Source File: ZkclientZookeeperClient.java From JobX with Apache License 2.0 | 5 votes |
public IZkChildListener createTargetChildListener(String path, final ChildListener listener) { return new IZkChildListener() { public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { listener.childChanged(parentPath, currentChilds); } }; }
Example #21
Source File: ZkclientZookeeperClient.java From dubbo-2.6.5 with Apache License 2.0 | 5 votes |
@Override public IZkChildListener createTargetChildListener(String path, final ChildListener listener) { return new IZkChildListener() { @Override public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { listener.childChanged(parentPath, currentChilds); } }; }
Example #22
Source File: ZKTest.java From krpc with MIT License | 5 votes |
@Test public void run() { zc.subscribeChildChanges("/krpc/user", new IZkChildListener() { @Override public void handleChildChange(String s, List<String> list) throws Exception { System.out.println("s====" + s); if (list != null && list.size() > 0) { list.forEach(l -> { System.out.println("list====" + l); }); } else { System.out.println("list====null"); } } }); System.out.println("监听打开"); try { TimeUnit.HOURS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } }
Example #23
Source File: ZkClientZkClient.java From light-task-scheduler with Apache License 2.0 | 5 votes |
protected IZkChildListener createTargetChildListener(String path, final ChildListener listener) { return new IZkChildListener() { public void handleChildChange(String parentPath, List<String> currentChildes) throws Exception { listener.childChanged(parentPath, currentChildes); } }; }
Example #24
Source File: ZkRegisterCenter.java From krpc with MIT License | 5 votes |
/** * 订阅 * * @param serverName */ public static void subscribe(String serverName) { ServiceParams serviceParams = ServiceParams.getService(serverName); StringBuffer path = new StringBuffer("/krpc/"); path.append(serverName); zc.subscribeChildChanges(path.toString(), new IZkChildListener() { @Override public void handleChildChange(String s, List<String> list) throws Exception { System.out.println("server change===" + list); if (list != null && list.size() > 0) { List<Address> newAddr = new ArrayList<>(list.size()); for (String ipport : list) { String[] content = ipport.split(":"); Address address = new Address(); address.setHost(content[0]); address.setPort(Integer.valueOf(content[1])); newAddr.add(address); } serviceParams.setAddresses(newAddr); log.info("{} server change,content={}", serverName, list.toString()); } else { serviceParams.getAddresses().clear(); log.info("{} server change,no able server!", serverName); } } }); }
Example #25
Source File: ZkClient.java From DDMQ with Apache License 2.0 | 5 votes |
private boolean hasListeners(String path) { Set<IZkDataListener> dataListeners = _dataListener.get(path); if (dataListeners != null && dataListeners.size() > 0) { return true; } Set<IZkChildListener> childListeners = _childListener.get(path); if (childListeners != null && childListeners.size() > 0) { return true; } return false; }
Example #26
Source File: ZkClient.java From DDMQ with Apache License 2.0 | 5 votes |
public void unsubscribeChildChanges(String path, IZkChildListener childListener) { synchronized (_childListener) { final Set<IZkChildListener> listeners = _childListener.get(path); if (listeners != null) { listeners.remove(childListener); } } }
Example #27
Source File: ZkClient.java From DDMQ with Apache License 2.0 | 5 votes |
public List<String> subscribeChildChanges(String path, IZkChildListener listener) { synchronized (_childListener) { Set<IZkChildListener> listeners = _childListener.get(path); if (listeners == null) { listeners = new CopyOnWriteArraySet<IZkChildListener>(); _childListener.put(path, listeners); } listeners.add(listener); } return watchForChilds(path); }
Example #28
Source File: ZkClient.java From DDMQ with Apache License 2.0 | 5 votes |
private boolean hasListeners(String path) { Set<IZkDataListener> dataListeners = _dataListener.get(path); if (dataListeners != null && dataListeners.size() > 0) { return true; } Set<IZkChildListener> childListeners = _childListener.get(path); if (childListeners != null && childListeners.size() > 0) { return true; } return false; }
Example #29
Source File: ZkClient.java From DDMQ with Apache License 2.0 | 5 votes |
public void unsubscribeChildChanges(String path, IZkChildListener childListener) { synchronized (_childListener) { final Set<IZkChildListener> listeners = _childListener.get(path); if (listeners != null) { listeners.remove(childListener); } } }
Example #30
Source File: ZkClient.java From DDMQ with Apache License 2.0 | 5 votes |
public List<String> subscribeChildChanges(String path, IZkChildListener listener) { synchronized (_childListener) { Set<IZkChildListener> listeners = _childListener.get(path); if (listeners == null) { listeners = new CopyOnWriteArraySet<IZkChildListener>(); _childListener.put(path, listeners); } listeners.add(listener); } return watchForChilds(path); }