| 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 framework.CreateClientExamples; |
| 23 |
import java.io.BufferedReader; |
| 24 |
import java.io.InputStreamReader; |
| 25 |
import org.apache.curator.framework.CuratorFramework; |
| 26 |
import org.apache.curator.framework.recipes.cache.TreeCache; |
| 27 |
|
| 28 |
public class TreeCacheExample { |
| 29 |
public static void main(String[] args) throws Exception { |
| 30 |
CuratorFramework client = CreateClientExamples.createSimple("127.0.0.1:2181"); |
| 31 |
client.getUnhandledErrorListenable().addListener((message, e) -> { |
| 32 |
System.err.println("error=" + message); |
| 33 |
e.printStackTrace(); |
| 34 |
}); |
| 35 |
client.getConnectionStateListenable().addListener((c, newState) -> { |
| 36 |
System.out.println("state=" + newState); |
| 37 |
}); |
| 38 |
client.start(); |
| 39 |
|
| 40 |
TreeCache cache = TreeCache.newBuilder(client, "/").setCacheData(false).build(); |
| 41 |
cache.getListenable().addListener((c, event) -> { |
| 42 |
if (event.getData() != null) { |
| 43 |
System.out.println( |
| 44 |
"type=" + event.getType() + " path=" + event.getData().getPath()); |
| 45 |
} else { |
| 46 |
System.out.println("type=" + event.getType()); |
| 47 |
} |
| 48 |
}); |
| 49 |
cache.start(); |
| 50 |
|
| 51 |
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); |
| 52 |
in.readLine(); |
| 53 |
} |
| 54 |
} |