void addStateChange(ConnectionState newConnectionState) { connectionStateManager.addStateChange(newConnectionState); }
@Override public boolean blockUntilConnected(int maxWaitTime, TimeUnit units) throws InterruptedException { return connectionStateManager.blockUntilConnected(maxWaitTime, units); }
@Override public Listenable<ConnectionStateListener> getConnectionStateListenable() { return connectionStateManager.getListenable(); }
/** * Change to {@link ConnectionState#SUSPENDED} only if not already suspended and not lost * * @return true if connection is set to SUSPENDED */ public synchronized boolean setToSuspended() { if ( state.get() != State.STARTED ) { return false; } if ( (currentConnectionState == ConnectionState.LOST) || (currentConnectionState == ConnectionState.SUSPENDED) ) { return false; } setCurrentConnectionState(ConnectionState.SUSPENDED); postState(ConnectionState.SUSPENDED); return true; }
boolean setToSuspended() { return connectionStateManager.setToSuspended(); }
connectionStateManager.close(); client.close();
connectionStateManager.start(); // ordering dependency - must be called before client.start()
threadFactory = getThreadFactory(builder); maxCloseWaitMs = builder.getMaxCloseWaitMs(); connectionStateManager = new ConnectionStateManager(this, builder.getThreadFactory(), builder.getSessionTimeoutMs(), builder.getConnectionHandlingPolicy().getSimulatedSessionExpirationPercent()); compressionProvider = builder.getCompressionProvider(); aclProvider = builder.getAclProvider();
public synchronized boolean blockUntilConnected(int maxWaitTime, TimeUnit units) throws InterruptedException { long startTime = System.currentTimeMillis(); boolean hasMaxWait = (units != null); long maxWaitTimeMs = hasMaxWait ? TimeUnit.MILLISECONDS.convert(maxWaitTime, units) : 0; while ( !isConnected() ) { if ( hasMaxWait ) { long waitTime = maxWaitTimeMs - (System.currentTimeMillis() - startTime); if ( waitTime <= 0 ) { return isConnected(); } wait(waitTime); } else { wait(); } } return isConnected(); }
/** * Change to {@link ConnectionState#SUSPENDED} only if not already suspended and not lost * * @return true if connection is set to SUSPENDED */ public synchronized boolean setToSuspended() { if ( state.get() != State.STARTED ) { return false; } if ( (currentConnectionState == ConnectionState.LOST) || (currentConnectionState == ConnectionState.SUSPENDED) ) { return false; } currentConnectionState = ConnectionState.SUSPENDED; postState(ConnectionState.SUSPENDED); return true; }
private void suspendConnection() { if ( !connectionStateManager.setToSuspended() ) { return; } doSyncForSuspendedConnection(client.getInstanceIndex()); }
connectionStateManager.close(); client.close(); namespaceWatcherMap.close();
connectionStateManager.start(); // ordering dependency - must be called before client.start()
public CuratorFrameworkImpl(CuratorFrameworkFactory.Builder builder) { ZookeeperFactory localZookeeperFactory = makeZookeeperFactory(builder.getZookeeperFactory()); this.client = new CuratorZookeeperClient(localZookeeperFactory, builder.getEnsembleProvider(), builder.getSessionTimeoutMs(), builder.getConnectionTimeoutMs(), new Watcher() { @Override public void process(WatchedEvent watchedEvent) { CuratorEvent event = new CuratorEventImpl(CuratorFrameworkImpl.this, CuratorEventType.WATCHED, watchedEvent.getState().getIntValue(), unfixForNamespace(watchedEvent.getPath()), null, null, null, null, null, watchedEvent, null); processEvent(event); } }, builder.getRetryPolicy(), builder.canBeReadOnly()); listeners = new ListenerContainer<CuratorListener>(); unhandledErrorListeners = new ListenerContainer<UnhandledErrorListener>(); backgroundOperations = new DelayQueue<OperationAndData<?>>(); namespace = new NamespaceImpl(this, builder.getNamespace()); threadFactory = getThreadFactory(builder); maxCloseWaitMs = builder.getMaxCloseWaitMs(); connectionStateManager = new ConnectionStateManager(this, builder.getThreadFactory()); compressionProvider = builder.getCompressionProvider(); aclProvider = builder.getAclProvider(); state = new AtomicReference<CuratorFrameworkState>(CuratorFrameworkState.LATENT); useContainerParentsIfAvailable = builder.useContainerParentsIfAvailable(); byte[] builderDefaultData = builder.getDefaultData(); defaultData = (builderDefaultData != null) ? Arrays.copyOf(builderDefaultData, builderDefaultData.length) : new byte[0]; authInfos = buildAuths(builder); failedDeleteManager = new FailedDeleteManager(this); namespaceFacadeCache = new NamespaceFacadeCache(this); }
/** * Post a state change. If the manager is already in that state the change * is ignored. Otherwise the change is queued for listeners. * * @param newConnectionState new state * @return true if the state actually changed, false if it was already at that state */ public synchronized boolean addStateChange(ConnectionState newConnectionState) { if ( state.get() != State.STARTED ) { return false; } ConnectionState previousState = currentConnectionState; if ( previousState == newConnectionState ) { return false; } setCurrentConnectionState(newConnectionState); ConnectionState localState = newConnectionState; boolean isNegativeMessage = ((newConnectionState == ConnectionState.LOST) || (newConnectionState == ConnectionState.SUSPENDED) || (newConnectionState == ConnectionState.READ_ONLY)); if ( !isNegativeMessage && initialConnectMessageSent.compareAndSet(false, true) ) { localState = ConnectionState.CONNECTED; } postState(localState); return true; }
public synchronized boolean blockUntilConnected(int maxWaitTime, TimeUnit units) throws InterruptedException { long startTime = System.currentTimeMillis(); boolean hasMaxWait = (units != null); long maxWaitTimeMs = hasMaxWait ? TimeUnit.MILLISECONDS.convert(maxWaitTime, units) : 0; while ( !isConnected() ) { if ( hasMaxWait ) { long waitTime = maxWaitTimeMs - (System.currentTimeMillis() - startTime); if ( waitTime <= 0 ) { return isConnected(); } wait(waitTime); } else { wait(); } } return isConnected(); }
/** * Post a state change. If the manager is already in that state the change * is ignored. Otherwise the change is queued for listeners. * * @param newConnectionState new state * @return true if the state actually changed, false if it was already at that state */ public synchronized boolean addStateChange(ConnectionState newConnectionState) { if ( state.get() != State.STARTED ) { return false; } ConnectionState previousState = currentConnectionState; if ( previousState == newConnectionState ) { return false; } currentConnectionState = newConnectionState; ConnectionState localState = newConnectionState; boolean isNegativeMessage = ((newConnectionState == ConnectionState.LOST) || (newConnectionState == ConnectionState.SUSPENDED) || (newConnectionState == ConnectionState.READ_ONLY)); if ( !isNegativeMessage && initialConnectMessageSent.compareAndSet(false, true) ) { localState = ConnectionState.CONNECTED; } postState(localState); return true; }
void checkInstanceIndex() { long instanceIndex = client.getInstanceIndex(); long newInstanceIndex = currentInstanceIndex.getAndSet(instanceIndex); if ( (newInstanceIndex >= 0) && (instanceIndex != newInstanceIndex) ) // currentInstanceIndex is initially -1 - ignore this { connectionStateManager.addStateChange(ConnectionState.LOST); } }
@Override public boolean blockUntilConnected(int maxWaitTime, TimeUnit units) throws InterruptedException { return connectionStateManager.blockUntilConnected(maxWaitTime, units); }
@Override public Listenable<ConnectionStateListener> getConnectionStateListenable() { return connectionStateManager.getListenable(); }