Search in sources :

Example 1 with ExponentialBackOffPolicy

use of org.springframework.retry.backoff.ExponentialBackOffPolicy in project spring-boot by spring-projects.

the class RabbitAutoConfigurationTests method testRabbitTemplateRetry.

@Test
public void testRabbitTemplateRetry() {
    load(TestConfiguration.class, "spring.rabbitmq.template.retry.enabled:true", "spring.rabbitmq.template.retry.maxAttempts:4", "spring.rabbitmq.template.retry.initialInterval:2000", "spring.rabbitmq.template.retry.multiplier:1.5", "spring.rabbitmq.template.retry.maxInterval:5000", "spring.rabbitmq.template.receiveTimeout:123", "spring.rabbitmq.template.replyTimeout:456");
    RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
    DirectFieldAccessor dfa = new DirectFieldAccessor(rabbitTemplate);
    assertThat(dfa.getPropertyValue("receiveTimeout")).isEqualTo(123L);
    assertThat(dfa.getPropertyValue("replyTimeout")).isEqualTo(456L);
    RetryTemplate retryTemplate = (RetryTemplate) dfa.getPropertyValue("retryTemplate");
    assertThat(retryTemplate).isNotNull();
    dfa = new DirectFieldAccessor(retryTemplate);
    SimpleRetryPolicy retryPolicy = (SimpleRetryPolicy) dfa.getPropertyValue("retryPolicy");
    ExponentialBackOffPolicy backOffPolicy = (ExponentialBackOffPolicy) dfa.getPropertyValue("backOffPolicy");
    assertThat(retryPolicy.getMaxAttempts()).isEqualTo(4);
    assertThat(backOffPolicy.getInitialInterval()).isEqualTo(2000);
    assertThat(backOffPolicy.getMultiplier()).isEqualTo(1.5);
    assertThat(backOffPolicy.getMaxInterval()).isEqualTo(5000);
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) RetryTemplate(org.springframework.retry.support.RetryTemplate) ExponentialBackOffPolicy(org.springframework.retry.backoff.ExponentialBackOffPolicy) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) SimpleRetryPolicy(org.springframework.retry.policy.SimpleRetryPolicy) Test(org.junit.Test)

Example 2 with ExponentialBackOffPolicy

use of org.springframework.retry.backoff.ExponentialBackOffPolicy in project fru-paqx-parent by dellemc-symphony.

the class RabbitConfig method retryTemplate.

@Bean
RetryTemplate retryTemplate() {
    RetryTemplate retryTemplate = new RetryTemplate();
    ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
    backOffPolicy.setInitialInterval(INITIAL_INTERVAL);
    backOffPolicy.setMultiplier(MULTIPLIER);
    backOffPolicy.setMaxInterval(MAX_INTERVAL);
    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
    retryPolicy.setMaxAttempts(MAX_ATTEMPTS);
    retryTemplate.setBackOffPolicy(backOffPolicy);
    retryTemplate.setRetryPolicy(retryPolicy);
    LOGGER.debug("Retry configuration: " + backOffPolicy + " / " + retryPolicy);
    return retryTemplate;
}
Also used : RetryTemplate(org.springframework.retry.support.RetryTemplate) ExponentialBackOffPolicy(org.springframework.retry.backoff.ExponentialBackOffPolicy) SimpleRetryPolicy(org.springframework.retry.policy.SimpleRetryPolicy) Bean(org.springframework.context.annotation.Bean)

Example 3 with ExponentialBackOffPolicy

use of org.springframework.retry.backoff.ExponentialBackOffPolicy in project spring-boot by spring-projects.

the class RabbitAutoConfigurationTests method testRabbitListenerContainerFactoryWithCustomSettings.

@Test
public void testRabbitListenerContainerFactoryWithCustomSettings() {
    load(new Class<?>[] { MessageConvertersConfiguration.class, MessageRecoverersConfiguration.class }, "spring.rabbitmq.listener.retry.enabled:true", "spring.rabbitmq.listener.retry.maxAttempts:4", "spring.rabbitmq.listener.retry.initialInterval:2000", "spring.rabbitmq.listener.retry.multiplier:1.5", "spring.rabbitmq.listener.retry.maxInterval:5000", "spring.rabbitmq.listener.autoStartup:false", "spring.rabbitmq.listener.acknowledgeMode:manual", "spring.rabbitmq.listener.concurrency:5", "spring.rabbitmq.listener.maxConcurrency:10", "spring.rabbitmq.listener.prefetch:40", "spring.rabbitmq.listener.defaultRequeueRejected:false", "spring.rabbitmq.listener.idleEventInterval:5", "spring.rabbitmq.listener.transactionSize:20");
    SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory = this.context.getBean("rabbitListenerContainerFactory", SimpleRabbitListenerContainerFactory.class);
    DirectFieldAccessor dfa = new DirectFieldAccessor(rabbitListenerContainerFactory);
    assertThat(dfa.getPropertyValue("autoStartup")).isEqualTo(Boolean.FALSE);
    assertThat(dfa.getPropertyValue("acknowledgeMode")).isEqualTo(AcknowledgeMode.MANUAL);
    assertThat(dfa.getPropertyValue("concurrentConsumers")).isEqualTo(5);
    assertThat(dfa.getPropertyValue("maxConcurrentConsumers")).isEqualTo(10);
    assertThat(dfa.getPropertyValue("prefetchCount")).isEqualTo(40);
    assertThat(dfa.getPropertyValue("txSize")).isEqualTo(20);
    assertThat(dfa.getPropertyValue("messageConverter")).isSameAs(this.context.getBean("myMessageConverter"));
    assertThat(dfa.getPropertyValue("defaultRequeueRejected")).isEqualTo(Boolean.FALSE);
    assertThat(dfa.getPropertyValue("idleEventInterval")).isEqualTo(5L);
    Advice[] adviceChain = (Advice[]) dfa.getPropertyValue("adviceChain");
    assertThat(adviceChain).isNotNull();
    assertThat(adviceChain.length).isEqualTo(1);
    dfa = new DirectFieldAccessor(adviceChain[0]);
    MessageRecoverer messageRecoverer = this.context.getBean("myMessageRecoverer", MessageRecoverer.class);
    MethodInvocationRecoverer<?> mir = (MethodInvocationRecoverer<?>) dfa.getPropertyValue("recoverer");
    Message message = mock(Message.class);
    Exception ex = new Exception("test");
    mir.recover(new Object[] { "foo", message }, ex);
    verify(messageRecoverer).recover(message, ex);
    RetryTemplate retryTemplate = (RetryTemplate) dfa.getPropertyValue("retryOperations");
    assertThat(retryTemplate).isNotNull();
    dfa = new DirectFieldAccessor(retryTemplate);
    SimpleRetryPolicy retryPolicy = (SimpleRetryPolicy) dfa.getPropertyValue("retryPolicy");
    ExponentialBackOffPolicy backOffPolicy = (ExponentialBackOffPolicy) dfa.getPropertyValue("backOffPolicy");
    assertThat(retryPolicy.getMaxAttempts()).isEqualTo(4);
    assertThat(backOffPolicy.getInitialInterval()).isEqualTo(2000);
    assertThat(backOffPolicy.getMultiplier()).isEqualTo(1.5);
    assertThat(backOffPolicy.getMaxInterval()).isEqualTo(5000);
}
Also used : RetryTemplate(org.springframework.retry.support.RetryTemplate) MessageRecoverer(org.springframework.amqp.rabbit.retry.MessageRecoverer) Message(org.springframework.amqp.core.Message) ExponentialBackOffPolicy(org.springframework.retry.backoff.ExponentialBackOffPolicy) SimpleRabbitListenerContainerFactory(org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MethodInvocationRecoverer(org.springframework.retry.interceptor.MethodInvocationRecoverer) SimpleRetryPolicy(org.springframework.retry.policy.SimpleRetryPolicy) Advice(org.aopalliance.aop.Advice) ExpectedException(org.junit.rules.ExpectedException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Test(org.junit.Test)

Aggregations

ExponentialBackOffPolicy (org.springframework.retry.backoff.ExponentialBackOffPolicy)3 SimpleRetryPolicy (org.springframework.retry.policy.SimpleRetryPolicy)3 RetryTemplate (org.springframework.retry.support.RetryTemplate)3 Test (org.junit.Test)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 Advice (org.aopalliance.aop.Advice)1 ExpectedException (org.junit.rules.ExpectedException)1 Message (org.springframework.amqp.core.Message)1 SimpleRabbitListenerContainerFactory (org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory)1 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)1 MessageRecoverer (org.springframework.amqp.rabbit.retry.MessageRecoverer)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 Bean (org.springframework.context.annotation.Bean)1 MethodInvocationRecoverer (org.springframework.retry.interceptor.MethodInvocationRecoverer)1