org.springframework.util.backoff.BackOffExecution Java Examples

The following examples show how to use org.springframework.util.backoff.BackOffExecution. 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: ExponentialBackOffTests.java    From spring-analysis-note with 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
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 #4
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 #5
Source File: SelfKeymasterServiceOps.java    From syncope with Apache License 2.0 6 votes vote down vote up
private void retry(
        final CompletionStage<Response> completionStage,
        final NetworkService service,
        final Action action,
        final int retries,
        final BackOffExecution backOffExecution) {

    completionStage.whenComplete((response, throwable) -> {
        if (throwable == null && response.getStatus() < 300) {
            LOG.info("{} successfully " + action + "ed", service);
        } else {
            LOG.error("Could not " + action + " {}", service, throwable);

            handleRetry(service, action, retries, backOffExecution);
        }
    }).exceptionally(throwable -> {
        LOG.error("Could not " + action + " {}", service, throwable);

        handleRetry(service, action, retries, backOffExecution);

        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    });
}
 
Example #6
Source File: DefaultMessageListenerContainerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void applyBackOff() {
	BackOff mock = mock(BackOff.class);
	BackOffExecution execution = mock(BackOffExecution.class);
	given(execution.nextBackOff()).willReturn(BackOffExecution.STOP);
	given(mock.start()).willReturn(execution);

	DefaultMessageListenerContainer container = createContainer(mock, createFailingContainerFactory());
	container.start();
	assertEquals(true, container.isRunning());

	container.refreshConnectionUntilSuccessful();

	assertEquals(false, container.isRunning());
	verify(mock).start();
	verify(execution).nextBackOff();
}
 
Example #7
Source File: DefaultMessageListenerContainerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void applyBackOff() {
	BackOff backOff = mock(BackOff.class);
	BackOffExecution execution = mock(BackOffExecution.class);
	given(execution.nextBackOff()).willReturn(BackOffExecution.STOP);
	given(backOff.start()).willReturn(execution);

	DefaultMessageListenerContainer container = createContainer(createFailingContainerFactory());
	container.setBackOff(backOff);
	container.start();
	assertEquals(true, container.isRunning());

	container.refreshConnectionUntilSuccessful();

	assertEquals(false, container.isRunning());
	verify(backOff).start();
	verify(execution).nextBackOff();
}
 
Example #8
Source File: DefaultMessageListenerContainerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void applyBackOffRetry() {
	BackOff backOff = mock(BackOff.class);
	BackOffExecution execution = mock(BackOffExecution.class);
	given(execution.nextBackOff()).willReturn(50L, BackOffExecution.STOP);
	given(backOff.start()).willReturn(execution);

	DefaultMessageListenerContainer container = createContainer(createFailingContainerFactory());
	container.setBackOff(backOff);
	container.start();
	container.refreshConnectionUntilSuccessful();

	assertEquals(false, container.isRunning());
	verify(backOff).start();
	verify(execution, times(2)).nextBackOff();
}
 
Example #9
Source File: DefaultMessageListenerContainerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void recoverResetBackOff() {
	BackOff backOff = mock(BackOff.class);
	BackOffExecution execution = mock(BackOffExecution.class);
	given(execution.nextBackOff()).willReturn(50L, 50L, 50L);  // 3 attempts max
	given(backOff.start()).willReturn(execution);

	DefaultMessageListenerContainer container = createContainer(createRecoverableContainerFactory(1));
	container.setBackOff(backOff);
	container.start();
	container.refreshConnectionUntilSuccessful();

	assertEquals(true, container.isRunning());
	verify(backOff).start();
	verify(execution, times(1)).nextBackOff();  // only on attempt as the second one lead to a recovery
}
 
Example #10
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 #11
Source File: ExponentialBackOffTests.java    From java-technology-stack with 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 #12
Source File: ExponentialBackOffTests.java    From spring4-understanding with Apache License 2.0 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 #13
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 #14
Source File: DefaultMessageListenerContainerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void applyBackOff() {
	BackOff backOff = mock(BackOff.class);
	BackOffExecution execution = mock(BackOffExecution.class);
	given(execution.nextBackOff()).willReturn(BackOffExecution.STOP);
	given(backOff.start()).willReturn(execution);

	DefaultMessageListenerContainer container = createContainer(createFailingContainerFactory());
	container.setBackOff(backOff);
	container.start();
	assertEquals(true, container.isRunning());

	container.refreshConnectionUntilSuccessful();

	assertEquals(false, container.isRunning());
	verify(backOff).start();
	verify(execution).nextBackOff();
}
 
Example #15
Source File: DefaultMessageListenerContainerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void applyBackOffRetry() {
	BackOff backOff = mock(BackOff.class);
	BackOffExecution execution = mock(BackOffExecution.class);
	given(execution.nextBackOff()).willReturn(50L, BackOffExecution.STOP);
	given(backOff.start()).willReturn(execution);

	DefaultMessageListenerContainer container = createContainer(createFailingContainerFactory());
	container.setBackOff(backOff);
	container.start();
	container.refreshConnectionUntilSuccessful();

	assertEquals(false, container.isRunning());
	verify(backOff).start();
	verify(execution, times(2)).nextBackOff();
}
 
Example #16
Source File: DefaultMessageListenerContainerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void recoverResetBackOff() {
	BackOff backOff = mock(BackOff.class);
	BackOffExecution execution = mock(BackOffExecution.class);
	given(execution.nextBackOff()).willReturn(50L, 50L, 50L);  // 3 attempts max
	given(backOff.start()).willReturn(execution);

	DefaultMessageListenerContainer container = createContainer(createRecoverableContainerFactory(1));
	container.setBackOff(backOff);
	container.start();
	container.refreshConnectionUntilSuccessful();

	assertEquals(true, container.isRunning());
	verify(backOff).start();
	verify(execution, times(1)).nextBackOff();  // only on attempt as the second one lead to a recovery
}
 
Example #17
Source File: RegisterClientHelper.java    From Java-Auto-Update with Apache License 2.0 5 votes vote down vote up
public ClientConfig registerClient() {
    BackOff exponentialBackOff = new ExponentialBackOff();
    BackOffExecution backOffExecution = exponentialBackOff.start();

    while (true) {
        try {
            return new CommandRegisterClient(artifactId, configServiceClient, clientName, clientId).execute();
        } catch (HystrixRuntimeException e) {
            RegisterClientExceptionHandler.handleRegisterClientException(e, exponentialBackOff, backOffExecution,
                    configServiceClient.getUrl());
        }
    }
}
 
Example #18
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 #19
Source File: RegisterClientExceptionHandler.java    From Java-Auto-Update with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example #20
Source File: DefaultMessageListenerContainerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void applyBackOffRetry() {
	BackOff mock = mock(BackOff.class);
	BackOffExecution execution = mock(BackOffExecution.class);
	given(execution.nextBackOff()).willReturn(50L, BackOffExecution.STOP);
	given(mock.start()).willReturn(execution);

	DefaultMessageListenerContainer container = createContainer(mock, createFailingContainerFactory());
	container.start();
	container.refreshConnectionUntilSuccessful();

	assertEquals(false, container.isRunning());
	verify(mock).start();
	verify(execution, times(2)).nextBackOff();
}
 
Example #21
Source File: FixedBackOffTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void defaultInstance() {
	FixedBackOff backOff = new FixedBackOff();
	BackOffExecution execution = backOff.start();
	for (int i = 0; i < 100; i++) {
		assertEquals(FixedBackOff.DEFAULT_INTERVAL, execution.nextBackOff());
	}
}
 
Example #22
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 #23
Source File: ExponentialBackOffTests.java    From spring4-understanding with Apache License 2.0 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 #24
Source File: ExponentialBackOffTests.java    From spring4-understanding with Apache License 2.0 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 #25
Source File: ExponentialBackOffTests.java    From spring4-understanding with Apache License 2.0 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 #26
Source File: ExponentialBackOffTests.java    From spring4-understanding with Apache License 2.0 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 #27
Source File: RegisterClientExceptionHandlerTest.java    From Java-Auto-Update with Apache License 2.0 5 votes vote down vote up
@Test
 public void testShouldHandleNotFoundExceptionWithRetry() {
    BackOffExecution backOffExecution = mock(BackOffExecution.class);
    when(backOffExecution.nextBackOff()).thenReturn((long) 1);

    HystrixRuntimeException hystrixRuntimeException = mock(HystrixRuntimeException.class);
    NotFoundException notFoundException = new NotFoundException();
    when(hystrixRuntimeException.getCause()).thenReturn(notFoundException);

    RegisterClientExceptionHandler.handleRegisterClientException(hystrixRuntimeException, null, backOffExecution, "");

    verify(backOffExecution).nextBackOff();
}
 
Example #28
Source File: ExponentialBackOffTests.java    From spring4-understanding with Apache License 2.0 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 #29
Source File: RegisterClientExceptionHandlerTest.java    From Java-Auto-Update with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldHandleTimeoutExceptionWithRetry() {
    BackOffExecution backOffExecution = mock(BackOffExecution.class);
    when(backOffExecution.nextBackOff()).thenReturn((long) 1);

    HystrixRuntimeException hystrixRuntimeException = mock(HystrixRuntimeException.class);
    when(hystrixRuntimeException.getCause()).thenReturn(new TimeoutException());

    RegisterClientExceptionHandler.handleRegisterClientException(hystrixRuntimeException, null, backOffExecution, "");

    verify(backOffExecution).nextBackOff();
}
 
Example #30
Source File: FixedBackOffTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void maxAttemptsReached() {
	FixedBackOff backOff = new FixedBackOff(200L, 2);
	BackOffExecution execution = backOff.start();
	assertEquals(200l, execution.nextBackOff());
	assertEquals(200l, execution.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution.nextBackOff());
}