Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *      Copyright (C) 2012 DataStax Inc.
   *
   *   Licensed under the Apache License, Version 2.0 (the "License");
   *   you may not use this file except in compliance with the License.
   *   You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *   Unless required by applicable law or agreed to in writing, software
  *   distributed under the License is distributed on an "AS IS" BASIS,
  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *   See the License for the specific language governing permissions and
  *   limitations under the License.
  */
 package com.datastax.driver.core;
 
 
 
 
 abstract class AbstractReconnectionHandler implements Runnable {
 
     private static final Logger logger = LoggerFactory.getLogger(AbstractReconnectionHandler.class);
 
     private final ScheduledExecutorService executor;
     private final AtomicReference<ScheduledFuture<?>> currentAttempt;
 
     private volatile boolean readyForNext;
     private volatile ScheduledFuture<?> localFuture;
 
         this. = executor;
         this. = schedule;
         this. = currentAttempt;
     }
 
     protected abstract Connection tryReconnect() throws ConnectionExceptionInterruptedException;
     protected abstract void onReconnection(Connection connection);
 
     protected boolean onConnectionException(ConnectionException elong nextDelayMs) { return true; }
     protected boolean onUnknownException(Exception elong nextDelayMs) { return true; }
 
     // Retrying on authentication error is unlikely to work
     protected boolean onAuthenticationException(AuthenticationException elong nextDelayMs) { return false; }
 
     public void start() {
         long firstDelay = .nextDelayMs();
         .debug("First reconnection scheduled in {}ms"firstDelay);
         try {
              = .schedule(thisfirstDelay.);
 
             // If there a previous task, cancel it, so only one reconnection handler runs.
             while (true) {
                 ScheduledFuture<?> previous = .get();
                 if (.compareAndSet(previous)) {
                     if (previous != null)
                         previous.cancel(false);
                     break;
                 }
             }
              = true;
         } catch (RejectedExecutionException e) {
             // The executor has been shutdown, fair enough, just ignore
             .debug("Aborting reconnection handling since the cluster is shutting down");
         }
     }
 
     @Override
     public void run() {
         // We shouldn't arrive here if the future is cancelled but better safe than sorry
         if (.isCancelled())
             return;
 
         // Don't run before ready, otherwise our cancel business might end up removing all connection attempts.
         while (!)
             Uninterruptibles.sleepUninterruptibly(5, .);
 
         try {
             onReconnection(tryReconnect());
             .compareAndSet(null);
         } catch (ConnectionException e) {
             long nextDelay = .nextDelayMs();
             if (onConnectionException(enextDelay))
                 reschedule(nextDelay);
             else
                 .compareAndSet(null);
         } catch (AuthenticationException e) {
             .error(e.getMessage());
            long nextDelay = .nextDelayMs();
            if (onAuthenticationException(enextDelay)) {
                reschedule(nextDelay);
            } else {
                .error("Retry against {} have been suspended. It won't be retried unless the node is restarted."e.getHost());
                .compareAndSet(null);
            }
        } catch (InterruptedException e) {
            // If interrupted, skip this attempt but still skip scheduling reconnections
            Thread.currentThread().interrupt();
            reschedule(.nextDelayMs());
        } catch (Exception e) {
            long nextDelay = .nextDelayMs();
            if (onUnknownException(enextDelay))
                reschedule(nextDelay);
            else
                .compareAndSet(null);
        }
    }
    private void reschedule(long nextDelay) {
         = false;
        ScheduledFuture<?> newFuture = .schedule(thisnextDelay.);
        assert  != null;
        // If it's not our future the current one, then we've been canceled
        if (!.compareAndSet(newFuture)) {
            newFuture.cancel(false);
        }
         = newFuture;
         = true;
    }
New to GrepCode? Check out our FAQ X