String global = children.get(i); String path = String.format("%s/%s", parent, global); this.curatorFramework.getData().inBackground(this).forPath(path);
private <T> T queryAndReturnResultsThrows(final T results, final Collection<String> paths, final BackgroundCallback callback, final CountDownLatch latch, final String pathNameForLogs, final AtomicInteger bytes, final CuratorQueryMethod method) throws Exception { final long start = System.currentTimeMillis(); try { for (String path : paths) { switch (method) { case GET_DATA: curator.getData().inBackground(callback).forPath(path); break; case GET_CHILDREN: curator.getChildren().inBackground(callback).forPath(path); break; case CHECK_EXISTS: default: curator.checkExists().inBackground(callback).forPath(path); break; } } checkLatch(latch, pathNameForLogs); } finally { log(method.operationType, Optional.of(paths.size()), bytes.get() > 0 ? Optional.of(bytes.get()) : Optional.<Integer>absent(), start, pathNameForLogs); } return results; }
@Override public ErrorListenerPathable<byte[]> inBackground(Object context) { return new ErrorListenerPathableDecorator<>(inner.inBackground(context)); }
@Override public ErrorListenerPathable<byte[]> inBackground() { return new ErrorListenerPathableDecorator<>(inner.inBackground()); }
public <T> Map<String, T> fetchDataInParallel(Collection<String> paths, Function<byte[], T> transformFunction) throws Exception { Map<String, T> dataMap = new ConcurrentHashMap<>(); CountDownLatch countDownLatch = new CountDownLatch(paths.size()); Queue<KeeperException> exceptions = new ConcurrentLinkedQueue<>(); BackgroundCallback callback = new GetDataCallback<>(dataMap, transformFunction, countDownLatch, exceptions); for (String path : paths) { curatorFramework.getData().inBackground(callback).forPath(path); } waitAndThrowExceptions(countDownLatch, exceptions); return dataMap; }
@Override public ErrorListenerPathable<byte[]> inBackground(BackgroundCallback callback, Object context) { return new ErrorListenerPathableDecorator<>(inner.inBackground(wrap(callback), context, executor)); }
@Override public ErrorListenerPathable<byte[]> inBackground(BackgroundCallback callback, Object context, Executor executor) { return new ErrorListenerPathableDecorator<>(inner.inBackground(wrap(callback), context, executor)); }
@Override public ErrorListenerPathable<byte[]> inBackground(BackgroundCallback callback) { return new ErrorListenerPathableDecorator<>(inner.inBackground(wrap(callback), executor)); }
@Override public ErrorListenerPathable<byte[]> inBackground(BackgroundCallback callback, Executor executor) { return new ErrorListenerPathableDecorator<>(inner.inBackground(wrap(callback), executor)); }
/** * * @描述:第二种监听器的添加方式: * 也是一次性的监听操作,使用后就无法在继续监听了 * @return void * @exception * @createTime:2016年5月18日 * @author: songqinghu * @throws Exception */ private static void setListenterTwo(CuratorFramework client) throws Exception{ ExecutorService pool = Executors.newCachedThreadPool(); CuratorListener listener = new CuratorListener() { @Override public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception { System.out.println("监听器 : "+ event.toString()); } }; client.getCuratorListenable().addListener(listener,pool); client.getData().inBackground().forPath("/two"); client.getData().inBackground().forPath("/two"); client.getData().inBackground().forPath("/two"); client.getData().inBackground().forPath("/two"); Thread.sleep(Long.MAX_VALUE ); } /**
private <T> T queryAndReturnResultsThrows(final T results, final Collection<String> paths, final BackgroundCallback callback, final CountDownLatch latch, final String pathNameForLogs, final AtomicInteger bytes, final CuratorQueryMethod method) throws Exception { final long start = System.currentTimeMillis(); try { for (String path : paths) { switch (method) { case GET_DATA: curator.getData().inBackground(callback).forPath(path); break; case GET_CHILDREN: curator.getChildren().inBackground(callback).forPath(path); break; case CHECK_EXISTS: default: curator.checkExists().inBackground(callback).forPath(path); break; } } checkLatch(latch, pathNameForLogs); } finally { log(method.operationType, Optional.of(paths.size()), bytes.get() > 0 ? Optional.of(bytes.get()) : Optional.<Integer>absent(), start, pathNameForLogs); } return results; }
private CompletableFuture<byte[]> readNode(String path) { CompletableFuture<byte[]> result = new CompletableFuture<>(); try { client.getData().inBackground((cli, event) -> { if (event.getResultCode() == KeeperException.Code.OK.intValue()) { result.complete(event.getData()); } else if (event.getResultCode() == KeeperException.Code.NONODE.intValue()) { log.debug("Node {} does not exist.", path); result.complete(null); } else { result.completeExceptionally(translateErrorCode(path, event)); } }, executor).forPath(path); } catch (Exception e) { result.completeExceptionally(StoreException.create(StoreException.Type.UNKNOWN, e)); } return result; }
private void getRecordById(Context context, String uuid, Handler<Record> handler) { ensureConnected(x -> { if (x.failed()) { handler.handle(null); } else { try { client.getData() .inBackground((curatorFramework, curatorEvent) -> runOnContextIfPossible(context, () -> { if (curatorEvent.getResultCode() == KeeperException.Code.OK.intValue()) { JsonObject json = new JsonObject(new String(curatorEvent.getData(), CHARSET)); handler.handle(new Record(json)); } else { handler.handle(null); } })) .forPath(getPath(uuid)); } catch (Exception e) { handler.handle(null); } } }); }
CompletableFuture<Data> getData(final String path) { final CompletableFuture<Data> result = new CompletableFuture<>(); try { client.getData().inBackground( callback(event -> result.complete(new Data(event.getData(), new Version.IntVersion(event.getStat().getVersion()))), result::completeExceptionally, path), executor) .forPath(path); } catch (Exception e) { result.completeExceptionally(StoreException.create(StoreException.Type.UNKNOWN, e, path)); } return result; }
try { client.getData() .inBackground((fmk, curatorEvent) -> runOnContextIfPossible(context, () -> { if (curatorEvent.getResultCode() == KeeperException.Code.OK.intValue()) {
.inBackground((client, event) -> { if (context != null) { context.runOnContext(x -> retrieve(event, completionHandler));
curator.sync().inBackground((syncClient, syncEvent) -> { if (syncEvent.getType() == CuratorEventType.SYNC) { curator.getData().inBackground((getClient, getEvent) -> { if (stat == null || stat.getMtime() <= getEvent.getStat().getMtime()) { vertx.runOnContext(aVoid -> future.complete());
curatorClient.getData().inBackground((client1, event) -> { callBackCalled.complete(null); }).forPath("/test");