使用curator treeCache时,如何确保缓存已准备好?

时间:2016-06-15 03:11:08

标签: java apache-zookeeper apache-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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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时,缓存就绪。