Java Code Examples for org.springframework.util.backoff.ExponentialBackOff#setMaxInterval()

The following examples show how to use org.springframework.util.backoff.ExponentialBackOff#setMaxInterval() . 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: ExponentialBackOffTests.java    From spring-analysis-note with 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 3
Source File: ExponentialBackOffTests.java    From spring-analysis-note with 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 4
Source File: ExponentialBackOffTests.java    From java-technology-stack with 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 5
Source File: ExponentialBackOffTests.java    From java-technology-stack with 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 6
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 7
Source File: ExponentialBackOffTests.java    From spring4-understanding with Apache License 2.0 4 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());
}