Domanda

The Apache Curator library for ZooKeeper uses a nice "fluent" syntax. For example, to modify several nodes in a transaction, the code looks something like:

client.inTransaction().
    .setData().forNode(node1Path, data1)
    .and()
    .SetData().forNode(node2Path, data2)
    .and()
    .commit();

This works great and, IMHO, produces very readable code. However, I have a situation where I have to modify a set of ZNodes in a transaction. I don't know until runtime how many nodes, or which nodes, will need to be modified. Thus, I don't think I can use the fluent syntax easily. Looking at the docs, I can manually manage the proxy objects each of the fluent method calls return, but then the code requires explicit use of CuratorTransaction, TransactionSetDataBuilder, CuratorTransactionBridge, etc. It's clearly do-able, but the code starts to look really ugly.

I don't see a non-fluent way to do transactions with Curator. Does anyone know if there is one and/or if there is a "nice" way to build a transaction at runtime? Specifically, given a Map<String, String> mapping from ZNode paths to the data that needs to end up in that ZNode, how would you set all the nodes transactionally?

È stato utile?

Soluzione

One way to skin the cat?:

CuratorTransaction curatorTransaction = client.inTransaction();

for (Map.Entry<String, String> entry : transactionInfo.entrySet()) {
    curatorTransaction = curatorTransaction
        .setData().forNode(entry.getKey(), entry.getValue()).and();
}

// If there was at least one entry in transactionInfo, and() makes it a CuratorTransactionFinal
if (curatorTransaction instanceof CuratorTransactionFinal) {
    ((CuratorTransactionFinal)curatorTransaction).commit();
}

Altri suggerimenti

I use the curator for a zookeeper transaction,first I do some like this

uratorTransaction.create().withMode(CreateMode.PERSISTENT_SEQUENTIAL)
                            .forPath().and();then I commit it like bfos',but the commit method can't  returned,and the exception in the log file is like this:
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss
    at org.apache.zookeeper.KeeperException.create(KeeperException.java:99)
    at org.apache.zookeeper.ZooKeeper.multiInternal(ZooKeeper.java:932)
    at org.apache.zookeeper.ZooKeeper.multi(ZooKeeper.java:912)
    at org.apache.curator.framework.imps.CuratorTransactionImpl.doOperation(CuratorTransactionImpl.java:159)
    at org.apache.curator.framework.imps.CuratorTransactionImpl.access$200(CuratorTransactionImpl.java:44)
    at org.apache.curator.framework.imps.CuratorTransactionImpl$2.call(CuratorTransactionImpl.java:129)
    at org.apache.curator.framework.imps.CuratorTransactionImpl$2.call(CuratorTransactionImpl.java:125)
    at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:107)
    at org.apache.curator.framework.imps.CuratorTransactionImpl.commit(CuratorTransactionImpl.java:121)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top