1

ZooKeeperのApache Curator ライブラリは、優れた「流れるような」構文を使用します。たとえば、トランザクション内の複数のノードを変更するには、コードは次のようになります。

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

これはうまく機能し、私見ですが、非常に読みやすいコードを生成します。しかし、トランザクションで一連の ZNode を変更しなければならない状況があります。いくつのノード、またはどのノードを変更する必要があるかは、実行時までわかりません。したがって、流暢な構文を簡単に使用できるとは思いません。ドキュメントを見ると、各流暢なメソッド呼び出しが返すプロキシ オブジェクトを手動で管理できますが、コードでは 、 、 などを明示的に使用する必要CuratorTransactionTransactionSetDataBuilderありCuratorTransactionBridgeます。

Curator でトランザクションを行う非流暢な方法は見当たりません。実行時にトランザクションを構築するための「良い」方法があるかどうか、および/またはあるかどうかを知っている人はいますか? 具体的には、Map<String, String>ZNode パスからその ZNode で終了する必要があるデータへのマッピングが与えられた場合、すべてのノードをどのようにトランザクション的に設定しますか?

4

2 に答える 2

4

猫の皮を剥く方法の 1 つ:

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();
}
于 2013-08-12T22:48:50.113 に答える
0

私は飼育係のトランザクションにキュレーターを使用します。まず、このようなことを行います

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)
于 2015-01-14T11:17:41.777 に答える