如何在 Apache Curator 中异步创建 znode

How to create a znode asynchronously in Apache Curator

借助 Curator 的流畅 API,我们可以通过调用以下内容来同步创建一个 znode:

client.create().withMode(CreateMode.PERSISTENT).forPath("/mypath", new byte[0]);

我想知道如何在指定创建模式的同时异步执行相同的操作?

我们可以在指定创建模式的同时异步执行给定的创建操作,如下所示,

    client.create()
          .withMode(CreateMode.PERSISTENT)
          .inBackground()
          .forPath("/mypath", new byte[0]);

如果您使用的是 Java 8 和 ZooKeeper 3.5.x,最新版本的 Curator(注意:我是主要作者)有一个新的异步 DSL。您可以在这里阅读:http://curator.apache.org/curator-x-async/index.html

例如

AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
async.checkExists().forPath(somePath).thenAccept(stat -> mySuccessOperation(stat));