Search in sources :

Example 1 with MessageRecoverer

use of org.springframework.amqp.rabbit.retry.MessageRecoverer in project spring-boot by spring-projects.

the class RabbitAutoConfigurationTests method checkCommonProps.

private void checkCommonProps(AssertableApplicationContext context, AbstractRabbitListenerContainerFactory<?> containerFactory) {
    assertThat(containerFactory).hasFieldOrPropertyWithValue("autoStartup", Boolean.FALSE);
    assertThat(containerFactory).hasFieldOrPropertyWithValue("acknowledgeMode", AcknowledgeMode.MANUAL);
    assertThat(containerFactory).hasFieldOrPropertyWithValue("prefetchCount", 40);
    assertThat(containerFactory).hasFieldOrPropertyWithValue("messageConverter", context.getBean("myMessageConverter"));
    assertThat(containerFactory).hasFieldOrPropertyWithValue("defaultRequeueRejected", Boolean.FALSE);
    assertThat(containerFactory).hasFieldOrPropertyWithValue("idleEventInterval", 5L);
    Advice[] adviceChain = containerFactory.getAdviceChain();
    assertThat(adviceChain).isNotNull();
    assertThat(adviceChain).hasSize(1);
    Advice advice = adviceChain[0];
    MessageRecoverer messageRecoverer = context.getBean("myMessageRecoverer", MessageRecoverer.class);
    MethodInvocationRecoverer<?> mir = (MethodInvocationRecoverer<?>) ReflectionTestUtils.getField(advice, "recoverer");
    Message message = mock(Message.class);
    Exception ex = new Exception("test");
    mir.recover(new Object[] { "foo", message }, ex);
    then(messageRecoverer).should().recover(message, ex);
    RetryTemplate retryTemplate = (RetryTemplate) ReflectionTestUtils.getField(advice, "retryOperations");
    assertThat(retryTemplate).isNotNull();
    SimpleRetryPolicy retryPolicy = (SimpleRetryPolicy) ReflectionTestUtils.getField(retryTemplate, "retryPolicy");
    ExponentialBackOffPolicy backOffPolicy = (ExponentialBackOffPolicy) ReflectionTestUtils.getField(retryTemplate, "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) MethodInvocationRecoverer(org.springframework.retry.interceptor.MethodInvocationRecoverer) SimpleRetryPolicy(org.springframework.retry.policy.SimpleRetryPolicy) Advice(org.aopalliance.aop.Advice) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 2 with MessageRecoverer

use of org.springframework.amqp.rabbit.retry.MessageRecoverer in project spring-boot by spring-projects.

the class AbstractRabbitListenerContainerFactoryConfigurer method configure.

protected void configure(T factory, ConnectionFactory connectionFactory, RabbitProperties.AmqpContainer configuration) {
    Assert.notNull(factory, "Factory must not be null");
    Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
    Assert.notNull(configuration, "Configuration must not be null");
    factory.setConnectionFactory(connectionFactory);
    if (this.messageConverter != null) {
        factory.setMessageConverter(this.messageConverter);
    }
    factory.setAutoStartup(configuration.isAutoStartup());
    if (configuration.getAcknowledgeMode() != null) {
        factory.setAcknowledgeMode(configuration.getAcknowledgeMode());
    }
    if (configuration.getPrefetch() != null) {
        factory.setPrefetchCount(configuration.getPrefetch());
    }
    if (configuration.getDefaultRequeueRejected() != null) {
        factory.setDefaultRequeueRejected(configuration.getDefaultRequeueRejected());
    }
    if (configuration.getIdleEventInterval() != null) {
        factory.setIdleEventInterval(configuration.getIdleEventInterval().toMillis());
    }
    factory.setMissingQueuesFatal(configuration.isMissingQueuesFatal());
    factory.setDeBatchingEnabled(configuration.isDeBatchingEnabled());
    ListenerRetry retryConfig = configuration.getRetry();
    if (retryConfig.isEnabled()) {
        RetryInterceptorBuilder<?, ?> builder = (retryConfig.isStateless()) ? RetryInterceptorBuilder.stateless() : RetryInterceptorBuilder.stateful();
        RetryTemplate retryTemplate = new RetryTemplateFactory(this.retryTemplateCustomizers).createRetryTemplate(retryConfig, RabbitRetryTemplateCustomizer.Target.LISTENER);
        builder.retryOperations(retryTemplate);
        MessageRecoverer recoverer = (this.messageRecoverer != null) ? this.messageRecoverer : new RejectAndDontRequeueRecoverer();
        builder.recoverer(recoverer);
        factory.setAdviceChain(builder.build());
    }
}
Also used : RejectAndDontRequeueRecoverer(org.springframework.amqp.rabbit.retry.RejectAndDontRequeueRecoverer) RetryTemplate(org.springframework.retry.support.RetryTemplate) MessageRecoverer(org.springframework.amqp.rabbit.retry.MessageRecoverer) ListenerRetry(org.springframework.boot.autoconfigure.amqp.RabbitProperties.ListenerRetry)

Example 3 with MessageRecoverer

use of org.springframework.amqp.rabbit.retry.MessageRecoverer 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

MessageRecoverer (org.springframework.amqp.rabbit.retry.MessageRecoverer)3 RetryTemplate (org.springframework.retry.support.RetryTemplate)3 Advice (org.aopalliance.aop.Advice)2 Message (org.springframework.amqp.core.Message)2 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)2 ExponentialBackOffPolicy (org.springframework.retry.backoff.ExponentialBackOffPolicy)2 MethodInvocationRecoverer (org.springframework.retry.interceptor.MethodInvocationRecoverer)2 SimpleRetryPolicy (org.springframework.retry.policy.SimpleRetryPolicy)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Test (org.junit.Test)1 ExpectedException (org.junit.rules.ExpectedException)1 SimpleRabbitListenerContainerFactory (org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory)1 RejectAndDontRequeueRecoverer (org.springframework.amqp.rabbit.retry.RejectAndDontRequeueRecoverer)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 ListenerRetry (org.springframework.boot.autoconfigure.amqp.RabbitProperties.ListenerRetry)1