| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package cache; |
| 21 |
|
| 22 |
import java.util.concurrent.ThreadLocalRandom; |
| 23 |
import org.apache.curator.framework.CuratorFramework; |
| 24 |
import org.apache.curator.framework.CuratorFrameworkFactory; |
| 25 |
import org.apache.curator.framework.recipes.cache.CuratorCache; |
| 26 |
import org.apache.curator.framework.recipes.cache.CuratorCacheListener; |
| 27 |
import org.apache.curator.retry.ExponentialBackoffRetry; |
| 28 |
import org.apache.curator.test.TestingServer; |
| 29 |
|
| 30 |
/** |
| 31 |
* Very simple example of creating a CuratorCache that listens to events and logs the changes |
| 32 |
* to standard out. A loop of random changes is run to exercise the cache. |
| 33 |
*/ |
| 34 |
public class CuratorCacheExample { |
| 35 |
private static final String PATH = "/example/cache"; |
| 36 |
|
| 37 |
public static void main(String[] args) throws Exception { |
| 38 |
ThreadLocalRandom random = ThreadLocalRandom.current(); |
| 39 |
try (TestingServer server = new TestingServer()) { |
| 40 |
try (CuratorFramework client = CuratorFrameworkFactory.newClient( |
| 41 |
server.getConnectString(), new ExponentialBackoffRetry(1000, 3))) { |
| 42 |
client.start(); |
| 43 |
try (CuratorCache cache = CuratorCache.build(client, PATH)) { |
| 44 |
// there are several ways to set a listener on a CuratorCache. You can watch for individual events |
| 45 |
// or for all events. Here, we'll use the builder to log individual cache actions |
| 46 |
CuratorCacheListener listener = CuratorCacheListener.builder() |
| 47 |
.forCreates(node -> System.out.println(String.format("Node created: [%s]", node))) |
| 48 |
.forChanges((oldNode, node) -> System.out.println( |
| 49 |
String.format("Node changed. Old: [%s] New: [%s]", oldNode, node))) |
| 50 |
.forDeletes(oldNode -> |
| 51 |
System.out.println(String.format("Node deleted. Old value: [%s]", oldNode))) |
| 52 |
.forInitialized(() -> System.out.println("Cache initialized")) |
| 53 |
.build(); |
| 54 |
|
| 55 |
// register the listener |
| 56 |
cache.listenable().addListener(listener); |
| 57 |
|
| 58 |
// the cache must be started |
| 59 |
cache.start(); |
| 60 |
|
| 61 |
// now randomly create/change/delete nodes |
| 62 |
for (int i = 0; i < 1000; ++i) { |
| 63 |
int depth = random.nextInt(1, 4); |
| 64 |
String path = makeRandomPath(random, depth); |
| 65 |
if (random.nextBoolean()) { |
| 66 |
client.create() |
| 67 |
.orSetData() |
| 68 |
.creatingParentsIfNeeded() |
| 69 |
.forPath( |
| 70 |
path, |
| 71 |
Long.toString(random.nextLong()).getBytes()); |
| 72 |
} else { |
| 73 |
client.delete().quietly().deletingChildrenIfNeeded().forPath(path); |
| 74 |
} |
| 75 |
|
| 76 |
Thread.sleep(5); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
private static String makeRandomPath(ThreadLocalRandom random, int depth) { |
| 84 |
if (depth == 0) { |
| 85 |
return PATH; |
| 86 |
} |
| 87 |
return makeRandomPath(random, depth - 1) + "/" + random.nextInt(3); |
| 88 |
} |
| 89 |
} |