在异常时重试spring Integration IntegrationFlow

时间:2017-06-30 11:21:47

标签: java spring spring-integration spring-amqp spring-integration-amqp

我有一个Spring Integration IntegrationFlow,其定义如下:

IntegrationFlows.from(Amqp.inboundAdapter(connectionFactory, "queueName")
                    .id("id")
                    .autoStartup(autoStartup)
                    .concurrentConsumers(2)
                    .maxConcurrentConsumers(3)
                    .messageConverter(messageConverter()))
                    .aggregate(a -> ...)
                    .handle(serviceActivatorBean)
                    .get();

serviceActivatorBean看起来像这样:

@Component
@Transactional
public class ServiceActivator {

    @ServiceActivator

    public void myMethod(Collection<MyEvent> events) {
        ....
    }    
}

如果myMethod抛出异常,它将被记录但不会重试。我试图将IntegrationFlow更改为:

RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice();
RetryTemplate retryTemplate = new RetryTemplate();
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(5);
retryTemplate.setRetryPolicy(retryPolicy);
advice.setRetryTemplate(retryTemplate);

IntegrationFlows.from(Amqp.inboundAdapter(connectionFactory, "queueName")
                    .id("id")
                    .autoStartup(autoStartup)
                    .adviceChain(advice)
                    .concurrentConsumers(2)
                    .maxConcurrentConsumers(3)
                    .messageConverter(messageConverter()))
                    .aggregate(a -> ...)
                    .handle(serviceActivatorBean)
                    .get();

但后来我发了这样的日志消息(重试不会发生):

  

2017-06-30 13:18:10.611 WARN 88706 --- [erContainer#1-2]   o.s.i.h.a.RequestHandlerRetryAdvice:这个建议   org.springframework.integration.handler.advice.RequestHandlerRetryAdvice   只能用于MessageHandlers;试图建议方法   &#39; invokeListener&#39;在   &#39; org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer $ 1#39;   被忽略

如何配置此IntegrationFlow的行为方式与RabbitListener相同?即让RabbitMQ再次发布消息。

1 个答案:

答案 0 :(得分:1)

使用适配器建议链中的retry interceptor而不是RequestHandlerRetryAdvice - 用于消费端点,如消息所示。