org.springframework.util.backoff.FixedBackOff Java Examples

The following examples show how to use org.springframework.util.backoff.FixedBackOff. 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: KafkaMessageChannelBinder.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #2
Source File: FixedBackOffTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void startReturnDifferentInstances() {
	FixedBackOff backOff = new FixedBackOff(100L, 1);
	BackOffExecution execution = backOff.start();
	BackOffExecution execution2 = backOff.start();

	assertEquals(100L, execution.nextBackOff());
	assertEquals(100L, execution2.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution2.nextBackOff());
}
 
Example #3
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 #4
Source File: FixedBackOffTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void liveUpdate() {
	FixedBackOff backOff = new FixedBackOff(100L, 1);
	BackOffExecution execution = backOff.start();
	assertEquals(100l, execution.nextBackOff());

	backOff.setInterval(200l);
	backOff.setMaxAttempts(2);

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

	assertEquals(100l, execution.nextBackOff());
	assertEquals(100l, execution2.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution2.nextBackOff());
}
 
Example #6
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());
}
 
Example #7
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 #8
Source File: JmsListenerContainerFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void backOffOverridesRecoveryInterval() {
	DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
	BackOff backOff = new FixedBackOff();
	factory.setBackOff(backOff);
	factory.setRecoveryInterval(2000L);

	SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
	MessageListener messageListener = new MessageListenerAdapter();
	endpoint.setMessageListener(messageListener);
	endpoint.setDestination("myQueue");
	DefaultMessageListenerContainer container = factory.createListenerContainer(endpoint);

	assertSame(backOff, new DirectFieldAccessor(container).getPropertyValue("backOff"));
}
 
Example #9
Source File: JmsListenerContainerFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void backOffOverridesRecoveryInterval() {
	DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
	BackOff backOff = new FixedBackOff();
	factory.setBackOff(backOff);
	factory.setRecoveryInterval(2000L);

	SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
	MessageListener messageListener = new MessageListenerAdapter();
	endpoint.setMessageListener(messageListener);
	endpoint.setDestination("myQueue");
	DefaultMessageListenerContainer container = factory.createListenerContainer(endpoint);

	assertSame(backOff, new DirectFieldAccessor(container).getPropertyValue("backOff"));
}
 
Example #10
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 #11
Source File: FixedBackOffTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void liveUpdate() {
	FixedBackOff backOff = new FixedBackOff(100L, 1);
	BackOffExecution execution = backOff.start();
	assertEquals(100L, execution.nextBackOff());

	backOff.setInterval(200L);
	backOff.setMaxAttempts(2);

	assertEquals(200L, execution.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution.nextBackOff());
}
 
Example #12
Source File: FixedBackOffTests.java    From java-technology-stack with MIT License 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());
}
 
Example #13
Source File: ConsumerProducerTransactionTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@Bean
public ListenerContainerCustomizer<AbstractMessageListenerContainer<?, ?>> customizer() {
	return (container, dest, group) -> {
		container.setAfterRollbackProcessor(new DefaultAfterRollbackProcessor<>(new FixedBackOff(0L, 1L)));
		if ("input2".equals(dest)) {
			this.input2Container = container;
		}
	};
}
 
Example #14
Source File: FixedBackOffTests.java    From java-technology-stack with MIT License 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 #15
Source File: JmsListenerContainerFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void backOffOverridesRecoveryInterval() {
	DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
	BackOff backOff = new FixedBackOff();
	factory.setBackOff(backOff);
	factory.setRecoveryInterval(2000L);

	SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
	MessageListener messageListener = new MessageListenerAdapter();
	endpoint.setMessageListener(messageListener);
	endpoint.setDestination("myQueue");
	DefaultMessageListenerContainer container = factory.createListenerContainer(endpoint);

	assertSame(backOff, new DirectFieldAccessor(container).getPropertyValue("backOff"));
}
 
Example #16
Source File: ProcessorApplication.java    From spring-cloud-stream-samples with Apache License 2.0 5 votes vote down vote up
@Bean
public ListenerContainerCustomizer<AbstractMessageListenerContainer<byte[], byte[]>> customizer() {
    // Disable retry in the AfterRollbackProcessor
    return (container, destination, group) -> container.setAfterRollbackProcessor(
            new DefaultAfterRollbackProcessor<byte[], byte[]>(
                    (record, exception) -> System.out.println("Discarding failed record: " + record),
                    new FixedBackOff(0L, 0)));
}
 
Example #17
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 #18
Source File: FixedBackOffTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void liveUpdate() {
	FixedBackOff backOff = new FixedBackOff(100L, 1);
	BackOffExecution execution = backOff.start();
	assertEquals(100L, execution.nextBackOff());

	backOff.setInterval(200L);
	backOff.setMaxAttempts(2);

	assertEquals(200L, execution.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution.nextBackOff());
}
 
Example #19
Source File: FixedBackOffTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void startReturnDifferentInstances() {
	FixedBackOff backOff = new FixedBackOff(100L, 1);
	BackOffExecution execution = backOff.start();
	BackOffExecution execution2 = backOff.start();

	assertEquals(100L, execution.nextBackOff());
	assertEquals(100L, execution2.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution.nextBackOff());
	assertEquals(BackOffExecution.STOP, execution2.nextBackOff());
}
 
Example #20
Source File: FixedBackOffTests.java    From spring-analysis-note with MIT License 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());
}
 
Example #21
Source File: FixedBackOffTests.java    From spring-analysis-note with MIT License 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: FixedBackOffTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void noAttemptAtAll() {
	FixedBackOff backOff = new FixedBackOff(100L, 0L);
	BackOffExecution execution = backOff.start();
	assertEquals(BackOffExecution.STOP, execution.nextBackOff());
}
 
Example #23
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();
    }
  }
}
 
Example #24
Source File: JmsNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private long getRecoveryInterval(String containerBeanName) {
	BackOff backOff = getBackOff(containerBeanName);
	assertEquals(FixedBackOff.class, backOff.getClass());
	return ((FixedBackOff)backOff).getInterval();
}
 
Example #25
Source File: DefaultMessageListenerContainer.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private FixedBackOff createDefaultBackOff(long interval) {
	return new FixedBackOff(interval, Long.MAX_VALUE);
}
 
Example #26
Source File: FixedBackOffTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void noAttemptAtAll() {
	FixedBackOff backOff = new FixedBackOff(100L, 0L);
	BackOffExecution execution = backOff.start();
	assertEquals(BackOffExecution.STOP, execution.nextBackOff());
}
 
Example #27
Source File: JmsNamespaceHandlerTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private long getRecoveryInterval(String containerBeanName) {
	BackOff backOff = getBackOff(containerBeanName);
	assertEquals(FixedBackOff.class, backOff.getClass());
	return ((FixedBackOff)backOff).getInterval();
}
 
Example #28
Source File: FixedBackOffTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void noAttemptAtAll() {
	FixedBackOff backOff = new FixedBackOff(100L, 0L);
	BackOffExecution execution = backOff.start();
	assertEquals(BackOffExecution.STOP, execution.nextBackOff());
}
 
Example #29
Source File: JmsNamespaceHandlerTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private long getRecoveryInterval(String containerBeanName) {
	BackOff backOff = getBackOff(containerBeanName);
	assertEquals(FixedBackOff.class, backOff.getClass());
	return ((FixedBackOff)backOff).getInterval();
}
 
Example #30
Source File: DefaultMessageListenerContainer.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Specify the interval between recovery attempts, in <b>milliseconds</b>.
 * The default is 5000 ms, that is, 5 seconds. This is a convenience method
 * to create a {@link FixedBackOff} with the specified interval.
 * <p>For more recovery options, consider specifying a {@link BackOff}
 * instance instead.
 * @see #setBackOff(BackOff)
 * @see #handleListenerSetupFailure
 */
public void setRecoveryInterval(long recoveryInterval) {
	this.backOff = new FixedBackOff(recoveryInterval, Long.MAX_VALUE);
}