com.alibaba.dubbo.remoting.zookeeper.StateListener Java Examples

The following examples show how to use com.alibaba.dubbo.remoting.zookeeper.StateListener. 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: ZookeeperRegistry.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public ZookeeperRegistry(URL url, ZookeeperTransporter zookeeperTransporter) {
    super(url);
    if (url.isAnyHost()) {
		throw new IllegalStateException("registry address == null");
	}
    String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT);
    if (! group.startsWith(Constants.PATH_SEPARATOR)) {
        group = Constants.PATH_SEPARATOR + group;
    }
    this.root = group;
    zkClient = zookeeperTransporter.connect(url);
    zkClient.addStateListener(new StateListener() {
        public void stateChanged(int state) {
        	if (state == RECONNECTED) {
         	try {
		recover();
	} catch (Exception e) {
		logger.error(e.getMessage(), e);
	}
        	}
        }
    });
}
 
Example #2
Source File: CuratorZookeeperClient.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
public CuratorZookeeperClient(URL url) {
    super(url);
    try {
        Builder builder = CuratorFrameworkFactory.builder()
                .connectString(url.getBackupAddress())
                .retryPolicy(new RetryNTimes(Integer.MAX_VALUE, 1000))
                .connectionTimeoutMs(5000);
        String authority = url.getAuthority();
        if (authority != null && authority.length() > 0) {
            builder = builder.authorization("digest", authority.getBytes());
        }
        client = builder.build();
        client.getConnectionStateListenable().addListener((client, state) -> {
            if (state == ConnectionState.LOST) {
                CuratorZookeeperClient.this.stateChanged(StateListener.DISCONNECTED);
            } else if (state == ConnectionState.CONNECTED) {
                CuratorZookeeperClient.this.stateChanged(StateListener.CONNECTED);
            } else if (state == ConnectionState.RECONNECTED) {
                CuratorZookeeperClient.this.stateChanged(StateListener.RECONNECTED);
            }
        });
        client.start();
    } catch (Exception e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #3
Source File: ZookeeperRegistry.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
public ZookeeperRegistry(URL url, ZookeeperTransporter zookeeperTransporter) {
    super(url);
    if (url.isAnyHost()) {
        throw new IllegalStateException("registry address == null");
    }
    String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT);
    if (!group.startsWith(Constants.PATH_SEPARATOR)) {
        group = Constants.PATH_SEPARATOR + group;
    }
    this.root = group;
    zkClient = zookeeperTransporter.connect(url);
    zkClient.addStateListener(state -> {
        if (state == StateListener.RECONNECTED) {
            try {
                recover();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    });
}
 
Example #4
Source File: ZkclientZookeeperClient.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
public ZkclientZookeeperClient(URL url) {
    super(url);
    client = new ZkClientWrapper(url.getBackupAddress(), 30000);
    client.addListener(new IZkStateListener() {
        @Override
        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);
            }
        }

        @Override
        public void handleNewSession() throws Exception {
            stateChanged(StateListener.RECONNECTED);
        }
    });
    client.start();
}
 
Example #5
Source File: ZookeeperRegistry.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public ZookeeperRegistry(URL url, ZookeeperTransporter zookeeperTransporter) {
    super(url);
    if (url.isAnyHost()) {
		throw new IllegalStateException("registry address == null");
	}
    String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT);
    if (! group.startsWith(Constants.PATH_SEPARATOR)) {
        group = Constants.PATH_SEPARATOR + group;
    }
    this.root = group;
    zkClient = zookeeperTransporter.connect(url);
    zkClient.addStateListener(new StateListener() {
        public void stateChanged(int state) {
        	if (state == RECONNECTED) {
         	try {
		recover();
	} catch (Exception e) {
		logger.error(e.getMessage(), e);
	}
        	}
        }
    });
}
 
Example #6
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 #7
Source File: ZookeeperRegistry.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public ZookeeperRegistry(URL url, ZookeeperTransporter zookeeperTransporter) {
    super(url);
    if (url.isAnyHost()) {
		throw new IllegalStateException("registry address == null");
	}
    String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT);
    if (! group.startsWith(Constants.PATH_SEPARATOR)) {
        group = Constants.PATH_SEPARATOR + group;
    }
    this.root = group;
    zkClient = zookeeperTransporter.connect(url);
    zkClient.addStateListener(new StateListener() {
        public void stateChanged(int state) {
        	if (state == RECONNECTED) {
         	try {
		recover();
	} catch (Exception e) {
		logger.error(e.getMessage(), e);
	}
        	}
        }
    });
}
 
Example #8
Source File: ZookeeperRegistry.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public ZookeeperRegistry(URL url, ZookeeperTransporter zookeeperTransporter) {
    super(url);
    if (url.isAnyHost()) {
		throw new IllegalStateException("registry address == null");
	}
    String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT);
    if (! group.startsWith(Constants.PATH_SEPARATOR)) {
        group = Constants.PATH_SEPARATOR + group;
    }
    this.root = group;
    zkClient = zookeeperTransporter.connect(url);
    zkClient.addStateListener(new StateListener() {
        public void stateChanged(int state) {
        	if (state == RECONNECTED) {
         	try {
		recover();
	} catch (Exception e) {
		logger.error(e.getMessage(), e);
	}
        	}
        }
    });
}
 
Example #9
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 #10
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 #11
Source File: ZookeeperRegistry.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
public ZookeeperRegistry(URL url, ZookeeperTransporter zookeeperTransporter) {
        super(url);
        if (url.isAnyHost()) {
            throw new IllegalStateException("registry address == null");
        }
//        group不进行配置默认值是dubbo
        String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT);
        if (!group.startsWith(Constants.PATH_SEPARATOR)) {
            group = Constants.PATH_SEPARATOR + group;
        }
        this.root = group;
//        创建zkclient=》
        zkClient = zookeeperTransporter.connect(url);
//        添加监听器
        zkClient.addStateListener(new StateListener() {
            @Override
            public void stateChanged(int state) {
                if (state == RECONNECTED) {
                    try {
//                        恢复注册信息=》
                        recover();
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    }
                }
            }
        });
    }
 
Example #12
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);
           }
	});
}
 
Example #13
Source File: AbstractZookeeperClient.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public void addStateListener(StateListener listener) {
	stateListeners.add(listener);
}
 
Example #14
Source File: AbstractZookeeperClient.java    From dubbox with Apache License 2.0 4 votes vote down vote up
protected void stateChanged(int state) {
	for (StateListener sessionListener : getSessionListeners()) {
		sessionListener.stateChanged(state);
	}
}
 
Example #15
Source File: AbstractZookeeperClient.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public Set<StateListener> getSessionListeners() {
	return stateListeners;
}
 
Example #16
Source File: AbstractZookeeperClient.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public void removeStateListener(StateListener listener) {
	stateListeners.remove(listener);
}
 
Example #17
Source File: AbstractZookeeperClient.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public void addStateListener(StateListener listener) {
	stateListeners.add(listener);
}
 
Example #18
Source File: AbstractZookeeperClient.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public void removeStateListener(StateListener listener) {
	stateListeners.remove(listener);
}
 
Example #19
Source File: AbstractZookeeperClient.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public Set<StateListener> getSessionListeners() {
	return stateListeners;
}
 
Example #20
Source File: AbstractZookeeperClient.java    From dubbox with Apache License 2.0 4 votes vote down vote up
protected void stateChanged(int state) {
	for (StateListener sessionListener : getSessionListeners()) {
		sessionListener.stateChanged(state);
	}
}
 
Example #21
Source File: AbstractZookeeperClient.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
public void addStateListener(StateListener listener) {
	stateListeners.add(listener);
}
 
Example #22
Source File: AbstractZookeeperClient.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
protected void stateChanged(int state) {
	for (StateListener sessionListener : getSessionListeners()) {
		sessionListener.stateChanged(state);
	}
}
 
Example #23
Source File: AbstractZookeeperClient.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
public Set<StateListener> getSessionListeners() {
	return stateListeners;
}
 
Example #24
Source File: AbstractZookeeperClient.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
public void removeStateListener(StateListener listener) {
	stateListeners.remove(listener);
}
 
Example #25
Source File: AbstractZookeeperClient.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
protected void stateChanged(int state) {
	for (StateListener sessionListener : getSessionListeners()) {
		sessionListener.stateChanged(state);
	}
}
 
Example #26
Source File: AbstractZookeeperClient.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
public Set<StateListener> getSessionListeners() {
	return stateListeners;
}
 
Example #27
Source File: AbstractZookeeperClient.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
public void removeStateListener(StateListener listener) {
	stateListeners.remove(listener);
}
 
Example #28
Source File: AbstractZookeeperClient.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
public void addStateListener(StateListener listener) {
	stateListeners.add(listener);
}
 
Example #29
Source File: AbstractZookeeperClient.java    From dubbox with Apache License 2.0 4 votes vote down vote up
protected void stateChanged(int state) {
	for (StateListener sessionListener : getSessionListeners()) {
		sessionListener.stateChanged(state);
	}
}
 
Example #30
Source File: AbstractZookeeperClient.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public Set<StateListener> getSessionListeners() {
	return stateListeners;
}