Repository: curator Updated Branches: refs/heads/persistent-watch fc2219ea9 -> 451add4cb
Updated cache example to use CuratorCache Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/451add4c Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/451add4c Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/451add4c Branch: refs/heads/persistent-watch Commit: 451add4cb5c01cce7cd8a76022dcca212c0d3de3 Parents: fc2219e Author: randgalt <randg...@apache.org> Authored: Thu Aug 10 13:10:36 2017 -0500 Committer: randgalt <randg...@apache.org> Committed: Thu Aug 10 13:10:36 2017 -0500 ---------------------------------------------------------------------- .../src/main/java/cache/CachingExample.java | 250 +++++++++++++++++++ .../src/main/java/cache/PathCacheExample.java | 247 ------------------ .../src/site/confluence/index.confluence | 2 +- .../recipes/watch/PersistentWatcher.java | 2 +- pom.xml | 2 +- 5 files changed, 253 insertions(+), 250 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/451add4c/curator-examples/src/main/java/cache/CachingExample.java ---------------------------------------------------------------------- diff --git a/curator-examples/src/main/java/cache/CachingExample.java b/curator-examples/src/main/java/cache/CachingExample.java new file mode 100644 index 0000000..eda5536 --- /dev/null +++ b/curator-examples/src/main/java/cache/CachingExample.java @@ -0,0 +1,250 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package cache; + +import com.google.common.collect.Lists; +import discovery.ExampleServer; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.recipes.watch.CacheEvent; +import org.apache.curator.framework.recipes.watch.CacheListener; +import org.apache.curator.framework.recipes.watch.CachedNode; +import org.apache.curator.framework.recipes.watch.CuratorCache; +import org.apache.curator.framework.recipes.watch.CuratorCacheBuilder; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.curator.test.TestingServer; +import org.apache.curator.utils.CloseableUtils; +import org.apache.curator.utils.ZKPaths; +import org.apache.zookeeper.KeeperException; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +/** + * An example of the CuratorCache. The example "harness" is a command processor + * that allows adding/updating/removed nodes in a path. A CuratorCache keeps a + * cache of these changes and outputs when updates occurs. + */ +public class CachingExample +{ + private static final String PATH = "/example/cache"; + + public static void main(String[] args) throws Exception + { + TestingServer server = new TestingServer(); + CuratorFramework client = null; + CuratorCache cache = null; + try + { + client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); + client.start(); + + // in this example we will cache data. Note that this is optional. + cache = CuratorCacheBuilder.builder(client, PATH).build(); + cache.start(); + + processCommands(client, cache); + } + finally + { + CloseableUtils.closeQuietly(cache); + CloseableUtils.closeQuietly(client); + CloseableUtils.closeQuietly(server); + } + } + + private static void addListener(CuratorCache cache) + { + // a PathChildrenCacheListener is optional. Here, it's used just to log changes + CacheListener listener = new CacheListener() + { + @Override + public void process(CacheEvent event, String path, CachedNode node) + { + switch ( event ) + { + case NODE_CREATED: + { + System.out.println("Node added: " + ZKPaths.getNodeFromPath(path)); + break; + } + + case NODE_CHANGED: + { + System.out.println("Node changed: " + ZKPaths.getNodeFromPath(path)); + break; + } + + case NODE_DELETED: + { + System.out.println("Node removed: " + ZKPaths.getNodeFromPath(path)); + break; + } + } + } + }; + cache.getListenable().addListener(listener); + } + + private static void processCommands(CuratorFramework client, CuratorCache cache) throws Exception + { + // More scaffolding that does a simple command line processor + + printHelp(); + + List<ExampleServer> servers = Lists.newArrayList(); + try + { + addListener(cache); + + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + boolean done = false; + while ( !done ) + { + System.out.print("> "); + + String line = in.readLine(); + if ( line == null ) + { + break; + } + + String command = line.trim(); + String[] parts = command.split("\\s"); + if ( parts.length == 0 ) + { + continue; + } + String operation = parts[0]; + String args[] = Arrays.copyOfRange(parts, 1, parts.length); + + if ( operation.equalsIgnoreCase("help") || operation.equalsIgnoreCase("?") ) + { + printHelp(); + } + else if ( operation.equalsIgnoreCase("q") || operation.equalsIgnoreCase("quit") ) + { + done = true; + } + else if ( operation.equals("set") ) + { + setValue(client, command, args); + } + else if ( operation.equals("remove") ) + { + remove(client, command, args); + } + else if ( operation.equals("list") ) + { + list(cache); + } + + Thread.sleep(1000); // just to allow the console output to catch up + } + } + finally + { + for ( ExampleServer server : servers ) + { + CloseableUtils.closeQuietly(server); + } + } + } + + private static void list(CuratorCache cache) + { + if ( cache.size() == 0 ) + { + System.out.println("* empty *"); + } + else + { + for ( Map.Entry<String, CachedNode> entry : cache.view().entrySet() ) + { + System.out.println(entry.getKey() + " = " + new String(entry.getValue().getData())); + } + } + } + + private static void remove(CuratorFramework client, String command, String[] args) throws Exception + { + if ( args.length != 1 ) + { + System.err.println("syntax error (expected remove <path>): " + command); + return; + } + + String name = args[0]; + if ( name.contains("/") ) + { + System.err.println("Invalid node name" + name); + return; + } + String path = ZKPaths.makePath(PATH, name); + + try + { + client.delete().forPath(path); + } + catch ( KeeperException.NoNodeException e ) + { + // ignore + } + } + + private static void setValue(CuratorFramework client, String command, String[] args) throws Exception + { + if ( args.length != 2 ) + { + System.err.println("syntax error (expected set <path> <value>): " + command); + return; + } + + String name = args[0]; + if ( name.contains("/") ) + { + System.err.println("Invalid node name" + name); + return; + } + String path = ZKPaths.makePath(PATH, name); + + byte[] bytes = args[1].getBytes(); + try + { + client.setData().forPath(path, bytes); + } + catch ( KeeperException.NoNodeException e ) + { + client.create().creatingParentContainersIfNeeded().forPath(path, bytes); + } + } + + private static void printHelp() + { + System.out.println("An example of using CuratorCache. This example is driven by entering commands at the prompt:\n"); + System.out.println("set <name> <value>: Adds or updates a node with the given name"); + System.out.println("remove <name>: Deletes the node with the given name"); + System.out.println("list: List the nodes/values in the cache"); + System.out.println("quit: Quit the example"); + System.out.println(); + } +} http://git-wip-us.apache.org/repos/asf/curator/blob/451add4c/curator-examples/src/main/java/cache/PathCacheExample.java ---------------------------------------------------------------------- diff --git a/curator-examples/src/main/java/cache/PathCacheExample.java b/curator-examples/src/main/java/cache/PathCacheExample.java deleted file mode 100644 index e121337..0000000 --- a/curator-examples/src/main/java/cache/PathCacheExample.java +++ /dev/null @@ -1,247 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package cache; - -import com.google.common.collect.Lists; -import org.apache.curator.utils.CloseableUtils; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.CuratorFrameworkFactory; -import org.apache.curator.framework.recipes.cache.ChildData; -import org.apache.curator.framework.recipes.cache.PathChildrenCache; -import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; -import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; -import org.apache.curator.retry.ExponentialBackoffRetry; -import org.apache.curator.test.TestingServer; -import org.apache.curator.utils.ZKPaths; -import discovery.ExampleServer; -import org.apache.zookeeper.KeeperException; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.util.Arrays; -import java.util.List; - -/** - * An example of the PathChildrenCache. The example "harness" is a command processor - * that allows adding/updating/removed nodes in a path. A PathChildrenCache keeps a - * cache of these changes and outputs when updates occurs. - */ -public class PathCacheExample -{ - private static final String PATH = "/example/cache"; - - public static void main(String[] args) throws Exception - { - TestingServer server = new TestingServer(); - CuratorFramework client = null; - PathChildrenCache cache = null; - try - { - client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); - client.start(); - - // in this example we will cache data. Notice that this is optional. - cache = new PathChildrenCache(client, PATH, true); - cache.start(); - - processCommands(client, cache); - } - finally - { - CloseableUtils.closeQuietly(cache); - CloseableUtils.closeQuietly(client); - CloseableUtils.closeQuietly(server); - } - } - - private static void addListener(PathChildrenCache cache) - { - // a PathChildrenCacheListener is optional. Here, it's used just to log changes - PathChildrenCacheListener listener = new PathChildrenCacheListener() - { - @Override - public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception - { - switch ( event.getType() ) - { - case CHILD_ADDED: - { - System.out.println("Node added: " + ZKPaths.getNodeFromPath(event.getData().getPath())); - break; - } - - case CHILD_UPDATED: - { - System.out.println("Node changed: " + ZKPaths.getNodeFromPath(event.getData().getPath())); - break; - } - - case CHILD_REMOVED: - { - System.out.println("Node removed: " + ZKPaths.getNodeFromPath(event.getData().getPath())); - break; - } - } - } - }; - cache.getListenable().addListener(listener); - } - - private static void processCommands(CuratorFramework client, PathChildrenCache cache) throws Exception - { - // More scaffolding that does a simple command line processor - - printHelp(); - - List<ExampleServer> servers = Lists.newArrayList(); - try - { - addListener(cache); - - BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); - boolean done = false; - while ( !done ) - { - System.out.print("> "); - - String line = in.readLine(); - if ( line == null ) - { - break; - } - - String command = line.trim(); - String[] parts = command.split("\\s"); - if ( parts.length == 0 ) - { - continue; - } - String operation = parts[0]; - String args[] = Arrays.copyOfRange(parts, 1, parts.length); - - if ( operation.equalsIgnoreCase("help") || operation.equalsIgnoreCase("?") ) - { - printHelp(); - } - else if ( operation.equalsIgnoreCase("q") || operation.equalsIgnoreCase("quit") ) - { - done = true; - } - else if ( operation.equals("set") ) - { - setValue(client, command, args); - } - else if ( operation.equals("remove") ) - { - remove(client, command, args); - } - else if ( operation.equals("list") ) - { - list(cache); - } - - Thread.sleep(1000); // just to allow the console output to catch up - } - } - finally - { - for ( ExampleServer server : servers ) - { - CloseableUtils.closeQuietly(server); - } - } - } - - private static void list(PathChildrenCache cache) - { - if ( cache.getCurrentData().size() == 0 ) - { - System.out.println("* empty *"); - } - else - { - for ( ChildData data : cache.getCurrentData() ) - { - System.out.println(data.getPath() + " = " + new String(data.getData())); - } - } - } - - private static void remove(CuratorFramework client, String command, String[] args) throws Exception - { - if ( args.length != 1 ) - { - System.err.println("syntax error (expected remove <path>): " + command); - return; - } - - String name = args[0]; - if ( name.contains("/") ) - { - System.err.println("Invalid node name" + name); - return; - } - String path = ZKPaths.makePath(PATH, name); - - try - { - client.delete().forPath(path); - } - catch ( KeeperException.NoNodeException e ) - { - // ignore - } - } - - private static void setValue(CuratorFramework client, String command, String[] args) throws Exception - { - if ( args.length != 2 ) - { - System.err.println("syntax error (expected set <path> <value>): " + command); - return; - } - - String name = args[0]; - if ( name.contains("/") ) - { - System.err.println("Invalid node name" + name); - return; - } - String path = ZKPaths.makePath(PATH, name); - - byte[] bytes = args[1].getBytes(); - try - { - client.setData().forPath(path, bytes); - } - catch ( KeeperException.NoNodeException e ) - { - client.create().creatingParentContainersIfNeeded().forPath(path, bytes); - } - } - - private static void printHelp() - { - System.out.println("An example of using PathChildrenCache. This example is driven by entering commands at the prompt:\n"); - System.out.println("set <name> <value>: Adds or updates a node with the given name"); - System.out.println("remove <name>: Deletes the node with the given name"); - System.out.println("list: List the nodes/values in the cache"); - System.out.println("quit: Quit the example"); - System.out.println(); - } -} http://git-wip-us.apache.org/repos/asf/curator/blob/451add4c/curator-examples/src/site/confluence/index.confluence ---------------------------------------------------------------------- diff --git a/curator-examples/src/site/confluence/index.confluence b/curator-examples/src/site/confluence/index.confluence index f9be506..6c5a30d 100644 --- a/curator-examples/src/site/confluence/index.confluence +++ b/curator-examples/src/site/confluence/index.confluence @@ -3,7 +3,7 @@ h1. Examples This module contains example usages of various Curator features. Each directory in the module is a separate example. |/leader|Example leader selector code| -|/cache|Example PathChildrenCache usage| +|/cache|Example CuratorCache usage| |/locking|Example of using InterProcessMutex| |/discovery|Example usage of the Curator's ServiceDiscovery| |/framework|A few examples of how to use the CuratorFramework class| http://git-wip-us.apache.org/repos/asf/curator/blob/451add4c/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/PersistentWatcher.java ---------------------------------------------------------------------- diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/PersistentWatcher.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/PersistentWatcher.java index 17b39f7..310478a 100644 --- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/PersistentWatcher.java +++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/PersistentWatcher.java @@ -40,7 +40,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; /** - * A managed persisten watcher. The watch will be managed such that it stays set through + * A managed persistent watcher. The watch will be managed such that it stays set through * connection lapses, etc. */ public class PersistentWatcher implements Closeable http://git-wip-us.apache.org/repos/asf/curator/blob/451add4c/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index e981493..efb43a9 100644 --- a/pom.xml +++ b/pom.xml @@ -59,7 +59,7 @@ <jdk-version>1.7</jdk-version> <!-- versions --> - <zookeeper-version>3.6.0-SNAPSHOT</zookeeper-version> + <zookeeper-version>3.5.4-beta-SNAPSHOT</zookeeper-version> <maven-project-info-reports-plugin-version>2.9</maven-project-info-reports-plugin-version> <maven-bundle-plugin-version>3.2.0</maven-bundle-plugin-version> <maven-javadoc-plugin-version>2.10.4</maven-javadoc-plugin-version>