首页 > 解决方案 > 如何绑定 Spring 集成错误处理和 RabbitMQ 错误处理以重新排队消息失败并出现一些跳过的异常?

问题描述

如何绑定 Spring 集成 errorChannel(错误处理)和 RabbitMQ 错误处理(listenerContainer 的 errorChannel)以重新排队消息失败并出现一些跳过的异常?

我有Spring一个Groovy使用RabbitMQ. 配置是:

    <int:service-activator id="GlobalErrorHandler" input-channel="errorChannel" 
ref="globalErrorHandler"/>

    <int-amqp:inbound-channel-adapter id="event-processing-inbound-rabbit-adapter"
                                          channel="mli-forever-event-processing-channel"
                                          error-channel="errorChannel"
                                          mapped-request-headers="*"
                                          listener-container="listenerContainer"/>

@Bean
    SimpleMessageListenerContainer listenerContainer( final CachingConnectionFactory connectionFactory, 
       final ApplicationProperties configuration ) {

        new SimpleMessageListenerContainer( connectionFactory ).with {
            concurrentConsumers = configuration.concurrentConsumers
            queueNames = configuration.rabbitQueueName
            autoDeclare = true
            it
        }
    }

仅记录日志的错误处理程序:

class GlobalErrorHandler extends AbstractFeedbackAware {
    @ServiceActivator
    void handleMessagingException( final MessagingException exception ) {
        feedbackProvider.sendFeedback( CoreFeedbackContext.CONSUMING_EVENT_ERROR, exception.message )
    }
}

如果我得到异常PersistenceException消息不会重新排队,但它应该。我在谷歌上搜索和阅读文档,如果发生异常,我不明白错误的方式。如果添加errorHandlercontainerListener处理程序的内容将被调用怎么办?两个都??什么顺序??我在想一些东西:

new SimpleMessageListenerContainer( null ).with {
    errorHandler = { it instanceof PersistenceException } as ConditionalRejectingErrorHandler
    it
}
class ExceptionStrategyToAvoidPersistenceException extends ConditionalRejectingErrorHandler.DefaultExceptionStrategy {

    @Override
    boolean isFatal( final Throwable t ) {
        super.isFatal( t ) && !( t instanceof PersistenceException )
    }
}

但我不明白为什么PersistenceException不重新排队,因为它不在默认异常策略的致命错误列表中。有人可以帮助我解决问题吗?或者给个解释??或者如何重现集成测试中的错误,因为我无法限制-containerPostgreSQL中的DB()空间Docker

将不胜感激!!

标签: javaspringgroovyrabbitmqspring-integration

解决方案


首先error-channel调用流;它必须重新抛出异常以使消息重新排队。


推荐阅读