publicstatic CuratorFramework createSimple(String connectionString) { // these are reasonable arguments for the ExponentialBackoffRetry. // The first retry will wait 1 second - the second will wait up to 2 seconds - the // third will wait up to 4 seconds. ExponentialBackoffRetryretryPolicy=newExponentialBackoffRetry(1000, 3); // The simplest way to get a CuratorFramework instance. This will use default values. // The only required arguments are the connection string and the retry policy return CuratorFrameworkFactory.newClient(connectionString, retryPolicy); }
publicstatic CuratorFramework createWithOptions(String connectionString, RetryPolicy retryPolicy, int connectionTimeoutMs, int sessionTimeoutMs) { // using the CuratorFrameworkFactory.builder() gives fine grained control // over creation options. See the CuratorFrameworkFactory.Builder javadoc details return CuratorFrameworkFactory.builder().connectString(connectionString) .retryPolicy(retryPolicy) .connectionTimeoutMs(connectionTimeoutMs) .sessionTimeoutMs(sessionTimeoutMs) // etc. etc. .build(); } }
CuratorFrameworkclient= CuratorFrameworkFactory.builder().namespace("MyApp") ... build(); ... client.create().forPath("/test", data); // node was actually written to: "/MyApp/test"
publicstaticvoidcreate(CuratorFramework client, String path, byte[] payload)throws Exception { // this will create the given ZNode with the given data client.create().forPath(path, payload); }
publicstaticvoidcreateEphemeral(CuratorFramework client, String path, byte[] payload)throws Exception { // this will create the given EPHEMERAL ZNode with the given data client.create().withMode(CreateMode.EPHEMERAL).forPath(path, payload); }
publicstatic String createEphemeralSequential(CuratorFramework client, String path, byte[] payload)throws Exception { // this will create the given EPHEMERAL-SEQUENTIAL ZNode with the given // data using Curator protection. return client.create().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path, payload); }
publicstaticvoidsetData(CuratorFramework client, String path, byte[] payload)throws Exception { // set data for the given node client.setData().forPath(path, payload); }
publicstaticvoidsetDataAsync(CuratorFramework client, String path, byte[] payload)throws Exception { // this is one method of getting event/async notifications CuratorListenerlistener=newCuratorListener() { @Override publicvoideventReceived(CuratorFramework client, CuratorEvent event)throws Exception { // examine event for details } }; client.getCuratorListenable().addListener(listener); // set data for the given node asynchronously. The completion // notification // is done via the CuratorListener. client.setData().inBackground().forPath(path, payload); }
publicstaticvoidsetDataAsyncWithCallback(CuratorFramework client, BackgroundCallback callback, String path, byte[] payload)throws Exception { // this is another method of getting notification of an async completion client.setData().inBackground(callback).forPath(path, payload); }
publicstaticvoiddelete(CuratorFramework client, String path)throws Exception { // delete the given node client.delete().forPath(path); }
publicstaticvoidguaranteedDelete(CuratorFramework client, String path)throws Exception { // delete the given node and guarantee that it completes client.delete().guaranteed().forPath(path); }
publicstatic List<String> watchedGetChildren(CuratorFramework client, String path)throws Exception { /** * Get children and set a watcher on the node. The watcher notification * will come through the CuratorListener (see setDataAsync() above). */ return client.getChildren().watched().forPath(path); }
publicstatic List<String> watchedGetChildren(CuratorFramework client, String path, Watcher watcher)throws Exception { /** * Get children and set the given watcher on the node. */ return client.getChildren().usingWatcher(watcher).forPath(path); } }
publicstatic Collection<CuratorTransactionResult> transaction(CuratorFramework client)throws Exception { // this example shows how to use ZooKeeper's new transactions Collection<CuratorTransactionResult> results = client.inTransaction().create().forPath("/a/path", "some data".getBytes()) .and().setData().forPath("/another/path", "other data".getBytes()) .and().delete().forPath("/yet/another/path") .and().commit(); // IMPORTANT! // called for (CuratorTransactionResult result : results) { System.out.println(result.getForPath() + " - " + result.getType()); } return results; }
/* * These next four methods show how to use Curator's transaction APIs in a * more traditional - one-at-a-time - manner */ publicstatic CuratorTransaction startTransaction(CuratorFramework client) { // start the transaction builder return client.inTransaction(); }