/** * @Title: registerListeners @TitleExplain: 向zk注册监听 @Description: 注册监听 * * @param client * @author wudan-mac */ private void registerListeners(CuratorFramework client) { /* * 注册监听者 */ client .getConnectionStateListenable() .addListener( new ConnectionStateListener() { @Override public void stateChanged( CuratorFramework client, ConnectionState newState) { // 当状态发生变化时通知监听者 logger.debug("CuratorFramework state changed: {}", newState); if (newState == ConnectionState.CONNECTED || newState == ConnectionState.RECONNECTED) { for (IZKListener listener : listeners) { listener.executor(client); logger.debug("Listener {} executed!", listener.getClass().getName()); } } } }); /* *异常处理 */ client .getUnhandledErrorListenable() .addListener( new UnhandledErrorListener() { @Override public void unhandledError(String message, Throwable e) { logger.debug("CuratorFramework unhandledError: {}", message); } }); }
/** * 连接 * * @return */ public boolean connect() { try { client .getConnectionStateListenable() .addListener( new ConnectionStateListener() { public void stateChanged(CuratorFramework framework, ConnectionState state) { logger.info("Zookeeper状态:" + state.name()); if (state.equals(ConnectionState.RECONNECTED)) { logger.info("Zookeeper状态:检查节点"); for (String key : backup.keySet()) { String value = backup.get(key); createNode(key, value); } } } }); client.start(); client.getZookeeperClient().blockUntilConnectedOrTimedOut(); createRoomProcessorNode(); return true; } catch (Exception e) { logger.error("ZooKeeperWrapper.connect出现异常", e); return false; } }
/** * The discovery must be started before use * * @throws Exception errors */ @Override public void start() throws Exception { try { reRegisterServices(); } catch (KeeperException e) { log.error("Could not register instances - will try again later", e); } client.getConnectionStateListenable().addListener(connectionStateListener); }
@Test public void assertAddConnectionStateListener() { CuratorFramework client = mock(CuratorFramework.class); @SuppressWarnings("unchecked") Listenable<ConnectionStateListener> listeners = mock(Listenable.class); ConnectionStateListener listener = mock(ConnectionStateListener.class); when(client.getConnectionStateListenable()).thenReturn(listeners); when(coordinatorRegistryCenter.getRawClient()).thenReturn(client); jobNodeStorage.addConnectionStateListener(listener); verify(listeners).addListener(listener); }
@Override public void close() throws IOException { if (!state.compareAndSet(State.STARTED, State.CLOSED)) { return; } client.getConnectionStateListenable().removeListener(connectionStateListener); try { deleteNode(); } catch (Exception e) { ThreadUtils.checkInterrupted(e); throw new IOException(e); } }
@Override public void close() throws IOException { for (ServiceCache<T> cache : Lists.newArrayList(caches)) { CloseableUtils.closeQuietly(cache); } for (ServiceProvider<T> provider : Lists.newArrayList(providers)) { CloseableUtils.closeQuietly(provider); } for (Entry<T> entry : services.values()) { try { internalUnregisterService(entry); } catch (KeeperException.NoNodeException ignore) { // ignore } catch (Exception e) { log.error("Could not unregister instance: " + entry.service.getName(), e); } } client.getConnectionStateListenable().removeListener(connectionStateListener); }
/** * You must call start() to initiate the persistent node. An attempt to create the node in the * background will be started */ public void start() { Preconditions.checkState(state.compareAndSet(State.LATENT, State.STARTED), "Already started"); client.getConnectionStateListenable().addListener(connectionStateListener); createNode(); }