org.apache.zookeeper.AsyncCallback.DataCallback Java Examples

The following examples show how to use org.apache.zookeeper.AsyncCallback.DataCallback. 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: ZookeeperVerticle.java    From examples with Apache License 2.0 6 votes vote down vote up
private DataCallback getVersionDataCallback() {
    return new DataCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, byte[] rawData, Stat s) {
            System.out.println("getVersionDataCallback.processResult - " + path + " - " + rc);
            if (rc != 0) {
                System.out.println(KeeperException.Code.get(rc));
                return;
            }
            ZookeeperVerticle zv = (ZookeeperVerticle) ctx;
            int version = -1;
            JsonObject data = new JsonObject(new String(rawData));
            synchronized (zv.configVersion) {
                version = zv.configVersion.get();
                System.out.println("getVersionDataCallback.processResult - " + path + " - "
                        + Constants.CONFIGURATION_PATH + "/" + version);
                if (path.equals(Constants.CONFIGURATION_PATH + "/" + version)) {
                    synchronized (WebsiteMain.jsonObject) {
                        WebsiteMain.jsonObject.clear();
                        WebsiteMain.jsonObject.mergeIn(data);
                    }
                }
            }
        }
    };
}
 
Example #2
Source File: ZookeeperVerticle.java    From examples with Apache License 2.0 6 votes vote down vote up
private DataCallback getVersionCallback() {
    return new DataCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, byte[] rawData, Stat s) {
            ZookeeperVerticle zv = (ZookeeperVerticle) ctx;
            int version = -1;
            synchronized (zv.configVersion) {
                version = zv.configVersion.get();
            }
            int fetchedVersion = new Integer(new String(rawData)).intValue();
            if (fetchedVersion > version) {
                synchronized (zv.configVersion) {
                    zv.configVersion.set(fetchedVersion);
                }
                zv.zk.getData(Constants.CONFIGURATION_PATH + "/" + fetchedVersion, false, getVersionDataCallback(),
                        zv);
            }
        }
    };
}
 
Example #3
Source File: ZookeeperCacheTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * This tests verifies that {{@link ZooKeeperDataCache} invalidates the cache if the get-operation time-out on that
 * path.
 *
 * @throws Exception
 */
@Test
public void testTimedOutZKCacheRequestInvalidates() throws Exception {

    OrderedScheduler executor = OrderedScheduler.newSchedulerBuilder().build();
    ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(2);
    ExecutorService zkExecutor = Executors.newSingleThreadExecutor(new DefaultThreadFactory("mockZk"));
    MockZooKeeper zkSession = spy(MockZooKeeper.newInstance(MoreExecutors.newDirectExecutorService()));

    String path = "test";
    doNothing().when(zkSession).getData(anyString(), any(Watcher.class), any(DataCallback.class), any());
    zkClient.create("/test", new byte[0], null, null);

    // add readOpDelayMs so, main thread will not serve zkCacahe-returned future and let zkExecutor-thread handle
    // callback-result process
    ZooKeeperCache zkCacheService = new LocalZooKeeperCache(zkSession, 1, executor);
    ZooKeeperDataCache<String> zkCache = new ZooKeeperDataCache<String>(zkCacheService) {
        @Override
        public String deserialize(String key, byte[] content) throws Exception {
            return new String(content);
        }
    };

    // try to do get on the path which will time-out and async-cache will have non-completed Future
    try {
        zkCache.get(path);
    } catch (Exception e) {
        // Ok
    }

    retryStrategically((test) -> {
        return zkCacheService.dataCache.getIfPresent(path) == null;
    }, 5, 1000);

    assertNull(zkCacheService.dataCache.getIfPresent(path));

    executor.shutdown();
    zkExecutor.shutdown();
    scheduledExecutor.shutdown();
}
 
Example #4
Source File: MockZooKeeper.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public void getData(final String path, boolean watch, final DataCallback cb, final Object ctx) {
    executor.execute(() -> {
        checkReadOpDelay();
        Optional<KeeperException.Code> failure = programmedFailure(Op.GET, path);
        if (failure.isPresent()) {
            cb.processResult(failure.get().intValue(), path, ctx, null, null);
            return;
        } else if (stopped) {
            cb.processResult(KeeperException.Code.ConnectionLoss, path, ctx, null, null);
            return;
        }

        Pair<byte[], Integer> value;
        mutex.lock();
        try {
            value = tree.get(path);
        } finally {
            mutex.unlock();
        }

        if (value == null) {
            cb.processResult(KeeperException.Code.NoNode, path, ctx, null, null);
        } else {
            Stat stat = new Stat();
            stat.setVersion(value.getRight());
            cb.processResult(0, path, ctx, value.getLeft(), stat);
        }
    });
}
 
Example #5
Source File: MockZooKeeper.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public void getData(final String path, final Watcher watcher, final DataCallback cb, final Object ctx) {
    executor.execute(() -> {
        checkReadOpDelay();
        mutex.lock();
        Optional<KeeperException.Code> failure = programmedFailure(Op.GET, path);
        if (failure.isPresent()) {
            mutex.unlock();
            cb.processResult(failure.get().intValue(), path, ctx, null, null);
            return;
        } else if (stopped) {
            mutex.unlock();
            cb.processResult(KeeperException.Code.CONNECTIONLOSS.intValue(), path, ctx, null, null);
            return;
        }

        Pair<byte[], Integer> value = tree.get(path);
        if (value == null) {
            mutex.unlock();
            cb.processResult(KeeperException.Code.NONODE.intValue(), path, ctx, null, null);
        } else {
            if (watcher != null) {
                watchers.put(path, watcher);
            }

            Stat stat = new Stat();
            stat.setVersion(value.getRight());
            mutex.unlock();
            cb.processResult(0, path, ctx, value.getLeft(), stat);
        }
    });
}
 
Example #6
Source File: ZooKeeperClient.java    From Mario with Apache License 2.0 4 votes vote down vote up
public void getData(String path, boolean watch, DataCallback cb, Object ctx) {
    zk.getData(path, watch, cb, ctx);
}
 
Example #7
Source File: DataCallbackable.java    From curator with Apache License 2.0 2 votes vote down vote up
/**
 * Passes a callback and a context object to the config/reconfig command.
 * @param callback  The async callback to use.
 * @param ctx       An object that will be passed to the callback.
 * @return this
 */
T usingDataCallback(DataCallback callback, Object ctx);