當前位置: 首頁>>代碼示例>>Java>>正文


Java ZooKeeper.multi方法代碼示例

本文整理匯總了Java中org.apache.zookeeper.ZooKeeper.multi方法的典型用法代碼示例。如果您正苦於以下問題:Java ZooKeeper.multi方法的具體用法?Java ZooKeeper.multi怎麽用?Java ZooKeeper.multi使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.zookeeper.ZooKeeper的用法示例。


在下文中一共展示了ZooKeeper.multi方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testMultiToFollower

import org.apache.zookeeper.ZooKeeper; //導入方法依賴的package包/類
/**
 * Tests if a multiop submitted to a non-leader propagates to the leader properly
 * (see ZOOKEEPER-1124).
 * 
 * The test works as follows. It has a client connect to a follower and submit a multiop
 * to the follower. It then verifies that the multiop successfully gets committed by the leader.
 *
 * Without the fix in ZOOKEEPER-1124, this fails with a ConnectionLoss KeeperException.
 */
@Test
public void testMultiToFollower() throws Exception {
    QuorumUtil qu = new QuorumUtil(1);
    CountdownWatcher watcher = new CountdownWatcher();
    qu.startQuorum();
    
    int index = 1;
    while(qu.getPeer(index).peer.leader == null) {
        index++;
    }
    
    ZooKeeper zk = new ZooKeeper(
            "127.0.0.1:" + qu.getPeer((index == 1)?2:1).peer.getClientPort(),
            ClientBase.CONNECTION_TIMEOUT, watcher);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    
    List<OpResult> results = new ArrayList<OpResult>();

    results = zk.multi(Arrays.asList(
            Op.create("/multi0", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
            Op.create("/multi1", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
            Op.create("/multi2", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)
            ));
    zk.getData("/multi0", false, null);
    zk.getData("/multi1", false, null);
    zk.getData("/multi2", false, null);

    zk.close();

    qu.tearDown();
}
 
開發者ID:maoling,項目名稱:fuck_zookeeper,代碼行數:41,代碼來源:FollowerTest.java

示例2: multi

import org.apache.zookeeper.ZooKeeper; //導入方法依賴的package包/類
private List<OpResult> multi(ZooKeeper zk, Iterable<Op> ops)
throws KeeperException, InterruptedException {
    if (useAsync) {
        final MultiResult res = new MultiResult();
        zk.multi(ops, new MultiCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx,
                                      List<OpResult> opResults) {
                synchronized (res) {
                    res.rc = rc;
                    res.results = opResults;
                    res.finished = true;
                    res.notifyAll();
                }
            }
        }, null);
        synchronized (res) {
            while (!res.finished) {
                res.wait();
            }
        }
        if (KeeperException.Code.OK.intValue() != res.rc) {
            KeeperException ke = KeeperException.create(KeeperException.Code.get(res.rc));
            throw ke;
        }
        return res.results;
    } else {
        return zk.multi(ops);
    }
}
 
開發者ID:maoling,項目名稱:fuck_zookeeper,代碼行數:31,代碼來源:MultiTransactionTest.java

示例3: ephemeralCreateMultiOpTest

import org.apache.zookeeper.ZooKeeper; //導入方法依賴的package包/類
@Test
public void ephemeralCreateMultiOpTest() throws KeeperException, InterruptedException, IOException {
    final ZooKeeper zk = createClient();

    String data = "test";
    String path = "/ephemeralcreatemultiop";
    zk.create(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

    QuorumZooKeeperServer server = getConnectedServer(zk.getSessionId());
    Assert.assertNotNull("unable to find server interlocutor", server);
    UpgradeableSessionTracker sessionTracker = (UpgradeableSessionTracker)server.getSessionTracker();
    Assert.assertFalse("session already global", sessionTracker.isGlobalSession(zk.getSessionId()));

    List<OpResult> multi = null;
    try {
        multi = zk.multi(Arrays.asList(
                Op.setData(path, data.getBytes(), 0),
                Op.create(path + "/e", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL),
                Op.create(path + "/p", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
                Op.create(path + "/q", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL)
        ));
    } catch (KeeperException.SessionExpiredException e) {
        // the scenario that inspired this unit test
        Assert.fail("received session expired for a session promotion in a multi-op");
    }

    Assert.assertNotNull(multi);
    Assert.assertEquals(4, multi.size());
    Assert.assertEquals(data, new String(zk.getData(path + "/e", false, null)));
    Assert.assertEquals(data, new String(zk.getData(path + "/p", false, null)));
    Assert.assertEquals(data, new String(zk.getData(path + "/q", false, null)));
    Assert.assertTrue("session not promoted", sessionTracker.isGlobalSession(zk.getSessionId()));
}
 
開發者ID:didichuxing2,項目名稱:https-github.com-apache-zookeeper,代碼行數:34,代碼來源:MultiOpSessionUpgradeTest.java

示例4: testMultiToFollower

import org.apache.zookeeper.ZooKeeper; //導入方法依賴的package包/類
/**
 * Tests if a multiop submitted to a non-leader propagates to the leader properly
 * (see ZOOKEEPER-1124).
 * 
 * The test works as follows. It has a client connect to a follower and submit a multiop
 * to the follower. It then verifies that the multiop successfully gets committed by the leader.
 *
 * Without the fix in ZOOKEEPER-1124, this fails with a ConnectionLoss KeeperException.
 */
@Test
public void testMultiToFollower() throws Exception {
    qu = new QuorumUtil(1);
    CountdownWatcher watcher = new CountdownWatcher();
    qu.startQuorum();

    int index = 1;
    while(qu.getPeer(index).peer.leader == null)
        index++;

    ZooKeeper zk = new ZooKeeper(
            "127.0.0.1:" + qu.getPeer((index == 1)?2:1).peer.getClientPort(),
            ClientBase.CONNECTION_TIMEOUT, watcher);
    watcher.waitForConnected(CONNECTION_TIMEOUT);

    zk.multi(Arrays.asList(
            Op.create("/multi0", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
            Op.create("/multi1", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
            Op.create("/multi2", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)
            ));
    zk.getData("/multi0", false, null);
    zk.getData("/multi1", false, null);
    zk.getData("/multi2", false, null);

    zk.close();
}
 
開發者ID:didichuxing2,項目名稱:https-github.com-apache-zookeeper,代碼行數:36,代碼來源:QuorumTest.java


注:本文中的org.apache.zookeeper.ZooKeeper.multi方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。