Java Code Examples for org.springframework.util.backoff.ExponentialBackOff

The following examples show how to use org.springframework.util.backoff.ExponentialBackOff. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: ExponentialBackOffTests.java    License: MIT License 6 votes vote down vote up
@Test
public void startReturnDifferentInstances() {
	ExponentialBackOff backOff = new ExponentialBackOff();
	backOff.setInitialInterval(2000L);
	backOff.setMultiplier(2.0);
	backOff.setMaxElapsedTime(4000L);

	BackOffExecution execution = backOff.start();
	BackOffExecution execution2 = backOff.start();

	assertEquals(2000L, execution.nextBackOff());
	assertEquals(2000L, execution2.nextBackOff());
	assertEquals(4000L, execution.nextBackOff());
	assertEquals(4000L, execution2.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution2.nextBackOff());
}
 
Example 2
Source Project: java-technology-stack   Source File: ExponentialBackOffTests.java    License: MIT License 6 votes vote down vote up
@Test
public void startReturnDifferentInstances() {
	ExponentialBackOff backOff = new ExponentialBackOff();
	backOff.setInitialInterval(2000L);
	backOff.setMultiplier(2.0);
	backOff.setMaxElapsedTime(4000L);

	BackOffExecution execution = backOff.start();
	BackOffExecution execution2 = backOff.start();

	assertEquals(2000L, execution.nextBackOff());
	assertEquals(2000L, execution2.nextBackOff());
	assertEquals(4000L, execution.nextBackOff());
	assertEquals(4000L, execution2.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution2.nextBackOff());
}
 
Example 3
@Test
public void startReturnDifferentInstances() {
	ExponentialBackOff backOff = new ExponentialBackOff();
	backOff.setInitialInterval(2000L);
	backOff.setMultiplier(2.0);
	backOff.setMaxElapsedTime(4000L);

	BackOffExecution execution = backOff.start();
	BackOffExecution execution2 = backOff.start();

	assertEquals(2000l, execution.nextBackOff());
	assertEquals(2000l, execution2.nextBackOff());
	assertEquals(4000l, execution.nextBackOff());
	assertEquals(4000l, execution2.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution2.nextBackOff());
}
 
Example 4
/**
 * Configure a {@link BackOff} for the after rollback processor, based on the consumer
 * retry properties. If retry is disabled, return a {@link BackOff} that disables
 * retry. Otherwise calculate the {@link ExponentialBackOff#setMaxElapsedTime(long)}
 * so that the {@link BackOff} stops after the configured
 * {@link ExtendedConsumerProperties#getMaxAttempts()}.
 * @param extendedConsumerProperties the properties.
 * @return the backoff.
 */
private BackOff createBackOff(
		final ExtendedConsumerProperties<KafkaConsumerProperties> extendedConsumerProperties) {

	int maxAttempts = extendedConsumerProperties.getMaxAttempts();
	if (maxAttempts < 2) {
		return new FixedBackOff(0L, 0L);
	}
	int initialInterval = extendedConsumerProperties.getBackOffInitialInterval();
	double multiplier = extendedConsumerProperties.getBackOffMultiplier();
	int maxInterval = extendedConsumerProperties.getBackOffMaxInterval();
	ExponentialBackOff backOff = new ExponentialBackOff(initialInterval, multiplier);
	backOff.setMaxInterval(maxInterval);
	long maxElapsed = extendedConsumerProperties.getBackOffInitialInterval();
	double accum = maxElapsed;
	for (int i = 1; i < maxAttempts - 1; i++) {
		accum = accum * multiplier;
		if (accum > maxInterval) {
			accum = maxInterval;
		}
		maxElapsed += accum;
	}
	backOff.setMaxElapsedTime(maxElapsed);
	return backOff;
}
 
Example 5
Source Project: fiat   Source File: FiatPermissionEvaluator.java    License: 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 6
Source Project: spring-analysis-note   Source File: ExponentialBackOffTests.java    License: MIT License 5 votes vote down vote up
@Test
public void defaultInstance() {
	ExponentialBackOff backOff = new ExponentialBackOff();
	BackOffExecution execution = backOff.start();
	assertEquals(2000L, execution.nextBackOff());
	assertEquals(3000L, execution.nextBackOff());
	assertEquals(4500L, execution.nextBackOff());
}
 
Example 7
Source Project: spring-analysis-note   Source File: ExponentialBackOffTests.java    License: MIT License 5 votes vote down vote up
@Test
public void simpleIncrease() {
	ExponentialBackOff backOff = new ExponentialBackOff(100L, 2.0);
	BackOffExecution execution = backOff.start();
	assertEquals(100L, execution.nextBackOff());
	assertEquals(200L, execution.nextBackOff());
	assertEquals(400L, execution.nextBackOff());
	assertEquals(800L, execution.nextBackOff());
}
 
Example 8
Source Project: spring-analysis-note   Source File: ExponentialBackOffTests.java    License: MIT License 5 votes vote down vote up
@Test
public void fixedIncrease() {
	ExponentialBackOff backOff = new ExponentialBackOff(100L, 1.0);
	backOff.setMaxElapsedTime(300L);

	BackOffExecution execution = backOff.start();
	assertEquals(100L, execution.nextBackOff());
	assertEquals(100L, execution.nextBackOff());
	assertEquals(100L, execution.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution.nextBackOff());
}
 
Example 9
Source Project: spring-analysis-note   Source File: ExponentialBackOffTests.java    License: MIT License 5 votes vote down vote up
@Test
public void maxIntervalReached() {
	ExponentialBackOff backOff = new ExponentialBackOff(2000L, 2.0);
	backOff.setMaxInterval(4000L);

	BackOffExecution execution = backOff.start();
	assertEquals(2000L, execution.nextBackOff());
	assertEquals(4000L, execution.nextBackOff());
	assertEquals(4000L, execution.nextBackOff()); // max reached
	assertEquals(4000L, execution.nextBackOff());
}
 
Example 10
Source Project: spring-analysis-note   Source File: ExponentialBackOffTests.java    License: MIT License 5 votes vote down vote up
@Test
public void maxAttemptsReached() {
	ExponentialBackOff backOff = new ExponentialBackOff(2000L, 2.0);
	backOff.setMaxElapsedTime(4000L);

	BackOffExecution execution = backOff.start();
	assertEquals(2000L, execution.nextBackOff());
	assertEquals(4000L, execution.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution.nextBackOff()); // > 4 sec wait in total
}
 
Example 11
Source Project: spring-analysis-note   Source File: ExponentialBackOffTests.java    License: MIT License 5 votes vote down vote up
@Test
public void maxIntervalReachedImmediately() {
	ExponentialBackOff backOff = new ExponentialBackOff(1000L, 2.0);
	backOff.setMaxInterval(50L);

	BackOffExecution execution = backOff.start();
	assertEquals(50L, execution.nextBackOff());
	assertEquals(50L, execution.nextBackOff());
}
 
Example 12
Source Project: spring-analysis-note   Source File: ExponentialBackOffTests.java    License: 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 13
Source Project: java-technology-stack   Source File: ExponentialBackOffTests.java    License: MIT License 5 votes vote down vote up
@Test
public void defaultInstance() {
	ExponentialBackOff backOff = new ExponentialBackOff();
	BackOffExecution execution = backOff.start();
	assertEquals(2000L, execution.nextBackOff());
	assertEquals(3000L, execution.nextBackOff());
	assertEquals(4500L, execution.nextBackOff());
}
 
Example 14
Source Project: java-technology-stack   Source File: ExponentialBackOffTests.java    License: MIT License 5 votes vote down vote up
@Test
public void simpleIncrease() {
	ExponentialBackOff backOff = new ExponentialBackOff(100L, 2.0);
	BackOffExecution execution = backOff.start();
	assertEquals(100L, execution.nextBackOff());
	assertEquals(200L, execution.nextBackOff());
	assertEquals(400L, execution.nextBackOff());
	assertEquals(800L, execution.nextBackOff());
}
 
Example 15
Source Project: java-technology-stack   Source File: ExponentialBackOffTests.java    License: MIT License 5 votes vote down vote up
@Test
public void fixedIncrease() {
	ExponentialBackOff backOff = new ExponentialBackOff(100L, 1.0);
	backOff.setMaxElapsedTime(300L);

	BackOffExecution execution = backOff.start();
	assertEquals(100L, execution.nextBackOff());
	assertEquals(100L, execution.nextBackOff());
	assertEquals(100L, execution.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution.nextBackOff());
}
 
Example 16
Source Project: java-technology-stack   Source File: ExponentialBackOffTests.java    License: MIT License 5 votes vote down vote up
@Test
public void maxIntervalReached() {
	ExponentialBackOff backOff = new ExponentialBackOff(2000L, 2.0);
	backOff.setMaxInterval(4000L);

	BackOffExecution execution = backOff.start();
	assertEquals(2000L, execution.nextBackOff());
	assertEquals(4000L, execution.nextBackOff());
	assertEquals(4000L, execution.nextBackOff()); // max reached
	assertEquals(4000L, execution.nextBackOff());
}
 
Example 17
Source Project: java-technology-stack   Source File: ExponentialBackOffTests.java    License: MIT License 5 votes vote down vote up
@Test
public void maxAttemptsReached() {
	ExponentialBackOff backOff = new ExponentialBackOff(2000L, 2.0);
	backOff.setMaxElapsedTime(4000L);

	BackOffExecution execution = backOff.start();
	assertEquals(2000L, execution.nextBackOff());
	assertEquals(4000L, execution.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution.nextBackOff()); // > 4 sec wait in total
}
 
Example 18
Source Project: java-technology-stack   Source File: ExponentialBackOffTests.java    License: MIT License 5 votes vote down vote up
@Test
public void invalidInterval() {
	ExponentialBackOff backOff = new ExponentialBackOff();

	thrown.expect(IllegalArgumentException.class);
	backOff.setMultiplier(0.9);
}
 
Example 19
Source Project: java-technology-stack   Source File: ExponentialBackOffTests.java    License: MIT License 5 votes vote down vote up
@Test
public void maxIntervalReachedImmediately() {
	ExponentialBackOff backOff = new ExponentialBackOff(1000L, 2.0);
	backOff.setMaxInterval(50L);

	BackOffExecution execution = backOff.start();
	assertEquals(50L, execution.nextBackOff());
	assertEquals(50L, execution.nextBackOff());
}
 
Example 20
Source Project: java-technology-stack   Source File: ExponentialBackOffTests.java    License: 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 21
@Test
public void defaultInstance() {
	ExponentialBackOff backOff = new ExponentialBackOff();
	BackOffExecution execution = backOff.start();
	assertEquals(2000l, execution.nextBackOff());
	assertEquals(3000l, execution.nextBackOff());
	assertEquals(4500l, execution.nextBackOff());
}
 
Example 22
@Test
public void simpleIncrease() {
	ExponentialBackOff backOff = new ExponentialBackOff(100L, 2.0);
	BackOffExecution execution = backOff.start();
	assertEquals(100l, execution.nextBackOff());
	assertEquals(200l, execution.nextBackOff());
	assertEquals(400l, execution.nextBackOff());
	assertEquals(800l, execution.nextBackOff());
}
 
Example 23
@Test
public void fixedIncrease() {
	ExponentialBackOff backOff = new ExponentialBackOff(100L, 1.0);
	backOff.setMaxElapsedTime(300l);

	BackOffExecution execution = backOff.start();
	assertEquals(100l, execution.nextBackOff());
	assertEquals(100l, execution.nextBackOff());
	assertEquals(100l, execution.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution.nextBackOff());
}
 
Example 24
@Test
public void maxIntervalReached() {
	ExponentialBackOff backOff = new ExponentialBackOff(2000L, 2.0);
	backOff.setMaxInterval(4000L);

	BackOffExecution execution = backOff.start();
	assertEquals(2000l, execution.nextBackOff());
	assertEquals(4000l, execution.nextBackOff());
	assertEquals(4000l, execution.nextBackOff()); // max reached
	assertEquals(4000l, execution.nextBackOff());
}
 
Example 25
@Test
public void maxAttemptsReached() {
	ExponentialBackOff backOff = new ExponentialBackOff(2000L, 2.0);
	backOff.setMaxElapsedTime(4000L);

	BackOffExecution execution = backOff.start();
	assertEquals(2000l, execution.nextBackOff());
	assertEquals(4000l, execution.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution.nextBackOff()); // > 4 sec wait in total
}
 
Example 26
@Test
public void invalidInterval() {
	ExponentialBackOff backOff = new ExponentialBackOff();

	thrown.expect(IllegalArgumentException.class);
	backOff.setMultiplier(0.9);
}
 
Example 27
@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 28
Source Project: fiat   Source File: RetrofitConfig.java    License: 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 29
Source Project: mojito   Source File: RetryCommand.java    License: 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 30
public static void handleRegisterClientException(HystrixRuntimeException e, BackOff exponentialBackOff,
                                                 BackOffExecution backOffExecution, String configServiceUrl) {
    Throwable cause = e.getCause();
    log.debug("Exception registering client, exception getMessage={}", e.getMessage());
    log.debug("Exception registering client, cause getMessage={}", cause.getMessage());

    if (cause instanceof ConnectException) {
        log.debug("Connection refused to ConfigService url={}", configServiceUrl);
    } else if (cause instanceof InternalServerErrorException) {
        log.debug("Internal server error in ConfigService url={}", configServiceUrl);
    } else if(cause instanceof NotFoundException) {
        log.debug("404 not found to ConfigService url={}", configServiceUrl);
    } else if (cause instanceof BadRequestException) {
        log.error("400 Bad Request. Probably need to fix something on the client. Exiting after a" +
                " wait, so as to not DDoS the server.");

        // TODO  Do a sensible BackOff implementation class comparissmnet before this!!!  
        SleepUtil.sleepWithLogging(((ExponentialBackOff)exponentialBackOff).getMaxInterval() * 2);
        System.exit(1);
    } else if (cause instanceof TimeoutException) {
        log.debug("CommandRegisterClient timed out.");
    } else {
        log.error("Couldn't handle exception: {}", e);
    }

    SleepUtil.sleepWithLogging(backOffExecution.nextBackOff());
}