Java Code Examples for org.I0Itec.zkclient.ZkClient#subscribeDataChanges()

The following examples show how to use org.I0Itec.zkclient.ZkClient#subscribeDataChanges() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocalCacheUtil.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Start listener.
 * 开始向zookeeper中心监听 notifyName 对应的改变
 *
 * @param notifyName the resource name
 * @return the long
 */
public static Long startListener(String notifyName) {
    ZkClient zClient = getZkClient();
    String resourcePath = getResourcePath(notifyName);
    String rsName = dataListenerCache.get(notifyName);
    if (rsName == null) {
        zClient.subscribeDataChanges(resourcePath, new org.stategen.framework.cache.LocalCacheUtil.ZkResourceDataListener(notifyName));
        dataListenerCache.put(notifyName, notifyName);
    }
    return getResourceNano(notifyName);
}
 
Example 2
Source File: ZkTest.java    From SkyEye with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {

        String zkHost = "192.168.88.196:2182,192.168.88.197:2182,192.168.88.198:2182";

        // 进行监控
        ZkClient zkClient = new ZkClient(zkHost, 60000, 5000);

        System.out.println("你妹的: " + zkClient.readData(Constants.ROOT_PATH_PERSISTENT + Constants.SLASH + "app-test"));



        List<String> apps = zkClient.getChildren(Constants.ROOT_PATH_EPHEMERAL);
        for (String app : apps) {
            zkClient.subscribeChildChanges(Constants.ROOT_PATH_EPHEMERAL + Constants.SLASH + app, new AppChildChangeListener());
            List<String> hosts = zkClient.getChildren(Constants.ROOT_PATH_EPHEMERAL + Constants.SLASH + app);
            for (String host : hosts) {
                zkClient.subscribeDataChanges(Constants.ROOT_PATH_EPHEMERAL + Constants.SLASH + app + Constants.SLASH + host, new HostDataChangeListener());
                System.out.println();
            }
        }

        synchronized (ZkTest.class) {
            while (true) {
                try {
                    ZkTest.class.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
Example 3
Source File: ZKConfigHandler.java    From sumk with Apache License 2.0 5 votes vote down vote up
public static NamePairs readAndListen(String zkUrl, String path, IZkDataListener listener) {
	ZkClient client = ZkClientHelper.getZkClient(zkUrl);
	if (!client.exists(path)) {
		return null;
	}
	String data = ZkClientHelper.data2String(client.readData(path));
	if (listener != null) {
		client.subscribeDataChanges(path, listener);
	}
	return new NamePairs(data);
}
 
Example 4
Source File: DynamicBrokerSelector.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
public DynamicBrokerSelector(String zkServers) {
  ZkClient zkClient = new ZkClient(zkServers);
  zkClient.setZkSerializer(new BytesPushThroughSerializer());
  zkClient.waitUntilConnected(60, TimeUnit.SECONDS);
  zkClient.subscribeDataChanges(ExternalViewReader.BROKER_EXTERNAL_VIEW_PATH, this);
  evReader = new ExternalViewReader(zkClient);
  refresh();
}
 
Example 5
Source File: ZkConfigUtil.java    From zkconfigutil with Apache License 2.0 4 votes vote down vote up
public final synchronized void register(Class<?> cla,
		boolean isCreateIfNUll, String rootPath)
		throws NotRegistedException, InstantiationException,
		IllegalAccessException {
	// if (!cla.isAnnotationPresent(TypeZkConfigurable.class)) {
	// throw new NotRegistedException();
	// }
	final ZkClient zkClient;
	if ("".equals(this.globalZkServer)) {
		logger.error("please set globalZkServer or set typeZkConfigurable.useOwnZkServer()=false to use own zkserver system will exit!!!");
		System.exit(0);
	}
	zkClient = this.makeZkClient(this.globalZkServer);
	// String packagePath = cla.getPackage().getName();
	// packagePath = packagePath.replaceAll("\\.", "/");
	//
	// rootPath = this.makeZkPath(rootPath, packagePath);
	String path = this.makeZkPath(rootPath, cla.getName());

	path = path.replace("$", "/"); // inclass

	final Field[] fields = cla.getDeclaredFields();

	for (Field field : fields) {
		if (!field.isAnnotationPresent(FieldZkConfigurable.class))
			continue;
		logger.info("field : " + field.getName() + "   type : "
				+ field.getType().getSimpleName());
		FieldZkConfigurable fieldZkConfigurable = field
				.getAnnotation(FieldZkConfigurable.class);

		final String fieldPath = this.makeZkPath(path, field.getName());

		String value = zkClient.readData(fieldPath, true);
		logger.info(fieldPath + " : " + value);

		Class<? extends AbstractResolve> resolve = fieldZkConfigurable
				.resolve();
		Resolve resolveInstance;
		if (resolve == ReflectResolve.class) {
			resolveInstance = new ReflectResolve(cla, field);
		} else {
			resolveInstance = resolve.newInstance();
		}

		/**
		 * Dosen't have value
		 */
		if (value == null && !isCreateIfNUll) {
			continue;
		} else if (value == null && isCreateIfNUll) {
			zkClient.createPersistent(fieldPath, true);
			String defaultValue = resolveInstance.resolve();
			zkClient.writeData(fieldPath, defaultValue);
		} else {
			/**
			 * have value
			 */
			resolveInstance.dResolve(value);
		}

		/**
		 * for USR2 signal
		 */
		SignalHelper.mark(cla, fieldPath, resolveInstance,
				fieldZkConfigurable.dynamicUpdate());

		if (fieldZkConfigurable.dynamicUpdate()) {
			logger.info("dynamicUpdate " + fieldPath);
			zkClient.subscribeDataChanges(fieldPath, this);
			Updater.register(fieldPath, resolveInstance);
		}
	}
}