/** * Add this instance to the leadership election and attempt to acquire leadership. * * @throws Exception errors */ public void start() throws Exception { Preconditions.checkState( state.compareAndSet(State.LATENT, State.STARTED), "Cannot be started more than once"); client.getConnectionStateListenable().addListener(listener); reset(); }
/** * Add this instance to the leadership election and attempt to acquire leadership. * * @throws Exception errors */ public void start() throws Exception { Preconditions.checkState(state.compareAndSet(State.LATENT, State.STARTED), "Already started"); client.getConnectionStateListenable().addListener(listener); client.newNamespaceAwareEnsurePath(latchPath).ensure(client.getZookeeperClient()); internalStart(); }
/** * The shared value must be started before it can be used. Call {@link #close()} when you are * finished with the shared value * * @throws Exception ZK errors, interruptions, etc. */ public void start() throws Exception { Preconditions.checkState(state.compareAndSet(State.LATENT, State.STARTED), "already started"); client.getConnectionStateListenable().addListener(connectionStateListener); try { client.create().creatingParentsIfNeeded().forPath(path, seedValue); } catch (KeeperException.NodeExistsException ignore) { // ignore } readValue(); }
/** * Remove this instance from the leadership election. If this instance is the leader, leadership * is released. IMPORTANT: the only way to release leadership is by calling close(). All * LeaderLatch instances must eventually be closed. * * @throws IOException errors */ @Override public void close() throws IOException { Preconditions.checkState(state.compareAndSet(State.STARTED, State.CLOSED), "Not started"); try { client.delete().guaranteed().inBackground().forPath(ourPath); } catch (Exception e) { throw new IOException(e); } finally { client.getConnectionStateListenable().removeListener(listener); setLeadership(false); } }
/** * Remove this instance from the leadership election. If this instance is the leader, leadership * is released. IMPORTANT: the only way to release leadership is by calling close(). All * LeaderLatch instances must eventually be closed. * * @throws IOException errors */ @Override public void close() throws IOException { Preconditions.checkState( state.compareAndSet(State.STARTED, State.CLOSED), "Already closed or has not been started"); try { setNode(null); } catch (Exception e) { throw new IOException(e); } finally { client.getConnectionStateListenable().removeListener(listener); setLeadership(false); } }
@Override public void close() throws IOException { client.getConnectionStateListenable().removeListener(connectionStateListener); state.set(State.CLOSED); listeners.clear(); }
private void buildZKClient(final String zkFullPath) { final String[] zkFullPathItems = zkFullPath.split(ZK_FULL_PATH_DELIMITER); zkHost_ = zkFullPathItems[1]; zkNamespace_ = zkFullPathItems[2].substring( 0, zkFullPathItems[2].lastIndexOf(ZK_FULL_PATH_NAMESPACE_SEPERATOR)); zkNodePath_ = zkFullPathItems[2].substring( zkFullPathItems[2].lastIndexOf(ZK_FULL_PATH_NAMESPACE_SEPERATOR)); if (zkClient_ == null) { try { zkClient_ = CuratorFrameworkFactory.builder() .connectString(zkHost_) .namespace(zkNamespace_) .retryPolicy(new ExponentialBackoffRetry(1000, 60)) .connectionTimeoutMs(2 * 3600 * 1000) .build(); zkClient_ .getConnectionStateListenable() .addListener( new ConnectionStateListener() { @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { // TODO Auto-generated method stub if (newState == ConnectionState.LOST) { try { synchronized (this) { while (!client.getZookeeperClient().blockUntilConnectedOrTimedOut()) { if (logger_ != null) { if (logger_.isInfoEnabled()) logger_.info("waitting reconnected..."); } wait(1000); } } if (logger_ != null) { if (logger_.isInfoEnabled()) logger_.info("to zookeeper reconnected."); } doNodesDiscovery(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); if (logger_ != null) { if (logger_.isInfoEnabled()) logger_.info( String.format( "To re connect to zookeeper to generate an error, cause:%s", e)); } } } } }); zkClient_.start(); } catch (Throwable t) { zkClient_.close(); throw new IllegalStateException("building zookeeper client failed."); } } }