Java Code Examples for org.apache.curator.framework.CuratorFramework#inTransaction()

The following examples show how to use org.apache.curator.framework.CuratorFramework#inTransaction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZkLeaderSelector.java    From binlake with Apache License 2.0 6 votes vote down vote up
/**
 * only startSelector and over can update the counter
 *
 * @param metaInfo
 * @throws Exception
 */
public void updateCounter(MetaInfo metaInfo) throws Exception {
    LogUtils.debug.debug("updateCounter");
    metaInfo.refreshCounter();
    CuratorFramework client = this.client;

    if (client != null) {
        CuratorTransaction trx = client.inTransaction();
        CuratorTransactionFinal trf = trx.setData().forPath(counterPath, Meta.Counter.marshalJson(metaInfo.getCounter())).and();

        Meta.Error err = null;
        if ((err = metaInfo.getError()) != null) {
            trf = trf.setData().forPath(errorPath, Meta.Error.marshalJson(err)).and();
        }
        trf.commit();
    }
}
 
Example 2
Source File: CrudDemo.java    From javabase with Apache License 2.0 6 votes vote down vote up
/**
 * Curator支持事务,一组crud操作同生同灭
 * @param client
 */
private static void transation(CuratorFramework client) {
    try {
        // 开启事务
        CuratorTransaction transaction = client.inTransaction();
        Collection<CuratorTransactionResult> results = transaction.create()
                .forPath("/root/transation", "transation".getBytes()).and().create()
                .forPath("/root/transation2", "transation2".getBytes()).and()
                .delete().forPath("/root/transation").and()
                .delete().forPath("/root/transation2").and().commit();
        for (CuratorTransactionResult result : results) {
            System.out.println(result.getForPath() + " - " + result.getType());
        }
    }catch (Exception e){
        log.error("transation exception ", e);
    }
}
 
Example 3
Source File: ZkService.java    From binlake with Apache License 2.0 5 votes vote down vote up
/**
 * @param c:        curator client
 * @param delPaths: path to delete
 * @param newDbKVs: new db key values
 * @throws Exception
 */
private void refreshDbInfo(CuratorFramework c, List<String> delPaths, Map<String, byte[]> newDbKVs) throws Exception {
    if (delPaths.size() == 0 && newDbKVs.size() == 0) {
        return;
    }

    // transaction
    CuratorTransaction trx = c.inTransaction();
    CuratorTransactionFinal trxf = null;

    for (String p : delPaths) {
        trxf = trx.delete().forPath(p).and();
    }

    for (Map.Entry<String, byte[]> entry : newDbKVs.entrySet()) {
        if (c.checkExists().forPath(entry.getKey()) == null) {
            // node not exist then create
            if (logger.isDebugEnabled()) {
                logger.warn("path " + entry.getKey() + " not exit under zookeeper node " + zkPath);
            }
            trxf = trx.create().forPath(entry.getKey(), entry.getValue()).and();
            continue;
        }
        trxf = trx.setData().forPath(entry.getKey(), entry.getValue()).and();
    }

    trxf.commit();
}
 
Example 4
Source File: ZkLeaderSelector.java    From binlake with Apache License 2.0 5 votes vote down vote up
/**
 * update candidates
 *
 * @param cans
 */
private void updateCandidate(byte[] cans, String leader) throws Exception {
    LogUtils.debug.debug("updateCandidate");
    CuratorFramework client = this.client;

    if (client != null) {
        CuratorTransaction trx = client.inTransaction();
        CuratorTransactionFinal ctf = trx.setData().forPath(candidatePath, cans).and();
        if (!StringUtils.equals("", leader)) {
            ctf = ctf.setData().forPath(leaderPath, leader.getBytes()).and();
        }
        // commit
        ctf.commit();
    }
}
 
Example 5
Source File: TransactionExamples.java    From xian with Apache License 2.0 4 votes vote down vote up
public static CuratorTransaction        startTransaction(CuratorFramework client)
{
    // start the transaction builder
    return client.inTransaction();
}