Debian Codegraph @

curator-examples/src/main/java/async/AsyncExamples.java

raw
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 async;
21
22 import java.util.concurrent.CompletionStage;
23 import org.apache.curator.framework.CuratorFramework;
24 import org.apache.curator.x.async.AsyncCuratorFramework;
25 import org.apache.curator.x.async.AsyncEventException;
26 import org.apache.curator.x.async.WatchMode;
27 import org.apache.zookeeper.WatchedEvent;
28
29 /**
30 * Examples using the asynchronous DSL
31 */
32 public class AsyncExamples {
33 public static AsyncCuratorFramework wrap(CuratorFramework client) {
34 // wrap a CuratorFramework instance so that it can be used async.
35 // do this once and re-use the returned AsyncCuratorFramework instance
36 return AsyncCuratorFramework.wrap(client);
37 }
38
39 public static void create(CuratorFramework client, String path, byte[] payload) {
40 AsyncCuratorFramework async =
41 AsyncCuratorFramework.wrap(client); // normally you'd wrap early in your app and reuse the instance
42
43 // create a node at the given path with the given payload asynchronously
44 async.create().forPath(path, payload).whenComplete((name, exception) -> {
45 if (exception != null) {
46 // there was a problem
47 exception.printStackTrace();
48 } else {
49 System.out.println("Created node name is: " + name);
50 }
51 });
52 }
53
54 public static void createThenWatch(CuratorFramework client, String path) {
55 AsyncCuratorFramework async =
56 AsyncCuratorFramework.wrap(client); // normally you'd wrap early in your app and reuse the instance
57
58 // this example shows to asynchronously use watchers for both event
59 // triggering and connection problems. If you don't need to be notified
60 // of connection problems, use the simpler approach shown in createThenWatchSimple()
61
62 // create a node at the given path with the given payload asynchronously
63 // then watch the created node
64 async.create().forPath(path).whenComplete((name, exception) -> {
65 if (exception != null) {
66 // there was a problem creating the node
67 exception.printStackTrace();
68 } else {
69 handleWatchedStage(async.watched().checkExists().forPath(path).event());
70 }
71 });
72 }
73
74 public static void createThenWatchSimple(CuratorFramework client, String path) {
75 AsyncCuratorFramework async =
76 AsyncCuratorFramework.wrap(client); // normally you'd wrap early in your app and reuse the instance
77
78 // create a node at the given path with the given payload asynchronously
79 // then watch the created node
80 async.create().forPath(path).whenComplete((name, exception) -> {
81 if (exception != null) {
82 // there was a problem creating the node
83 exception.printStackTrace();
84 } else {
85 // because "WatchMode.successOnly" is used the watch stage is only triggered when
86 // the EventType is a node event
87 async.with(WatchMode.successOnly)
88 .watched()
89 .checkExists()
90 .forPath(path)
91 .event()
92 .thenAccept(event -> {
93 System.out.println(event.getType());
94 System.out.println(event);
95 });
96 }
97 });
98 }
99
100 private static void handleWatchedStage(CompletionStage<WatchedEvent> watchedStage) {
101 // async handling of Watchers is complicated because watchers can trigger multiple times
102 // and CompletionStage don't support this behavior
103
104 // thenAccept() handles normal watcher triggering.
105 watchedStage.thenAccept(event -> {
106 System.out.println(event.getType());
107 System.out.println(event);
108 // etc.
109 });
110
111 // exceptionally is called if there is a connection problem in which case
112 // watchers trigger to signal the connection problem. "reset()" must be called
113 // to reset the watched stage
114 watchedStage.exceptionally(exception -> {
115 AsyncEventException asyncEx = (AsyncEventException) exception;
116 asyncEx.printStackTrace(); // handle the error as needed
117 handleWatchedStage(asyncEx.reset());
118 return null;
119 });
120 }
121 }