当前位置: 首页>>代码示例>>Java>>正文


Java ZooKeeper.exists方法代码示例

本文整理汇总了Java中org.apache.zookeeper.ZooKeeper.exists方法的典型用法代码示例。如果您正苦于以下问题:Java ZooKeeper.exists方法的具体用法?Java ZooKeeper.exists怎么用?Java ZooKeeper.exists使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.zookeeper.ZooKeeper的用法示例。


在下文中一共展示了ZooKeeper.exists方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testNodeCreated

import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testNodeCreated() throws Exception {
    QuorumUtil qu = new QuorumUtil(1);
    qu.startAll();

    EventsWatcher watcher = new EventsWatcher();
    ZooKeeper zk1 = createClient(qu, 1, watcher);
    ZooKeeper zk2 = createClient(qu, 2);

    String path = "/test1-created";

    zk1.exists(path, watcher);
    qu.shutdown(1);
    zk2.create(path, new byte[2], ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    qu.start(1);
    watcher.waitForConnected(TIMEOUT * 1000L);
    watcher.assertEvent(TIMEOUT, EventType.NodeCreated);

    qu.shutdownAll();
}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:22,代码来源:WatchEventWhenAutoResetTest.java

示例2: getTree

import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
private static String[] getTree(ZooKeeper zk, String path) throws Exception{
 if(zk.exists(path, false) == null){
  return new String[0];
 }
 List<String> dealList = new ArrayList<String>();
 dealList.add(path);
 int index =0;
 while(index < dealList.size()){
  String tempPath = dealList.get(index);
  List<String> children = zk.getChildren(tempPath, false);
  if(!"/".equalsIgnoreCase(tempPath)){
   tempPath = tempPath +"/";
  }
  Collections.sort(children);
  for (int i = children.size() - 1; i >= 0; i--) {
   dealList.add(index + 1, tempPath + children.get(i));
  }
  index++;
 }
 return dealList.toArray(new String[0]);
}
 
开发者ID:liuht777,项目名称:uncode-scheduler,代码行数:22,代码来源:ZKTools.java

示例3: getTree

import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public static String[] getTree(ZooKeeper zk,String path) throws Exception{
 if(zk.exists(path, false) == null){
  return new String[0];
 }
 List<String> dealList = new ArrayList<String>();
 dealList.add(path);
 int index =0;
 while(index < dealList.size()){
  String tempPath = dealList.get(index);
  List<String> children = zk.getChildren(tempPath, false);
  if(tempPath.equalsIgnoreCase("/") == false){
   tempPath = tempPath +"/";
  }
  Collections.sort(children);
  for(int i = children.size() -1;i>=0;i--){
   dealList.add(index+1, tempPath + children.get(i));
  }
  index++;
 }
 return (String[])dealList.toArray(new String[0]);
}
 
开发者ID:hungki,项目名称:tbschedule-wed,代码行数:22,代码来源:ZKTools.java

示例4: testBadAuthThenSendOtherCommands

import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testBadAuthThenSendOtherCommands() throws Exception {
    ZooKeeper zk = createClient();     
    try {        
        zk.addAuthInfo("INVALID", "BAR".getBytes());
        zk.exists("/foobar", false);             
        zk.getData("/path1", false, null);
        Assert.fail("Should get auth state error");
    } catch(KeeperException.AuthFailedException e) {
        if(!authFailed.await(CONNECTION_TIMEOUT,
                TimeUnit.MILLISECONDS))
        {
            Assert.fail("Should have called my watcher");
        }
    }
    finally {
        zk.close();          
    }
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:20,代码来源:AuthTest.java

示例5: testNullData

import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testNullData() throws IOException, 
    InterruptedException, KeeperException {
    String path = "/SIZE";
    ZooKeeper zk = null;
    zk = createClient();
    try {
        zk.create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        // try sync zk exists 
        zk.exists(path, false);
        zk.exists(path, false, this , null);
        cn.await(10, TimeUnit.SECONDS);
        Assert.assertSame(0L, cn.getCount());
    } finally {
        if(zk != null)
            zk.close();
    }
    
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:20,代码来源:NullDataTest.java

示例6: processResult

import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Override
public void processResult(int rc, String path, Object ctx, String name) {
  if (rc == KeeperException.Code.NODEEXISTS.intValue()) {
    LOG.warn("Node for " + path + " already exists");
  } else if (rc != 0) {
    // This is result code.  If non-zero, need to resubmit.
    LOG.warn("rc != 0 for " + path + " -- retryable connectionloss -- " +
      "FIX see http://wiki.apache.org/hadoop/ZooKeeper/FAQ#A2");
    this.counter.addAndGet(1);
    return;
  }

  if (LOG.isDebugEnabled()) {
    LOG.debug("rs=" + ctx + ", server=" + destination);
  }
  // Async exists to set a watcher so we'll get triggered when
  // unassigned node changes.
  ZooKeeper zk = this.zkw.getRecoverableZooKeeper().getZooKeeper();
  zk.exists(path, this.zkw, callBack, ctx);
}
 
开发者ID:fengchen8086,项目名称:ditb,代码行数:21,代码来源:OfflineCallback.java

示例7: ZkDistributeLock

import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
 * @param zkServers
 * @param lockName
 * @param sessionTimeout
 */
public ZkDistributeLock(String zkServers, String lockName, int sessionTimeout) {
    if (lockName.contains(LOCK_KEY_SUFFIX)) {
        throw new LockException("lockName 不能包含[" + LOCK_KEY_SUFFIX + "]");
    }
    this.lockName = lockName;
    this.sessionTimeout = sessionTimeout;
    try {
        zk = new ZooKeeper(zkServers, sessionTimeout, this);
        Stat stat = zk.exists(ROOT_PATH, false);
        if (stat == null) {
            // 创建根节点
            zk.create(ROOT_PATH, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
    } catch (Exception e) {
        throw new LockException(e);
    }
}
 
开发者ID:warlock-china,项目名称:azeroth,代码行数:23,代码来源:ZkDistributeLock.java

示例8: deleteZKDir

import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
private void deleteZKDir(ZooKeeper zk, String nodeName)
        throws IOException, InterruptedException, KeeperException {

  Stat stat = zk.exists(nodeName, false);
  if (stat == null) {
    return;
  }

  List<String> children1 = zk.getChildren(nodeName, false);
  List<String> c2 = zk.getChildren(nodeName, false, stat);

  if (!children1.equals(c2)) {
      Assert.fail("children lists from getChildren()/getChildren2() do not match");
  }

  if (!stat.equals(stat)) {
      Assert.fail("stats from exists()/getChildren2() do not match");
  }

  if (children1.size() == 0) {
    zk.delete(nodeName, -1);
    return;
  }
  for (String n : children1) {
    deleteZKDir(zk, n);
  }
}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:28,代码来源:ZooKeeperTestClient.java

示例9: testPing

import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/** Verify that pings are sent, keeping the "idle" client alive */
@Test
public void testPing() throws Exception {
    ZooKeeper zkIdle = null;
    ZooKeeper zkWatchCreator = null;
    try {
        CountdownWatcher watcher = new CountdownWatcher();
        zkIdle = createClient(watcher, hostPort, 10000);

        zkWatchCreator = createClient();

        for (int i = 0; i < 10; i++) {
            zkWatchCreator.create("/" + i, new byte[0], Ids.OPEN_ACL_UNSAFE,
                    CreateMode.PERSISTENT);
        }
        for (int i = 0; i < 10; i++) {
            zkIdle.exists("/" + i, true);
        }
        for (int i = 0; i < 10; i++) {
            Thread.sleep(1000);
            zkWatchCreator.delete("/" + i, -1);
        }
        // The bug will manifest itself here because zkIdle will expire
        zkIdle.exists("/0", false);
    } finally {
        if (zkIdle != null) {
            zkIdle.close();
        }
        if (zkWatchCreator != null) {
            zkWatchCreator.close();
        }
    }
}
 
开发者ID:l294265421,项目名称:ZooKeeper,代码行数:34,代码来源:ClientTest.java

示例10: testNodeDataChanged

import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testNodeDataChanged() throws Exception {
    QuorumUtil qu = new QuorumUtil(1);
    qu.startAll();

    EventsWatcher watcher = new EventsWatcher();
    ZooKeeper zk1 = createClient(qu, 1, watcher);
    ZooKeeper zk2 = createClient(qu, 2);

    String path = "/test-changed";

    zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    zk1.getData(path, watcher, null);
    qu.shutdown(1);
    zk2.delete(path, -1);
    zk2.create(path, new byte[2], ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    qu.start(1);
    watcher.waitForConnected(TIMEOUT);
    watcher.assertEvent(TIMEOUT, EventType.NodeDataChanged);

    zk1.exists(path, watcher);
    qu.shutdown(1);
    zk2.delete(path, -1);
    zk2.create(path, new byte[2], ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    qu.start(1);
    watcher.waitForConnected(TIMEOUT * 1000L);
    watcher.assertEvent(TIMEOUT, EventType.NodeDataChanged);

    qu.shutdownAll();
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:34,代码来源:WatchEventWhenAutoReset.java

示例11: testNodeDeleted

import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testNodeDeleted() throws Exception {
    QuorumUtil qu = new QuorumUtil(1);
    qu.startAll();

    EventsWatcher watcher = new EventsWatcher();
    ZooKeeper zk1 = createClient(qu, 1, watcher);
    ZooKeeper zk2 = createClient(qu, 2);

    String path = "/test-deleted";

    zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    zk1.getData(path, watcher, null);
    qu.shutdown(1);
    zk2.delete(path, -1);
    qu.start(1);
    watcher.waitForConnected(TIMEOUT * 1000L);
    watcher.assertEvent(TIMEOUT, EventType.NodeDeleted);

    zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    zk1.exists(path, watcher);
    qu.shutdown(1);
    zk2.delete(path, -1);
    qu.start(1);
    watcher.waitForConnected(TIMEOUT * 1000L);
    watcher.assertEvent(TIMEOUT, EventType.NodeDeleted);

    zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    zk1.getChildren(path, watcher);
    qu.shutdown(1);
    zk2.delete(path, -1);
    qu.start(1);
    watcher.waitForConnected(TIMEOUT * 1000L);
    watcher.assertEvent(TIMEOUT, EventType.NodeDeleted);

    qu.shutdownAll();
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:41,代码来源:WatchEventWhenAutoReset.java

示例12: testUpgrade

import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
 * test the upgrade
 * @throws Exception
 */
@Test
public void testUpgrade() throws Exception {
    File upgradeDir = new File(testData, "upgrade");
    UpgradeMain upgrade = new UpgradeMain(upgradeDir, upgradeDir);
    upgrade.runUpgrade();
    ZooKeeperServer zks = new ZooKeeperServer(upgradeDir, upgradeDir, 3000);
    SyncRequestProcessor.setSnapCount(1000);
    final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
    f.startup(zks);
    LOG.info("starting up the zookeeper server .. waiting");
    Assert.assertTrue("waiting for server being up",
            ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
    ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this);
    Stat stat = zk.exists("/", false);
    List<String> children = zk.getChildren("/", false);
    Collections.sort(children);
    for (int i = 0; i < 10; i++) {
        Assert.assertTrue("data tree sanity check",
                ("test-" + i).equals(children.get(i)));
    }
    //try creating one node
    zk.create("/upgrade", "upgrade".getBytes(), Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    // check if its there
    if (zk.exists("/upgrade", false) == null) {
        Assert.assertTrue(false);
    }

    zk.close();

    // bring down the server
    f.shutdown();
    Assert.assertTrue("waiting for server down",
               ClientBase.waitForServerDown(HOSTPORT,
                       ClientBase.CONNECTION_TIMEOUT));

}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:43,代码来源:UpgradeTest.java

示例13: testCreatePersistent

import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
 * When local session is enabled, leader will allow persistent node
 * to be create for unknown session
 */
@Test
public void testCreatePersistent() throws Exception {
    qu.enableLocalSession(true);
    qu.startAll();

    QuorumPeer leader = qu.getLeaderQuorumPeer();

    ZooKeeper zk = ClientBase.createZKClient(qu.getConnectString(leader));

    CreateRequest createRequest = new CreateRequest("/success",
            new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT.toFlag());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
    createRequest.serialize(boa, "request");
    ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());

    // Mimic sessionId generated by follower's local session tracker
    long sid = qu.getFollowerQuorumPeers().get(0).getActiveServer()
            .getServerId();
    long locallSession = (sid << 56) + 1;

    LOG.info("Local session Id: " + Long.toHexString(locallSession));

    Request request = new Request(null, locallSession, 0, OpCode.create,
            bb, new ArrayList<Id>());

    // Submit request directly to leader
    leader.getActiveServer().submitRequest(request);

    // Make sure that previous request is finished
    zk.create("/ok", new byte[0], Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);

    Stat stat = zk.exists("/success", null);
    Assert.assertTrue("Request from local sesson failed", stat != null);

}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:42,代码来源:LeaderSessionTrackerTest.java

示例14: loadNode

import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public void loadNode(ZkNode zkNode) throws KeeperException, InterruptedException {
    ZooKeeper zk = zookeeprClientFactory.createZookeeper();
    Stat stat = zk.exists(zkNode.getPath(), null);
    if(stat!=null){
        byte[] data = zk.getData(zkNode.getPath(), null, stat);
        zkNode.setContent(zkNode.parse(data));
    }
}
 
开发者ID:liaojiacan,项目名称:zkAdmin,代码行数:9,代码来源:ZookeeperManager.java

示例15: utestExists

import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
private void utestExists(int port)
    throws IOException, InterruptedException, KeeperException
{
    ZooKeeper zk =
        new ZooKeeper("127.0.0.1:" + port, CONNECTION_TIMEOUT, this);
    for (int i = 0; i < 10000; i++) {
        zk.exists("/this/path/doesnt_exist!", true);
    }
    zk.close();
}
 
开发者ID:l294265421,项目名称:ZooKeeper,代码行数:11,代码来源:OOMTest.java


注:本文中的org.apache.zookeeper.ZooKeeper.exists方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。