gpt4 book ai didi

java - 使用 curator treeCache 时,如何确保缓存准备就绪?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:05:45 25 4
gpt4 key购买 nike

使用 curator treeCache 时,如何确保缓存准备就绪?

cache.start() 之后,如果我立即调用 getCurrentData,它将返回 null,那么如何确保缓存准备就绪?,有人可以给我举个例子吗?谢谢

client = CuratorFrameworkFactory.builder()
.connectString(connectionString)
.retryPolicy(new ExponentialBackoffRetry(zkConnectionTimeoutMs, 3))
.sessionTimeoutMs(zkSessionTimeoutMs)
.build();
client.start();

cache = new TreeCache(client, rootPath);
cache.start();
ChildData child = cache.getCurrentData(rootPath); // child is null
Thread.sleep(50); // must sleep for a while
child = cache.getCurrentData(rootPath); // child is ok

最佳答案

您可以为 Treecache 添加一个监听器并监听 INITIALIZED 事件。

    Semaphore sem = new Semaphore(0);
client = CuratorFrameworkFactory.builder()
.connectString(connectionString)
.retryPolicy(new ExponentialBackoffRetry(zkConnectionTimeoutMs, 3))
.sessionTimeoutMs(zkSessionTimeoutMs)
.build();
client.start();

cache = new TreeCache(client, rootPath);
cache.start();

TreeCacheListener listener = new TreeCacheListener() {

@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event)
throws Exception {
switch (event.getType()) {
case INITIALIZED: {

sem.release();

}

}

};
cache.getListenable().addListener(listener);
sem.acquire();
child = cache.getCurrentData(rootPath);

关于java - 使用 curator treeCache 时,如何确保缓存准备就绪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37825464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com