本文整理汇总了Java中org.apache.curator.framework.recipes.cache.NodeCacheListener类的典型用法代码示例。如果您正苦于以下问题:Java NodeCacheListener类的具体用法?Java NodeCacheListener怎么用?Java NodeCacheListener使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NodeCacheListener类属于org.apache.curator.framework.recipes.cache包,在下文中一共展示了NodeCacheListener类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: runWatch
import org.apache.curator.framework.recipes.cache.NodeCacheListener; //导入依赖的package包/类
/**
* @param cache NodeCache
* @param zkListen
* @throws Exception
* @Created 2016/9/20
*/
private static void runWatch(final NodeCache cache, final ZookeeperProcessListen zkListen)
throws Exception {
cache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() {
LOGGER.info("ZktoxmlMain runWatch process path event start ");
String notPath = cache.getCurrentData().getPath();
LOGGER.info("NodeCache changed, path is: " + notPath);
// notify
zkListen.notify(notPath);
LOGGER.info("ZktoxmlMain runWatch process path event over");
}
});
}
示例2: test_zkNode
import org.apache.curator.framework.recipes.cache.NodeCacheListener; //导入依赖的package包/类
@Test
public void test_zkNode() throws Exception {
CuratorFramework zk = curator.usingNamespace("namespace-test");
String groupPath = "/group-1/localhost:9001";
String s = zk.create().creatingParentsIfNeeded().forPath(groupPath);
Assert.assertEquals(s, "/group-1/localhost:9001");
NodeCache nodeCache = new NodeCache(zk, "/group-1", true);
nodeCache.start();
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
logger.info("node cache change");
}
});
zk.setData().forPath("/group-1", "test-0".getBytes());
zk.setData().forPath("/group-1", "test-2".getBytes());
}
示例3: start
import org.apache.curator.framework.recipes.cache.NodeCacheListener; //导入依赖的package包/类
@Override
public void start() throws Exception {
// Create the zookeeper node
createNode();
// Initial data load (avoid race conditions w/"NodeCache.start(true)")
updateFromZkBytes(_curator.getData().forPath(_zkPath), _defaultValue);
// Re-load the data and watch for changes.
_nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
ChildData childData = _nodeCache.getCurrentData();
if (childData != null) {
updateFromZkBytes(childData.getData(), _defaultValue);
}
}
});
_nodeCache.start();
}
示例4: start
import org.apache.curator.framework.recipes.cache.NodeCacheListener; //导入依赖的package包/类
public void start() throws Exception {
started.set(true);
if (curatorFramework.checkExists().forPath(path) == null) {
try {
curatorFramework.create().creatingParentsIfNeeded().forPath(path, "{}".getBytes());
} catch (KeeperException.NodeExistsException ex) {
log.info("Node exists on create, continuing");
}
}
this.nodeCacheListener = new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
notifyListeners(get());
}
};
this.nodeCache.getListenable().addListener(nodeCacheListener);
this.nodeCache.start(true);
}
示例5: main
import org.apache.curator.framework.recipes.cache.NodeCacheListener; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
CuratorFramework framework = CuratorFrameworkFactory.builder()
.connectString("localhost:2181").retryPolicy(new RetryNTimes(3, 2000)).build();
try {
framework.start();
final NodeCache nodeCache = new NodeCache(framework, "/test");
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
ChildData data = nodeCache.getCurrentData();
System.out.println(new String(data.getData()) + " / " + data);
}
});
nodeCache.start();
Thread.sleep(30000);
nodeCache.close();
} finally {
framework.close();
}
}
示例6: start
import org.apache.curator.framework.recipes.cache.NodeCacheListener; //导入依赖的package包/类
/**
* Start.
*
* @throws Exception the exception
*/
public void start() throws Exception { //NOSONAR
LOG.info("Starting node tracker");
zkClient.getUnhandledErrorListenable().addListener(errorsListener);
if (createZkNode()) {
controlCache = new NodeCache(zkClient, CONTROL_SERVER_NODE_PATH);
controlCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
ChildData currentData = controlCache.getCurrentData();
if (currentData == null) {
LOG.warn("Control service node died!");
onNoMaster();
} else {
LOG.warn("Control service node changed!");
onMasterChange(currentData);
}
}
});
controlCache.start();
} else {
LOG.warn("Failed to create ZK node!");
}
}
示例7: createValue_TriggersListenerOnPropagate
import org.apache.curator.framework.recipes.cache.NodeCacheListener; //导入依赖的package包/类
@Test
public void createValue_TriggersListenerOnPropagate() throws Exception {
String value = "foo";
final CountDownLatch latch = new CountDownLatch(1);
container.addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
latch.countDown();
}
});
framework.create().creatingParentsIfNeeded().forPath("/dev/test", value.getBytes(Charsets.UTF_8));
latch.await();
assertEquals(value, container.get());
}
示例8: runOneIteration
import org.apache.curator.framework.recipes.cache.NodeCacheListener; //导入依赖的package包/类
@Override
protected void runOneIteration() {
String newValue = PropertyReader.getProperty(propOverride);
if (!Objects.equal(newValue, lastValue)) {
for (final Map.Entry<NodeCacheListener, Executor> o : listeners.entrySet()) {
o.getValue().execute(new Runnable() {
@Override
public void run() {
try {
o.getKey().nodeChanged();
} catch (Exception ex) {
LOG.warn("Exception when processing node change.", ex);
}
}
});
}
}
}
示例9: start
import org.apache.curator.framework.recipes.cache.NodeCacheListener; //导入依赖的package包/类
@Override
public void start() throws Exception {
if (!isStarted.compareAndSet(false, true)) {
throw new RuntimeException("Service already started!");
}
if (framework.getState() != CuratorFrameworkState.STARTED) {
throw new RuntimeException("CuratorFramework is not started!");
}
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
build();
}
});
nodeCache.start(true);
build();
}
示例10: registerListener
import org.apache.curator.framework.recipes.cache.NodeCacheListener; //导入依赖的package包/类
private void registerListener(final CuratorFramework curator, final String path, final ZookeeperConfigurationListener listener) throws Exception
{
final NodeCache cache = new NodeCache(curator, path, true);
cache.start();
cache.getListenable().addListener(new NodeCacheListener()
{
@Override
public void nodeChanged() throws Exception
{
listener.onChange(curator, cache, path);
}
});
childrens.add(cache);
logger.info(String.format("Add listener %s for %s", listener, path));
}
示例11: serviceStart
import org.apache.curator.framework.recipes.cache.NodeCacheListener; //导入依赖的package包/类
@Override
protected void serviceStart() throws Exception {
framework.start();
nodeCache = new NodeCache(framework, zkMountTablePath, false);
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
handleMountTableChange(nodeCache.getCurrentData().getData());
}
});
nodeCache.start(false);
}
示例12: listenForZNode
import org.apache.curator.framework.recipes.cache.NodeCacheListener; //导入依赖的package包/类
/**
* Listen for a single node
*
* @param path
* @param listener
* @throws Exception
*/
public void listenForZNode(String path, ZookeeperEventListener listener) throws Exception {
init();
// TODO: simplify code of this method to:
// ZNodeListener nodeListener = new ZNodeListener(path, listener, client);
NodeCache cache = new NodeCache(client, path);
cache.start();
NodeCacheListener cacheListener = new ZNodeListener(
listener.getHandler(), cache);
cache.getListenable().addListener(cacheListener);
cachePool.add(cache);
}
示例13: forcReadListenForZNode
import org.apache.curator.framework.recipes.cache.NodeCacheListener; //导入依赖的package包/类
public void forcReadListenForZNode(String path, ZookeeperEventListener listener) throws Exception {
init();
NodeCache cache = new NodeCache(client, path);
cache.start();
NodeCacheListener cacheListener = new ZNodeListener(
listener.getHandler(), cache);
cache.getListenable().addListener(cacheListener);
cacheListener.nodeChanged();
cachePool.add(cache);
}
示例14: _initCacheWatcher
import org.apache.curator.framework.recipes.cache.NodeCacheListener; //导入依赖的package包/类
private void _initCacheWatcher() {
cacheNodeWatcher = CacheBuilder.newBuilder()
.concurrencyLevel(Runtime.getRuntime().availableProcessors()).maximumSize(10000)
.expireAfterAccess(3600, TimeUnit.SECONDS)
.removalListener(new RemovalListener<String, NodeCache>() {
@Override
public void onRemoval(RemovalNotification<String, NodeCache> event) {
try {
event.getValue().close();
} catch (IOException e) {
LOGGER.warn(e.getMessage(), e);
}
}
}).build(new CacheLoader<String, NodeCache>() {
@Override
public NodeCache load(final String path) throws Exception {
final NodeCache nodeCache = new NodeCache(curatorFramework, path);
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
ChildData data = nodeCache.getCurrentData();
_invalidateCache(path, data != null ? data.getData() : null);
}
});
nodeCache.start();
return nodeCache;
}
});
}
示例15: registerUpdater
import org.apache.curator.framework.recipes.cache.NodeCacheListener; //导入依赖的package包/类
public void registerUpdater(ConfigurationUpdater updater) {
NodeCache cache = getOrCreateNodeCache(updater.getPath());
cache
.getListenable()
.addListener(
new NodeCacheListener() {
@Override
public void nodeChanged() {
updater.update(cache.getCurrentData().getData());
}
});
}