Java Code Examples for org.springframework.util.backoff.BackOffExecution#nextBackOff()

The following examples show how to use org.springframework.util.backoff.BackOffExecution#nextBackOff() . 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: DefaultMessageListenerContainer.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Apply the next back-off time using the specified {@link BackOffExecution}.
 * <p>Return {@code true} if the back-off period has been applied and a new
 * attempt to recover should be made, {@code false} if no further attempt
 * should be made.
 * @since 4.1
 */
protected boolean applyBackOffTime(BackOffExecution execution) {
	if (this.recovering && this.interrupted) {
		// Interrupted right before and still failing... give up.
		return false;
	}
	long interval = execution.nextBackOff();
	if (interval == BackOffExecution.STOP) {
		return false;
	}
	else {
		try {
			synchronized (this.lifecycleMonitor) {
				this.lifecycleMonitor.wait(interval);
			}
		}
		catch (InterruptedException interEx) {
			// Re-interrupt current thread, to allow other threads to react.
			Thread.currentThread().interrupt();
			if (this.recovering) {
				this.interrupted = true;
			}
		}
		return true;
	}
}
 
Example 2
Source File: DefaultMessageListenerContainer.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Apply the next back-off time using the specified {@link BackOffExecution}.
 * <p>Return {@code true} if the back-off period has been applied and a new
 * attempt to recover should be made, {@code false} if no further attempt
 * should be made.
 * @since 4.1
 */
protected boolean applyBackOffTime(BackOffExecution execution) {
	if (this.recovering && this.interrupted) {
		// Interrupted right before and still failing... give up.
		return false;
	}
	long interval = execution.nextBackOff();
	if (interval == BackOffExecution.STOP) {
		return false;
	}
	else {
		try {
			synchronized (this.lifecycleMonitor) {
				this.lifecycleMonitor.wait(interval);
			}
		}
		catch (InterruptedException interEx) {
			// Re-interrupt current thread, to allow other threads to react.
			Thread.currentThread().interrupt();
			if (this.recovering) {
				this.interrupted = true;
			}
		}
		return true;
	}
}
 
Example 3
Source File: DefaultMessageListenerContainer.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Apply the next back off time using the specified {@link BackOffExecution}.
 * <p>Return {@code true} if the back off period has been applied and a new
 * attempt to recover should be made, {@code false} if no further attempt
 * should be made.
 */
protected boolean applyBackOffTime(BackOffExecution execution) {
	long interval = execution.nextBackOff();
	if (interval == BackOffExecution.STOP) {
		return false;
	}
	else {
		try {
			Thread.sleep(interval);
		}
		catch (InterruptedException interEx) {
			// Re-interrupt current thread, to allow other threads to react.
			Thread.currentThread().interrupt();
		}
	}
	return true;
}
 
Example 4
Source File: FiatPermissionEvaluator.java    From fiat with Apache License 2.0 6 votes vote down vote up
public <T> T retry(String description, Callable<T> callable) throws Exception {
  ExponentialBackOff backOff =
      new ExponentialBackOff(
          retryConfiguration.getInitialBackoffMillis(),
          retryConfiguration.getRetryMultiplier());
  backOff.setMaxElapsedTime(retryConfiguration.getMaxBackoffMillis());
  BackOffExecution backOffExec = backOff.start();
  while (true) {
    try {
      return callable.call();
    } catch (Throwable e) {
      long waitTime = backOffExec.nextBackOff();
      if (waitTime == BackOffExecution.STOP) {
        throw e;
      }
      log.warn(description + " failed. Retrying in " + waitTime + "ms", e);
      TimeUnit.MILLISECONDS.sleep(waitTime);
    }
  }
}
 
Example 5
Source File: SelfKeymasterServiceOps.java    From syncope with Apache License 2.0 6 votes vote down vote up
private void handleRetry(
        final NetworkService service,
        final Action action,
        final int retries,
        final BackOffExecution backOffExecution) {

    try {
        if (action == Action.register && retries > 0) {
            long nextBackoff = backOffExecution.nextBackOff();

            LOG.debug("Still {} retries available for {}; waiting for {} ms", retries, service, nextBackoff);
            try {
                Thread.sleep(nextBackoff);
            } catch (InterruptedException e) {
                // ignore
            }

            retry(completionStage(action, service), service, action, retries - 1, backOffExecution);
        } else {
            LOG.debug("No more retries {} for {}", action, service);
        }
    } catch (Throwable t) {
        LOG.error("Could not continue {} for {}, aborting", action, service, t);
    }
}
 
Example 6
Source File: ExponentialBackOffTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void toStringContent() {
	ExponentialBackOff backOff = new ExponentialBackOff(2000L, 2.0);
	BackOffExecution execution = backOff.start();
	assertEquals("ExponentialBackOff{currentInterval=n/a, multiplier=2.0}", execution.toString());
	execution.nextBackOff();
	assertEquals("ExponentialBackOff{currentInterval=2000ms, multiplier=2.0}", execution.toString());
	execution.nextBackOff();
	assertEquals("ExponentialBackOff{currentInterval=4000ms, multiplier=2.0}", execution.toString());
}
 
Example 7
Source File: FixedBackOffTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void toStringContent() {
	FixedBackOff backOff = new FixedBackOff(200L, 10);
	BackOffExecution execution = backOff.start();
	assertEquals("FixedBackOff{interval=200, currentAttempts=0, maxAttempts=10}", execution.toString());
	execution.nextBackOff();
	assertEquals("FixedBackOff{interval=200, currentAttempts=1, maxAttempts=10}", execution.toString());
	execution.nextBackOff();
	assertEquals("FixedBackOff{interval=200, currentAttempts=2, maxAttempts=10}", execution.toString());
}
 
Example 8
Source File: ExponentialBackOffTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void toStringContent() {
	ExponentialBackOff backOff = new ExponentialBackOff(2000L, 2.0);
	BackOffExecution execution = backOff.start();
	assertEquals("ExponentialBackOff{currentInterval=n/a, multiplier=2.0}", execution.toString());
	execution.nextBackOff();
	assertEquals("ExponentialBackOff{currentInterval=2000ms, multiplier=2.0}", execution.toString());
	execution.nextBackOff();
	assertEquals("ExponentialBackOff{currentInterval=4000ms, multiplier=2.0}", execution.toString());
}
 
Example 9
Source File: FixedBackOffTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void toStringContent() {
	FixedBackOff backOff = new FixedBackOff(200L, 10);
	BackOffExecution execution = backOff.start();
	assertEquals("FixedBackOff{interval=200, currentAttempts=0, maxAttempts=10}", execution.toString());
	execution.nextBackOff();
	assertEquals("FixedBackOff{interval=200, currentAttempts=1, maxAttempts=10}", execution.toString());
	execution.nextBackOff();
	assertEquals("FixedBackOff{interval=200, currentAttempts=2, maxAttempts=10}", execution.toString());
}
 
Example 10
Source File: ExponentialBackOffTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void toStringContent() {
	ExponentialBackOff backOff = new ExponentialBackOff(2000L, 2.0);
	BackOffExecution execution = backOff.start();
	assertEquals("ExponentialBackOff{currentInterval=n/a, multiplier=2.0}", execution.toString());
	execution.nextBackOff();
	assertEquals("ExponentialBackOff{currentInterval=2000ms, multiplier=2.0}", execution.toString());
	execution.nextBackOff();
	assertEquals("ExponentialBackOff{currentInterval=4000ms, multiplier=2.0}", execution.toString());
}
 
Example 11
Source File: FixedBackOffTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void toStringContent() {
	FixedBackOff backOff = new FixedBackOff(200L, 10);
	BackOffExecution execution = backOff.start();
	assertEquals("FixedBackOff{interval=200, currentAttempts=0, maxAttempts=10}", execution.toString());
	execution.nextBackOff();
	assertEquals("FixedBackOff{interval=200, currentAttempts=1, maxAttempts=10}", execution.toString());
	execution.nextBackOff();
	assertEquals("FixedBackOff{interval=200, currentAttempts=2, maxAttempts=10}", execution.toString());
}
 
Example 12
Source File: RetrofitConfig.java    From fiat with Apache License 2.0 5 votes vote down vote up
@Override
public Response intercept(Chain chain) throws IOException {
  ExponentialBackOff backoff = new ExponentialBackOff();
  backoff.setMaxElapsedTime(maxElapsedBackoffMs);
  BackOffExecution backOffExec = backoff.start();

  Response response = null;
  long waitTime = 0;
  while (waitTime != BackOffExecution.STOP) {
    Request request = chain.request();
    response = chain.proceed(request);
    if (response.isSuccessful()
        || NON_RETRYABLE_METHODS.contains(request.method())
        || response.code() == 404) {
      return response;
    }

    try {
      waitTime = backOffExec.nextBackOff();
      if (waitTime != BackOffExecution.STOP) {
        if (response.body() != null) {
          response.body().close();
        }
        log.warn(
            "Request for "
                + request.url().toString()
                + " failed. Backing off for "
                + waitTime
                + "ms");
        Thread.sleep(waitTime);
      }
    } catch (Throwable ignored) {
      break;
    }
  }
  return response;
}
 
Example 13
Source File: RetryCommand.java    From mojito with Apache License 2.0 5 votes vote down vote up
@Override
public void execute() throws CommandException {

    List<String> commandToExecute = getCommandToExecute();

    ExponentialBackOff exponentialBackOff = new ExponentialBackOff(initialInterval, multiplier);
    BackOffExecution backOffExecution = exponentialBackOff.start();

    int numberOfAttempts = 0;
    int exitCode = -1;

    while (exitCode !=0 && numberOfAttempts < maxNumberOfAttempts) {
        try {
            numberOfAttempts++;
            exitCode = executeCommand(commandToExecute);

            if (exitCode != 0) {
                consoleWriter.a("Attempt: ").fg(Ansi.Color.CYAN)
                        .a(numberOfAttempts).a("/").a(maxNumberOfAttempts).reset()
                        .a(" failed. ");
                        
                if (numberOfAttempts < maxNumberOfAttempts) {
                    long nextBackOff = backOffExecution.nextBackOff();
                    consoleWriter.a("Retry in ").fg(Ansi.Color.CYAN).a(nextBackOff / 1000.0).reset().a(" seconds").println();
                    Thread.sleep(nextBackOff);
                } else {
                    consoleWriter.a("Abort").println();
                }
            }
        } catch (CommandException ce) {
            throw ce;
        } catch (Throwable t) {
            throw new CommandException("Couldn't run command with retry", t);
        }
    }

    if (exitCode != 0) {
        throw new CommandWithExitStatusException(exitCode);
    }
}
 
Example 14
Source File: UserRolesSyncer.java    From fiat with Apache License 2.0 4 votes vote down vote up
public long syncAndReturn(List<String> roles) {
  FixedBackOff backoff = new FixedBackOff();
  backoff.setInterval(retryIntervalMs);
  backoff.setMaxAttempts(Math.floorDiv(syncDelayTimeoutMs, retryIntervalMs) + 1);
  BackOffExecution backOffExec = backoff.start();

  // after this point the execution will get rescheduled
  final long timeout = System.currentTimeMillis() + syncDelayTimeoutMs;

  if (!isServerHealthy()) {
    log.warn(
        "Server is currently UNHEALTHY. User permission role synchronization and "
            + "resolution may not complete until this server becomes healthy again.");
  }

  // Ensure we're going to reload app and service account definitions
  permissionsResolver.clearCache();

  while (true) {
    try {
      Map<String, UserPermission> combo = new HashMap<>();
      // force a refresh of the unrestricted user in case the backing repository is empty:
      combo.put(UnrestrictedResourceConfig.UNRESTRICTED_USERNAME, new UserPermission());
      Map<String, UserPermission> temp;
      if (!(temp = getUserPermissions(roles)).isEmpty()) {
        combo.putAll(temp);
      }
      if (!(temp = getServiceAccountsAsMap(roles)).isEmpty()) {
        combo.putAll(temp);
      }

      return updateUserPermissions(combo);
    } catch (ProviderException | PermissionResolutionException ex) {
      registry
          .counter(metricName("syncFailure"), "cause", ex.getClass().getSimpleName())
          .increment();
      Status status = healthIndicator.health().getStatus();
      long waitTime = backOffExec.nextBackOff();
      if (waitTime == BackOffExecution.STOP || System.currentTimeMillis() > timeout) {
        String cause = (waitTime == BackOffExecution.STOP) ? "backoff-exhausted" : "timeout";
        registry.counter("syncAborted", "cause", cause).increment();
        log.error("Unable to resolve service account permissions.", ex);
        return 0;
      }
      String message =
          new StringBuilder("User permission sync failed. ")
              .append("Server status is ")
              .append(status)
              .append(". Trying again in ")
              .append(waitTime)
              .append(" ms. Cause:")
              .append(ex.getMessage())
              .toString();
      if (log.isDebugEnabled()) {
        log.debug(message, ex);
      } else {
        log.warn(message);
      }

      try {
        Thread.sleep(waitTime);
      } catch (InterruptedException ignored) {
      }
    } finally {
      isServerHealthy();
    }
  }
}