当前位置: 首页>>代码示例>>Java>>正文


Java CuratorWatcher类代码示例

本文整理汇总了Java中org.apache.curator.framework.api.CuratorWatcher的典型用法代码示例。如果您正苦于以下问题:Java CuratorWatcher类的具体用法?Java CuratorWatcher怎么用?Java CuratorWatcher使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


CuratorWatcher类属于org.apache.curator.framework.api包,在下文中一共展示了CuratorWatcher类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: addDataListener

import org.apache.curator.framework.api.CuratorWatcher; //导入依赖的package包/类
public void addDataListener (String path, ZkDataListener dataListener) {
    if (dataListener == null)
        return;

    ConcurrentMap<ZkDataListener, CuratorWatcher> listeners = acitveDataListeners.get(path);
    if (listeners == null) {
        acitveDataListeners.putIfAbsent(path,new ConcurrentHashMap<ZkDataListener, CuratorWatcher>());
        listeners = acitveDataListeners.get(path);
    }

    CuratorWatcher curatorWatcher = listeners.get(dataListener);
    if (curatorWatcher == null) {
        listeners.putIfAbsent(dataListener,new CuratorWatcherImpl(dataListener));
        curatorWatcher = listeners.get(dataListener);
    }

    try {
        curator.getData().usingWatcher(curatorWatcher).forPath(path);
    } catch (Exception e) {
        LOGGER.warn("",e);
    }
}
 
开发者ID:variflight,项目名称:feeyo-redisproxy,代码行数:23,代码来源:ZkClient.java

示例2: checkSignal

import org.apache.curator.framework.api.CuratorWatcher; //导入依赖的package包/类
private void checkSignal() {

        try {
            client.checkExists().usingWatcher(new CuratorWatcher() {
                @Override
                public void process(WatchedEvent watchedEvent) throws Exception {
                    if (watchedEvent.getType().equals(Watcher.Event.EventType.NodeCreated)) {
                        System.out.println("Updating queries!");
                        _siddhiManager.shutdown();
                        parserQuerysFile();
                        if (client.checkExists().forPath("/query-siddhi/update") != null)
                            client.delete().forPath("/query-siddhi/update");
                    }
                    checkSignal();
                }
            }).forPath("/query-siddhi/update");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
 
开发者ID:redBorder,项目名称:rb-bi,代码行数:22,代码来源:SiddhiState.java

示例3: register

import org.apache.curator.framework.api.CuratorWatcher; //导入依赖的package包/类
public void register() throws Exception {

		String ip = InetAddress.getLocalHost().getHostAddress();
		String registeNode = "zk/register/" + ip;// 节点路径

		byte[] data = "disable".getBytes(charset);// 节点值

		CuratorWatcher watcher = new ZKWatchRegister(registeNode, data); // 创建一个register
																			// watcher

		Stat stat = zkTools.checkExists().forPath(registeNode);
		if (stat != null) {
			zkTools.delete().forPath(registeNode);
		}
		zkTools.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
				.forPath(registeNode, data);// 创建的路径和值

		// 添加到session过期监控事件中
		addReconnectionWatcher(registeNode, ZookeeperWatcherType.CREATE_ON_NO_EXITS, watcher);
		data = zkTools.getData().usingWatcher(watcher).forPath(registeNode);
		System.out.println("get path form zk : " + registeNode + ":" + new String(data, charset));
	}
 
开发者ID:benson-git,项目名称:ibole-microservice,代码行数:23,代码来源:CuratorTest.java

示例4: start

import org.apache.curator.framework.api.CuratorWatcher; //导入依赖的package包/类
public void start() throws Exception {
    CuratorFramework curatorFramework = serviceRegistry.getService().getCuratorFramework();
    curatorFramework.getChildren().usingWatcher(new CuratorWatcher() {
        @Override
        public void process(WatchedEvent event) throws Exception {
            switch (event.getType()) {

                case NodeChildrenChanged: {
                    checkForUpdate();
                    break;
                }
                case None:
                case NodeCreated:
                case NodeDeleted:
                case NodeDataChanged:
                    break;
                default:
                    break;
            }
        }
    }).forPath(PathBuilder.path(serviceRegistry.getService())); //Start watcher on service node
    serviceRegistry.nodes(checkForUpdateOnZookeeper());
    logger.info("Started polling zookeeper for changes");
}
 
开发者ID:flipkart-incubator,项目名称:ranger,代码行数:25,代码来源:ServiceRegistryUpdater.java

示例5: watchDefaultStorage

import org.apache.curator.framework.api.CuratorWatcher; //导入依赖的package包/类
@PostConstruct
private void watchDefaultStorage() {
    try {
        curator.getData().usingWatcher((CuratorWatcher) event -> {
            final byte[] defaultStorageId = curator.getData().forPath(ZK_TIMELINES_DEFAULT_STORAGE);
            if (defaultStorageId != null) {
                final Result<Storage> storageResult = getStorage(new String(defaultStorageId));
                if (storageResult.isSuccessful()) {
                    defaultStorage.setStorage(storageResult.getValue());
                }
            }
            watchDefaultStorage();
        }).forPath(ZK_TIMELINES_DEFAULT_STORAGE);
    } catch (final Exception e) {
        LOG.warn("Error while creating watcher for default storage updates {}", e.getMessage(), e);
    }
}
 
开发者ID:zalando,项目名称:nakadi,代码行数:18,代码来源:StorageService.java

示例6: readCurrentMapping

import org.apache.curator.framework.api.CuratorWatcher; //导入依赖的package包/类
private synchronized ZMapping readCurrentMapping(Stat stat) {
    ZMapping zMap = null;
    try {
        if (client.getState().equals(CuratorFrameworkState.STOPPED)) {
            LOG.warn("Attempting to read state on stopped client.");
            return null;
        }
        byte[] data = client.getData().storingStatIn(stat).usingWatcher(new CuratorWatcher() {
            @Override
            public void process(WatchedEvent watchedEvent) throws Exception {
                ZMapping newMapping = readCurrentMapping();
                if (newMapping == null && mapping != null) {
                    LOG.warn("An attempt was made to overwrite current mapping with a null one.");
                } else {
                    mapping = newMapping;
                }
                LOG.debug("Host {} just updated its mapping to version {}", hostPort, (mapping != null) ? mapping.getVersion() : -1);
            }
        }).forPath(MAPPING_PATH);
        zMap = ZMapping.deserialize(data);
    } catch (Exception e) {
        LOG.error("Error reading current mapping: ", e);
    }
    return zMap;
}
 
开发者ID:tzaeschke,项目名称:distributed-phtree,代码行数:26,代码来源:ZKClusterService.java

示例7: getAndWatch

import org.apache.curator.framework.api.CuratorWatcher; //导入依赖的package包/类
public void getAndWatch(String key, String defaultValue, Consumer<String> listener) {
    try {
        CuratorWatcher cw = (WatchedEvent watchedEvent) -> {
            if (watchedEvent.getType() == Watcher.Event.EventType.NodeDataChanged) {
                byte[] value = cf.getData().forPath(watchedEvent.getPath());
                String strVal = new String(value, CHARSET);
                Logger.getLogger(getClass()).debug("ZK key " + key + " changed value to " + strVal);
                listener.accept(strVal);
            }
        };

        Stat stat = cf.checkExists().forPath(PREFIX + key);
        if (stat == null)
            cf.create().creatingParentsIfNeeded().forPath(PREFIX + key, defaultValue.getBytes(CHARSET));
        cf.getData().usingWatcher(cw).forPath(PREFIX + key);
        listener.accept(get(key, defaultValue));
    } catch (Exception ex) {
        Logger.getLogger(getClass()).error("Failed to add config watcher", ex);
    }
}
 
开发者ID:Neutrinet,项目名称:ISP-ng,代码行数:21,代码来源:Config.java

示例8: watch

import org.apache.curator.framework.api.CuratorWatcher; //导入依赖的package包/类
public void watch(String key, Consumer<String> listener) {
    try {
        CuratorWatcher cw = (WatchedEvent watchedEvent) -> {
            if (watchedEvent.getType() == Watcher.Event.EventType.NodeDataChanged) {
                byte[] value = cf.getData().forPath(watchedEvent.getPath());
                listener.accept(new String(value, CHARSET));
            }
        };

        Stat stat = cf.checkExists().forPath(PREFIX + key);
        if (stat == null)
            cf.create().creatingParentsIfNeeded().forPath(PREFIX + key, null);
        cf.getData().usingWatcher(cw).forPath(PREFIX + key);
    } catch (Exception ex) {
        Logger.getLogger(getClass()).error("Failed to add config watcher", ex);
    }
}
 
开发者ID:Neutrinet,项目名称:ISP-ng,代码行数:18,代码来源:Config.java

示例9: addWatcher

import org.apache.curator.framework.api.CuratorWatcher; //导入依赖的package包/类
private ZookeeperConfigService addWatcher(String path) throws Exception {
	zkClient.getChildren().usingWatcher(new CuratorWatcher() {
		public void process(WatchedEvent event) throws Exception {
			// updateLocalRules();
		}

	}).forPath(path);
	return this;
}
 
开发者ID:polarcoral,项目名称:monica,代码行数:10,代码来源:ZookeeperConfigService.java

示例10: removeDataListener

import org.apache.curator.framework.api.CuratorWatcher; //导入依赖的package包/类
public void removeDataListener(String path, ZkDataListener listener) {
    if (listener == null)
        return;

    ConcurrentMap<ZkDataListener, CuratorWatcher> listeners = acitveDataListeners.get(path);
    if (listeners != null) {
        CuratorWatcher curatorWatcher = listeners.remove(listener);
        if (curatorWatcher != null) {
            ((CuratorWatcherImpl)curatorWatcher).unwatch();
        }
    }
}
 
开发者ID:variflight,项目名称:feeyo-redisproxy,代码行数:13,代码来源:ZkClient.java

示例11: addReconnectionWatcher

import org.apache.curator.framework.api.CuratorWatcher; //导入依赖的package包/类
public void addReconnectionWatcher(final String path, final ZookeeperWatcherType watcherType,
		final CuratorWatcher watcher) {
	synchronized (this) {
		if (!watchers.contains(watcher.toString()))// 不要添加重复的监听事件
		{
			watchers.add(watcher.toString());
			System.out.println("add new watcher " + watcher);
			zkTools.getConnectionStateListenable().addListener(new ConnectionStateListener() {
				@Override
				public void stateChanged(CuratorFramework client, ConnectionState newState) {
					System.out.println(newState);
					if (newState == ConnectionState.LOST) {// 处理session过期
						try {
							if (watcherType == ZookeeperWatcherType.EXITS) {
								zkTools.checkExists().usingWatcher(watcher).forPath(path);
							} else if (watcherType == ZookeeperWatcherType.GET_CHILDREN) {
								zkTools.getChildren().usingWatcher(watcher).forPath(path);
							} else if (watcherType == ZookeeperWatcherType.GET_DATA) {
								zkTools.getData().usingWatcher(watcher).forPath(path);
							} else if (watcherType == ZookeeperWatcherType.CREATE_ON_NO_EXITS) {
								// ephemeral类型的节点session过期了,需要重新创建节点,并且注册监听事件,之后监听事件中,
								// 会处理create事件,将路径值恢复到先前状态
								Stat stat = zkTools.checkExists().usingWatcher(watcher).forPath(path);
								if (stat == null) {
									System.err.println("to create");
									zkTools.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL)
											.withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE).forPath(path);
								}
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			});
		}
	}
}
 
开发者ID:benson-git,项目名称:ibole-microservice,代码行数:39,代码来源:CuratorTest.java

示例12: exists

import org.apache.curator.framework.api.CuratorWatcher; //导入依赖的package包/类
public static boolean exists(CuratorFramework client, String path, CuratorWatcher watcher) {
    try {
        if (watcher != null) {
            return ((BackgroundPathable) client.checkExists().usingWatcher(watcher)).forPath(path) != null;
        }
        return client.checkExists().forPath(path) != null;
    } catch (Exception e) {
        LOGGER.error("ZKUtil-->>exists(CuratorFramework client, String path, CuratorWatcher watcher) error, ", e);
    }
    return false;
}
 
开发者ID:classtag,项目名称:scratch_zookeeper_netty,代码行数:12,代码来源:ZKUtil.java

示例13: getData

import org.apache.curator.framework.api.CuratorWatcher; //导入依赖的package包/类
public static String getData(CuratorFramework client, String path, CuratorWatcher watcher) {
    try {
        if (client.checkExists().forPath(path) == null) {
            return null;
        }
        if (watcher != null) {
            return List2StringUtil
                    .toString((byte[]) ((BackgroundPathable) client.getData().usingWatcher(watcher)).forPath(path));
        }
        return List2StringUtil.toString((byte[]) client.getData().forPath(path));
    } catch (Exception e) {
        LOGGER.error("ZKUtil-->>getData(CuratorFramework client, String path, CuratorWatcher watcher)  error ", e);
    }
    return null;
}
 
开发者ID:classtag,项目名称:scratch_zookeeper_netty,代码行数:16,代码来源:ZKUtil.java

示例14: getChilds

import org.apache.curator.framework.api.CuratorWatcher; //导入依赖的package包/类
public static List<String> getChilds(CuratorFramework client, String path, CuratorWatcher watcher) {
    try {
        if (watcher != null) {
            return (List) ((BackgroundPathable) client.getChildren().usingWatcher(watcher)).forPath(path);
        }
        return (List) client.getChildren().forPath(path);
    } catch (Exception e) {
        LOGGER.error("ZKUtil-->>getChilds(CuratorFramework client, String path, CuratorWatcher watcher) error,", e);
    }
    return null;
}
 
开发者ID:classtag,项目名称:scratch_zookeeper_netty,代码行数:12,代码来源:ZKUtil.java

示例15: readSize

import org.apache.curator.framework.api.CuratorWatcher; //导入依赖的package包/类
private void readSize(final String hostId) {
    String path = sizePath(hostId);

    try {
        byte[] data = client.getData().usingWatcher(new CuratorWatcher() {
            @Override
            public void process(WatchedEvent watchedEvent) throws Exception {
                readSize(hostId);
            }
        }).forPath(path);
        this.sizes.put(hostId, byteToInt(data));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:tzaeschke,项目名称:distributed-phtree,代码行数:16,代码来源:ZKClusterService.java


注:本文中的org.apache.curator.framework.api.CuratorWatcher类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。