org.springframework.util.backoff.BackOff Java Examples

The following examples show how to use org.springframework.util.backoff.BackOff. 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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
Source File: DefaultMessageListenerContainerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void recoverResetBackOff() {
	BackOff mock = mock(BackOff.class);
	BackOffExecution execution = mock(BackOffExecution.class);
	given(execution.nextBackOff()).willReturn(50L, 50L, 50L); // 3 attempts max
	given(mock.start()).willReturn(execution);

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

	assertEquals(true, container.isRunning());
	verify(mock).start();
	verify(execution, times(1)).nextBackOff(); // only on attempt as the second one lead to a recovery
}
 
Example #12
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 #13
Source File: JmsNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testRecoveryInterval() {
	Object testBackOff = context.getBean("testBackOff");
	BackOff backOff1 = getBackOff("listener1");
	BackOff backOff2 = getBackOff("listener2");
	long recoveryInterval3 = getRecoveryInterval(DefaultMessageListenerContainer.class.getName() + "#0");

	assertSame(testBackOff, backOff1);
	assertSame(testBackOff, backOff2);
	assertEquals(DefaultMessageListenerContainer.DEFAULT_RECOVERY_INTERVAL, recoveryInterval3);
}
 
Example #14
Source File: JmsNamespaceHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testRecoveryInterval() {
	Object testBackOff = context.getBean("testBackOff");
	BackOff backOff1 = getBackOff("listener1");
	BackOff backOff2 = getBackOff("listener2");
	long recoveryInterval3 = getRecoveryInterval(DefaultMessageListenerContainer.class.getName() + "#0");

	assertSame(testBackOff, backOff1);
	assertSame(testBackOff, backOff2);
	assertEquals(DefaultMessageListenerContainer.DEFAULT_RECOVERY_INTERVAL, recoveryInterval3);
}
 
Example #15
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 #16
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 #17
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 #18
Source File: JmsNamespaceHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testRecoveryInterval() {
	Object testBackOff = context.getBean("testBackOff");
	BackOff backOff1 = getBackOff("listener1");
	BackOff backOff2 = getBackOff("listener2");
	long recoveryInterval3 = getRecoveryInterval(DefaultMessageListenerContainer.class.getName() + "#0");

	assertSame(testBackOff, backOff1);
	assertSame(testBackOff, backOff2);
	assertEquals(DefaultMessageListenerContainer.DEFAULT_RECOVERY_INTERVAL, recoveryInterval3);
}
 
Example #19
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 #20
Source File: JmsNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private BackOff getBackOff(String containerBeanName) {
	DefaultMessageListenerContainer container = this.context.getBean(containerBeanName, DefaultMessageListenerContainer.class);
	return (BackOff) new DirectFieldAccessor(container).getPropertyValue("backOff");
}
 
Example #21
Source File: JmsNamespaceHandlerTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private BackOff getBackOff(String containerBeanName) {
	DefaultMessageListenerContainer container = this.context.getBean(containerBeanName, DefaultMessageListenerContainer.class);
	return (BackOff) new DirectFieldAccessor(container).getPropertyValue("backOff");
}
 
Example #22
Source File: DefaultJmsListenerContainerFactory.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * @see DefaultMessageListenerContainer#setBackOff
 */
public void setBackOff(BackOff backOff) {
	this.backOff = backOff;
}
 
Example #23
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 #24
Source File: DefaultJmsListenerContainerFactory.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * @see DefaultMessageListenerContainer#setBackOff
 */
public void setBackOff(BackOff backOff) {
	this.backOff = backOff;
}
 
Example #25
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 #26
Source File: JmsNamespaceHandlerTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private BackOff getBackOff(String containerBeanName) {
	DefaultMessageListenerContainer container = this.context.getBean(containerBeanName, DefaultMessageListenerContainer.class);
	return (BackOff) new DirectFieldAccessor(container).getPropertyValue("backOff");
}
 
Example #27
Source File: DefaultJmsListenerContainerFactory.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * @see DefaultMessageListenerContainer#setBackOff
 */
public void setBackOff(BackOff backOff) {
	this.backOff = backOff;
}
 
Example #28
Source File: DefaultMessageListenerContainerTests.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
private DefaultMessageListenerContainer createContainer(BackOff backOff, ConnectionFactory connectionFactory) {

		Destination destination = new Destination() {};


		DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
		container.setConnectionFactory(connectionFactory);
		container.setCacheLevel(DefaultMessageListenerContainer.CACHE_NONE);
		container.setDestination(destination);
		container.setBackOff(backOff);
		return container;

	}
 
Example #29
Source File: DefaultMessageListenerContainer.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Specify the {@link BackOff} instance to use to compute the interval
 * between recovery attempts. If the {@link BackOffExecution} implementation
 * returns {@link BackOffExecution#STOP}, this listener container will not further
 * attempt to recover.
 * <p>The {@link #setRecoveryInterval(long) recovery interval} is ignored
 * when this property is set.
 */
public void setBackOff(BackOff backOff) {
	this.backOff = backOff;
}
 
Example #30
Source File: DefaultMessageListenerContainer.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Specify the {@link BackOff} instance to use to compute the interval
 * between recovery attempts. If the {@link BackOffExecution} implementation
 * returns {@link BackOffExecution#STOP}, this listener container will not further
 * attempt to recover.
 * <p>The {@link #setRecoveryInterval(long) recovery interval} is ignored
 * when this property is set.
 * @since 4.1
 */
public void setBackOff(BackOff backOff) {
	this.backOff = backOff;
}