3

curator を使用treeCacheする場合、キャッシュの準備ができていることを確認するにはどうすればよいですか?

の後、すぐcache.start()に呼び出すと が返されるので、キャッシュの準備ができていることを確認するにはどうすればよいですか? 誰か例を教えてください。ありがとうgetCurrentDatanull

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

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 の場合、または NodeState が存在しない場合は null が返され、NodeState が LIVE の場合は Map インスタンスが返されることがわかります。したがって、戻り値が null でない場合、キャッシュは準備完了です。

于 2016-07-30T11:53:46.853 に答える