final String node = ZKPaths.makePath(path, child); final byte[] bytes = curator.getData() .usingWatcher(dataWatcher) .forPath(node); final String json = new String(bytes, UTF_8);
try { final byte[] bytes = curator.getData() .usingWatcher(dataWatcher) .forPath(node); value = Json.read(bytes, valueType);
private void findLeader() { ReentrantReadWriteLock.WriteLock lock = this.readWriteLock.writeLock(); lock.lock(); try { if (this.zooKeeper.checkExists().usingWatcher(new FindLeaderWatcher()).forPath(this.leaderNode) == null) { determineLeadership(); } byte[] leaderData = this.zooKeeper.getData().usingWatcher(new FindLeaderWatcher()).forPath(this.leaderNode); this.leaderMetadata = deserializeMetadata(leaderData); } catch (KeeperException exc) { reset(); } catch (Throwable exc) { log.error("Fatal failure.", exc); this.fatalFailure = true; } finally { lock.unlock(); } }
String element = itr.next(); String path = String.format("%s/%s", parent, element); this.curatorFramework.getData().usingWatcher(this).inBackground(this).forPath(path);
final byte[] coordinatorAddressBytes = curatorClient.getData().usingWatcher(new Watcher() { @Override public void process(final WatchedEvent event) {
public static byte[] zkFetch(CuratorFramework client, Watcher watcher, String path) { try { GetDataBuilder b = client.getData(); if ( watcher != null ) b.usingWatcher(watcher); return b.forPath(path); } catch (IllegalArgumentException ex) { return null; } catch (Exception ex) { //LOG.warn("Failed: zkFetch(" + path + ") " + ex.getMessage()); return null; } }
public static String getStringData(CuratorFramework curator, String path, Watcher watcher) throws Exception { byte[] bytes = watcher != null ? curator.getData().usingWatcher(watcher).forPath(path) : curator.getData().forPath(path); if (bytes == null) { return null; } else { return new String(bytes, UTF_8); } }
public static String getStringData(CuratorFramework curator, String path, Watcher watcher) throws Exception { byte[] bytes = watcher != null ? curator.getData().usingWatcher(watcher).forPath(path) : curator.getData().forPath(path); if (bytes == null) { return null; } else { return new String(bytes, UTF_8); } }
@Override public byte[] registerWatcher(String path, Watcher watcher) throws Exception { return client.getData().usingWatcher(watcher).forPath(path); }
private void readValueAndNotifyListenersInBackground() throws Exception { client.getData().usingWatcher(watcher).inBackground(upadateAndNotifyListenerCallback).forPath(path); }
private boolean setCancelWatcher(CuratorFramework curatorClient, String experimentId, String processId) throws Exception { String experimentNodePath = GFacUtils.getExperimentNodePath(experimentId); // /experiments/{experimentId}/cancelListener, set watcher for data changes String experimentCancelNode = ZKPaths.makePath(experimentNodePath, ZkConstants.ZOOKEEPER_CANCEL_LISTENER_NODE); byte[] bytes = curatorClient.getData().forPath(experimentCancelNode); if (bytes != null && new String(bytes).equalsIgnoreCase(ZkConstants.ZOOKEEPER_CANCEL_REQEUST)) { return true; } else { bytes = curatorClient.getData().usingWatcher(Factory.getCancelRequestWatcher(experimentId, processId)).forPath(experimentCancelNode); return bytes != null && new String(bytes).equalsIgnoreCase(ZkConstants.ZOOKEEPER_CANCEL_REQEUST); } }
@Override public BackgroundPathable<byte[]> usingWatcher(Watcher watcher) { return new BackgroundPathableDecorator<>(inner.usingWatcher(wrap(watcher))); }
@Override public BackgroundPathable<byte[]> usingWatcher(CuratorWatcher watcher) { return new BackgroundPathableDecorator<>(inner.usingWatcher(wrap(watcher))); } }
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); } }
@Override public TreeData load(String key) throws Exception { Stat stat = client.checkExists().forPath(key); if (stat!= null) { byte[] bytes = dataIsCompressed ? client.getData().decompressed().usingWatcher(watcher).forPath(key) : client.getData().usingWatcher(watcher).forPath(key); List<String> children = client.getChildren().usingWatcher(watcher).forPath(key); return new TreeData(key, stat, bytes, children); } else { return null; } } });
@Override public TreeData load(String key) throws Exception { Stat stat = client.checkExists().forPath(key); if (stat!= null) { byte[] bytes = dataIsCompressed ? client.getData().decompressed().usingWatcher(watcher).forPath(key) : client.getData().usingWatcher(watcher).forPath(key); List<String> children = client.getChildren().usingWatcher(watcher).forPath(key); return new TreeData(key, stat, bytes, children); } else { return null; } } });
private void doRefreshData() throws Exception { if ( dataIsCompressed ) { client.getData().decompressed().usingWatcher(this).inBackground(this).forPath(path); } else { client.getData().usingWatcher(this).inBackground(this).forPath(path); } }
private void findLeader() { ReentrantReadWriteLock.WriteLock lock = this.readWriteLock.writeLock(); lock.lock(); try { if (this.zooKeeper.checkExists().usingWatcher(new FindLeaderWatcher()).forPath(this.leaderNode) == null) { determineLeadership(); } byte[] leaderData = this.zooKeeper.getData().usingWatcher(new FindLeaderWatcher()).forPath(this.leaderNode); this.leaderMetadata = deserializeMetadata(leaderData); } catch (KeeperException exc) { reset(); } catch (Throwable exc) { log.error("Fatal failure.", exc); this.fatalFailure = true; } finally { lock.unlock(); } }
public void usingWatcher(ZookeeperWatcherType watcherType, String path) throws Exception { if (watcherType == null) { throw new ZookeeperException("Watcher type is null"); } PathUtils.validatePath(path); this.watcherType = watcherType; this.path = path; switch (watcherType) { case EXISTS: usingWatcher(client.checkExists().usingWatcher(this), path); break; case GET_CHILDREN: usingWatcher(client.getChildren().usingWatcher(this), path); break; case GET_DATA: usingWatcher(client.getData().usingWatcher(this), path); break; } }
void getDataAndStat(String fullPath) throws Exception { BackgroundCallback callback = (client, event) -> { if (event.getResultCode() == KeeperException.Code.OK.intValue()) { updateCache(fullPath, event.getStat(), cacheData ? event.getData() : null); } }; if (dataIsCompressed && cacheData) { client.getData().decompressed().usingWatcher(dataWatcher).inBackground(callback).forPath(fullPath); } else { client.getData().usingWatcher(dataWatcher).inBackground(callback).forPath(fullPath); } }