public static void setDataAsyncWithCallback(CuratorFramework client, BackgroundCallback callback, String path, byte[] payload) throws Exception { // this is another method of getting notification of an async completion client.setData().inBackground(callback).forPath(path, payload); }
@Override public ErrorListenerPathAndBytesable<Stat> inBackground() { return new ErrorListenerPathAndBytesableDecorator<>(inner.inBackground()); }
@Override public ErrorListenerPathAndBytesable<Stat> inBackground(Object context) { return new ErrorListenerPathAndBytesableDecorator<>(inner.inBackground(context)); }
public static void setDataAsyncWithCallback(CuratorFramework client, BackgroundCallback callback, String path, byte[] payload) throws Exception { // this is another method of getting notification of an async completion client.setData().inBackground(callback).forPath(path, payload); }
protected void checkAndWriteString(String parentPath, String currPath, String value) throws Exception { checkNotNull(parentPath, "data of path" + parentPath + " must be not null!"); checkNotNull(currPath, "data of path" + currPath + " must be not null!"); checkNotNull(value, "data of value:" + value + " must be not null!"); String nodePath = ZKPaths.makePath(parentPath, currPath); Stat stat = curator.checkExists().forPath(nodePath); if (null == stat) { this.createPath(nodePath); } LOGGER.debug("ZkMultiLoader write file :" + nodePath + ", value :" + value); curator.setData().inBackground().forPath(nodePath, value.getBytes()); }
@Override public ErrorListenerPathAndBytesable<Stat> inBackground(BackgroundCallback callback) { return new ErrorListenerPathAndBytesableDecorator<>(inner.inBackground(wrap(callback), executor)); }
@Override public ErrorListenerPathAndBytesable<Stat> inBackground(BackgroundCallback callback, Object context, Executor executor) { return new ErrorListenerPathAndBytesableDecorator<>(inner.inBackground(wrap(callback), context, executor)); }
@Override public ErrorListenerPathAndBytesable<Stat> inBackground(BackgroundCallback callback, Object context) { return new ErrorListenerPathAndBytesableDecorator<>(inner.inBackground(wrap(callback), context, executor)); }
@Override public ErrorListenerPathAndBytesable<Stat> inBackground(BackgroundCallback callback, Executor executor) { return new ErrorListenerPathAndBytesableDecorator<>(inner.inBackground(wrap(callback), executor)); }
/** * Set data that node should set in ZK also writes the data to the node. NOTE: it * is an error to call this method after {@link #start()} but before the initial create * has completed. Use {@link #waitForInitialCreate(long, TimeUnit)} to ensure initial * creation. * * @param data new data value * @throws Exception errors */ public void setData(byte[] data) throws Exception { data = Preconditions.checkNotNull(data, "data cannot be null"); Preconditions.checkState(nodePath.get() != null, "initial create has not been processed. Call waitForInitialCreate() to ensure."); this.data.set(Arrays.copyOf(data, data.length)); if ( isActive() ) { client.setData().inBackground().forPath(getActualPath(), getData()); } }
public static void setDataAsync(CuratorFramework client, String path, byte[] payload) throws Exception { // this is one method of getting event/async notifications CuratorListener listener = new CuratorListener() { @Override public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception { // examine event for details } }; client.getCuratorListenable().addListener(listener); // set data for the given node asynchronously. The completion // notification // is done via the CuratorListener. client.setData().inBackground().forPath(path, payload); }
public static void setDataAsync(CuratorFramework client, String path, byte[] payload) throws Exception { // this is one method of getting event/async notifications CuratorListener listener = new CuratorListener() { @Override public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception { // examine event for details } }; client.getCuratorListenable().addListener(listener); // set data for the given node asynchronously. The completion notification // is done via the CuratorListener. client.setData().inBackground().forPath(path, payload); }
/** * used in DataChangeListener, call this to refresh cache data. */ public void dataChanged(){ BuguCache cache = BuguCache.getInstance(); CuratorFramework zkClient = cache.getZkClient(); //process single JVM if(zkClient == null){ reloadCacheData(); } //process cluster JVM, by ZooKeeper/curator else{ //set new value to ZK node, in order to trigger nodeChanged event String path = BuguCache.ZK_PREFIX + clazz.getName(); String data = String.valueOf(System.currentTimeMillis()); try { zkClient.setData().inBackground().forPath(path, data.getBytes()); } catch (Exception ex) { throw new BuguCacheException(ex.getMessage()); } } }
Future<Stat> setData(String path, V v) { Future<Stat> future = Future.future(); try { curator.setData().inBackground((client, event) -> { if (event.getType() == CuratorEventType.SET_DATA) { vertx.runOnContext(e -> future.complete(event.getStat())); } }).forPath(path, asByte(v)); } catch (Exception ex) { vertx.runOnContext(event -> future.fail(ex)); } return future; }
@Override public void update(Record record, Handler<AsyncResult<Void>> resultHandler) { Objects.requireNonNull(record.getRegistration(), "No registration id in the record"); Context context = Vertx.currentContext(); ensureConnected(x -> { if (x.failed()) { resultHandler.handle(Future.failedFuture(x.cause())); } else { try { client.setData() .inBackground((framework, event) -> runOnContextIfPossible(context, () -> { if (event.getResultCode() == KeeperException.Code.OK.intValue()) { resultHandler.handle(Future.succeededFuture()); } else { KeeperException.Code code = KeeperException.Code.get(event.getResultCode()); resultHandler.handle(Future.failedFuture(KeeperException.create(code))); } })) .withUnhandledErrorListener((message, e) -> resultHandler.handle(Future.failedFuture(e))) .forPath(getPath(record.getRegistration()), record.toJson().encode().getBytes(CHARSET)); } catch (Exception e) { resultHandler.handle(Future.failedFuture(e)); } } }); }
CompletableFuture<Integer> setData(final String path, final Data data) { final CompletableFuture<Integer> result = new CompletableFuture<>(); try { if (data.getVersion() == null) { client.setData().inBackground( callback(event -> result.complete(event.getStat().getVersion()), result::completeExceptionally, path), executor) .forPath(path, data.getData()); } else { client.setData().withVersion(data.getVersion().asIntVersion().getIntValue()).inBackground( callback(event -> result.complete(event.getStat().getVersion()), result::completeExceptionally, path), executor) .forPath(path, data.getData()); } } catch (Exception e) { result.completeExceptionally(StoreException.create(StoreException.Type.UNKNOWN, e, path)); } return result; }
private void writeSomeConf(String path, boolean create, Handler<AsyncResult<Void>> handler) { Context context = vertx.getOrCreateContext(); try { if (create) { JsonObject conf = new JsonObject().put("some-key", "some-value"); client.create() .creatingParentsIfNeeded() .inBackground((client, event) -> dataWrittenCallback(handler, context, event)) .forPath(path, conf.encode().getBytes()); } else { JsonObject conf = new JsonObject().put("some-key", "some-value-2"); client.setData() .inBackground((client, event) -> dataWrittenCallback(handler, context, event)) .forPath(path, conf.encode().getBytes()); } } catch (Exception e) { handler.handle(Future.failedFuture(e)); } }
client.setData().inBackground(setDataCallback).forPath(getActualPath(), getData());