3

使用 curatortreeCache时,如何确保缓存已准备就绪?

之后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
4

2 回答 2

3

您可以为 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);
于 2016-08-14T07:12:02.270 回答
1

从 getCurrentChildren 的代码

 public Map<String, ChildData> getCurrentChildren(String fullPath)
{
    TreeNode node = find(fullPath);
    if ( node == null || node.nodeState.get() != NodeState.LIVE )
    {
        return null;
    }
    ConcurrentMap<String, TreeNode> map = node.children.get();
    Map<String, ChildData> result;
    if ( map == null )
    {
        result = ImmutableMap.of();
    }
    else
    {
        ImmutableMap.Builder<String, ChildData> builder = ImmutableMap.builder();
        for ( Map.Entry<String, TreeNode> entry : map.entrySet() )
        {
            TreeNode childNode = entry.getValue();
            ChildData childData = new ChildData(childNode.path, childNode.stat.get(), childNode.data.get());
            // Double-check liveness after retreiving data.
            if ( childNode.nodeState.get() == NodeState.LIVE )
            {
                builder.put(entry.getKey(), childData);
            }
        }
        result = builder.build();
    }

    // Double-check liveness after retreiving children.
    return node.nodeState.get() == NodeState.LIVE ? result : null;
}

可以看到,当 NodeState 为 PENDING 或 DEAD 或不存在时,它会返回 null,当 NodeState 为 LIVE 时,它会返回一个 Map 实例。因此,当返回值不为 null 时,缓存已准备就绪。

于 2016-07-30T11:53:46.853 回答