2016-06-15 8 views
2

キュレーターtreeCacheを使用している場合、どのようにキャッシュが準備されていることを確認できますか? cache.start()キュレーターtreeCacheを使用する場合、どのようにしてキャッシュが準備されていることを確認できますか?

私はすぐにgetCurrentDataを呼び出す場合、それはnullを返しますので、どのように私はキャッシュの準備ができていることを確認することができます?誰かが私に例を与えることができますか?あなたはNodeStateが保留中またはDEADまたは存在していないされている場合NodeStateは、それが返されますLIVEされたとき、それは、nullを返しますことがわかります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; 
} 

のコードから感謝

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 

答えて

1

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); 
+0

誰でもこれより優れた解決策が見つかりましたか? –

1

マップインスタンス。したがって、戻り値がnullでない場合、キャッシュは準備完了です。

関連する問題