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

The following examples show how to use org.I0Itec.zkclient.ZkClient#subscribeStateChanges() . 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: ZkclientZookeeperClient.java    From JobX with Apache License 2.0 6 votes vote down vote up
public ZkclientZookeeperClient(URL url) {
    super(url);
    client = new ZkClient(url.getBackupAddress(), Constants.ZK_CONNECTION_TIMEOUT);
    client.subscribeStateChanges(new IZkStateListener() {
        public void handleStateChanged(KeeperState state) throws Exception {
            ZkclientZookeeperClient.this.state = state;
            if (state == KeeperState.Disconnected) {
                stateChanged(StateListener.DISCONNECTED);
            } else if (state == KeeperState.SyncConnected) {
                stateChanged(StateListener.CONNECTED);
            }
        }

        public void handleNewSession() throws Exception {
            stateChanged(StateListener.RECONNECTED);
        }

        @Override
        public void handleSessionEstablishmentError(Throwable throwable) throws Exception {

        }
    });
}
 
Example 2
Source File: ZkclientZookeeperClient.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public ZkclientZookeeperClient(URL url) {
	super(url);
	client = new ZkClient(
               url.getBackupAddress(),
               url.getParameter(Constants.SESSION_TIMEOUT_KEY, Constants.DEFAULT_SESSION_TIMEOUT),
               url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_REGISTRY_CONNECT_TIMEOUT));
	client.subscribeStateChanges(new IZkStateListener() {
		public void handleStateChanged(KeeperState state) throws Exception {
			ZkclientZookeeperClient.this.state = state;
			if (state == KeeperState.Disconnected) {
				stateChanged(StateListener.DISCONNECTED);
			} else if (state == KeeperState.SyncConnected) {
				stateChanged(StateListener.CONNECTED);
			}
		}
		public void handleNewSession() throws Exception {
			stateChanged(StateListener.RECONNECTED);
		}
	});
}
 
Example 3
Source File: ZkclientZookeeperClient.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public ZkclientZookeeperClient(URL url) {
	super(url);
	client = new ZkClient(
               url.getBackupAddress(),
               url.getParameter(Constants.SESSION_TIMEOUT_KEY, Constants.DEFAULT_SESSION_TIMEOUT),
               url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_REGISTRY_CONNECT_TIMEOUT));
	client.subscribeStateChanges(new IZkStateListener() {
		public void handleStateChanged(KeeperState state) throws Exception {
			ZkclientZookeeperClient.this.state = state;
			if (state == KeeperState.Disconnected) {
				stateChanged(StateListener.DISCONNECTED);
			} else if (state == KeeperState.SyncConnected) {
				stateChanged(StateListener.CONNECTED);
			}
		}
		public void handleNewSession() throws Exception {
			stateChanged(StateListener.RECONNECTED);
		}
	});
}
 
Example 4
Source File: ZclientChange.java    From javabase with Apache License 2.0 6 votes vote down vote up
private static void test() {
    final ZkClient zkClient = new ZkClient("localhost:2181");

    zkClient.subscribeStateChanges(new IZkStateListener() {
        @Override
        public void handleStateChanged(Watcher.Event.KeeperState state) throws Exception {
            //当zookeeper被关闭时候 会触发这个事件
            if (state == Watcher.Event.KeeperState.Disconnected) {
              log.info("Disconnected");
            } else if (state == Watcher.Event.KeeperState.SyncConnected) {
                //  当zookeeper被重新连接的时候 会触发这个事件
                log.info("SyncConnected");
            }
        }
        @Override
        public void handleNewSession() throws Exception {
            log.info("handleNewSession");
        }

        @Override
        public void handleSessionEstablishmentError(Throwable error) throws Exception {
            log.info("handleSessionEstablishmentError");
        }
    });
}
 
Example 5
Source File: ZkclientZookeeperClient.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public ZkclientZookeeperClient(URL url) {
	super(url);
	client = new ZkClient(
               url.getBackupAddress(),
               url.getParameter(Constants.SESSION_TIMEOUT_KEY, Constants.DEFAULT_SESSION_TIMEOUT),
               url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_REGISTRY_CONNECT_TIMEOUT));
	client.subscribeStateChanges(new IZkStateListener() {
		public void handleStateChanged(KeeperState state) throws Exception {
			ZkclientZookeeperClient.this.state = state;
			if (state == KeeperState.Disconnected) {
				stateChanged(StateListener.DISCONNECTED);
			} else if (state == KeeperState.SyncConnected) {
				stateChanged(StateListener.CONNECTED);
			}
		}
		public void handleNewSession() throws Exception {
			stateChanged(StateListener.RECONNECTED);
		}
	});
}
 
Example 6
Source File: ZkClientZkClient.java    From light-task-scheduler with Apache License 2.0 6 votes vote down vote up
public ZkClientZkClient(Config config) {
    String registryAddress = NodeRegistryUtils.getRealRegistryAddress(config.getRegistryAddress());
    zkClient = new ZkClient(registryAddress, connectionTimeout);

    zkClient.subscribeStateChanges(new IZkStateListener() {

        public void handleStateChanged(Watcher.Event.KeeperState state) throws Exception {
            ZkClientZkClient.this.state = state;
            if (state == KeeperState.Disconnected) {
                stateChanged(StateListener.DISCONNECTED);
            } else if (state == KeeperState.SyncConnected) {
                stateChanged(StateListener.CONNECTED);
            } else if (state == KeeperState.Expired) {
                stateChanged(StateListener.DISCONNECTED);
            }
        }

        public void handleNewSession() throws Exception {
            stateChanged(StateListener.RECONNECTED);
        }
    });
}
 
Example 7
Source File: ZkclientZookeeperClient.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public ZkclientZookeeperClient(URL url) {
	super(url);
	client = new ZkClient(
               url.getBackupAddress(),
               url.getParameter(Constants.SESSION_TIMEOUT_KEY, Constants.DEFAULT_SESSION_TIMEOUT),
               url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_REGISTRY_CONNECT_TIMEOUT));
	client.subscribeStateChanges(new IZkStateListener() {
		public void handleStateChanged(KeeperState state) throws Exception {
			ZkclientZookeeperClient.this.state = state;
			if (state == KeeperState.Disconnected) {
				stateChanged(StateListener.DISCONNECTED);
			} else if (state == KeeperState.SyncConnected) {
				stateChanged(StateListener.CONNECTED);
			}
		}
		public void handleNewSession() throws Exception {
               stateChanged(StateListener.RECONNECTED);
           }

           //@Override
           public void handleSessionEstablishmentError(Throwable error) throws Exception {
               //TODO list...
               logger.error("zookeeper connection error!", error);
               throw new Exception(error);
           }
	});
}