Debian Codegraph @

curator-examples/src/main/java/modeled/ModeledCuratorExamplesAlt.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 modeled;
21
22 import java.util.function.Consumer;
23 import org.apache.curator.x.async.modeled.ModeledFramework;
24
25 public class ModeledCuratorExamplesAlt {
26 public static void createOrUpdate(PersonModelSpec modelSpec, PersonModel model) {
27 // change the affected path to be modeled's base path plus id: i.e. "/example/path/{id}"
28 ModeledFramework<PersonModel> resolved = modelSpec.resolved(model.getContainerType(), model.getId());
29
30 // by default ModeledFramework instances update the node if it already exists
31 // so this will either create or update the node
32 resolved.set(model); // note - this is async
33 }
34
35 public static void readPerson(
36 PersonModelSpec modelSpec, ContainerType containerType, PersonId id, Consumer<PersonModel> receiver) {
37 ModeledFramework<PersonModel> resolved = modelSpec.resolved(containerType, id);
38
39 // read the person with the given ID and asynchronously call the receiver after it is read
40 resolved.read().whenComplete((person, exception) -> {
41 if (exception != null) {
42 exception.printStackTrace(); // handle the error
43 } else {
44 receiver.accept(person);
45 }
46 });
47 }
48 }