在Spring-AMQP with Rabbitmq中重试不起作用

时间:2015-09-03 09:47:23

标签: spring rabbitmq spring-amqp

如果在使用来自rabbitmq的消息时出现任何异常,我试图重试3次,但是3次后没有停止,连续多次重试。

请有人帮我这个 提前谢谢

的Config.xml

<!-- Provides connection to the RabbitMQ broker -->
<bean id="connectionFactoryRabbit"
    class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory"
    p:username="guest" p:password="guest" p:port="5672">
    <constructor-arg value="localhost" />

</bean>

<bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate"
    p:connectionFactory-ref="connectionFactoryRabbit">
</bean>

<bean id="messageConverter"
    class="org.springframework.amqp.support.converter.SimpleMessageConverter">
    <property name="createMessageIds" value="true" />
</bean>
<!-- A template for sending messages and performing other commands to RabbitMQ -->

<bean
    class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer"
    p:connectionFactory-ref="connectionFactoryRabbit" p:queueNames="TEST"
    p:messageListener-ref="messageListener">

    <!-- <property name="txSize" value="3" /> -->
    <property name="prefetchCount" value="100" />
    <property name="messageListener" ref="messageListenerAdapter" />
    <property name="adviceChain" ref="retryChain" />

    <!-- <property name="adviceChain" ref="retryInterceptor"></property> -->
</bean>

<!-- <rabbit:queue id="adt" name="TEST" /> <rabbit:queue id="adt"> <rabbit:queue-arguments> 
    <entry key="x-dead-letter-queue" value="dead" /> </rabbit:queue-arguments> 
    </rabbit:queue> <rabbit:direct-queue name="dead"> <rabbit:bindings> <rabbit:binding 
    queue="dead" key="dead" /> </rabbit:bindings> </rabbit:direct-queue> -->

<util:list id="retryChain">
    <bean class="org.springframework.amqp.rabbit.retry.MissingMessageIdAdvice">
        <constructor-arg>
            <bean class="org.springframework.retry.policy.MapRetryContextCache" />
        </constructor-arg>
    </bean>
    <ref bean="retryInterceptor" />
</util:list>

<bean id="retryInterceptor"
    class="org.springframework.amqp.rabbit.config.StatelessRetryOperationsInterceptorFactoryBean">
    <property name="messageRecoverer" ref="rejectAndDontRequeueRecoverer" />
    <property name="retryOperations" ref="retryTemplate" />
</bean>

<bean id="messageListenerAdapter"
    class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
    <property name="delegate" ref="writeToFileHandler" />
    <property name="messageConverter" ref="messageConverter" />
</bean>
<bean id="writeToFileHandler" class="com.gnax.sola.jms.AmqpConsumer">
</bean>
<bean id="rejectAndDontRequeueRecoverer"
    class="org.springframework.amqp.rabbit.retry.RejectAndDontRequeueRecoverer" />
<!-- <bean id="missingMessageId" class="org.springframework.amqp.rabbit.retry.MissingMessageIdAdvice" 
    /> -->
<bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">
    <property name="backOffPolicy">
        <bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
            <property name="initialInterval" value="500" />
            <property name="multiplier" value="2" />
            <property name="maxInterval" value="30000" />
        </bean>
    </property>
    <property name="retryPolicy">
        <bean class="org.springframework.retry.policy.SimpleRetryPolicy">
            <property name="maxAttempts" value="3" />
        </bean>
    </property>
</bean>

<bean id="messageListener" class="com.test.amqp.AmqpConsumer" />
<!-- This helps in configuring the AMQP broker, like creating a new queue -->
<bean id="amqpAdmin" class="org.springframework.amqp.rabbit.core.RabbitAdmin">
    <constructor-arg ref="connectionFactoryRabbit" />
</bean>

消费者代码

public void onMessage ( Message message, Channel channel ) throws Exception
{
    try
    {
        if ( message != null && message.getMessageProperties ().getReceivedRoutingKey () != null )
        {
            system.out.println(message);
        }
    }
    catch ( Exception e )
    {
        LOGGER.error ( "Error while consuming the message ", e );
        channel.basicNack ( message.getMessageProperties ().getDeliveryTag (), true, true );
    }


}

调试日志

 at [Source: java.io.StringReader@d998aa1; line: 1, column: 15]
11:42:41,228 INFO  [com.gnax.sola.jms.AmqpConsumer] (SimpleAsyncTaskExecutor-5) Proccessed the message from AMQP
11:42:41,448 INFO  [com.gnax.sola.imagemanager.jms.ADTRequestHandler] (SimpleAsyncTaskExecutor-5) Consuming ADT Message
11:42:41,448 INFO  [com.gnax.sola.imagemanager.jms.ADTRequestHandler] (SimpleAsyncTaskExecutor-5) Error while receiving ADT Message
11:42:41,448 ERROR [com.gnax.sola.imagemanager.jms.ADTRequestHandler] (SimpleAsyncTaskExecutor-5) Unrecognized token 'fgffgfd': was expecting 
 at [Source: java.io.StringReader@2f852217; line: 1, column: 15]
11:42:41,448 INFO  [com.gnax.sola.jms.AmqpConsumer] (SimpleAsyncTaskExecutor-5) Proccessed the message from AMQP
11:42:41,448 WARN  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-5) Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: com.rabbitmq.client.ShutdownSignalException: channel error; reason: {#method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - unknown delivery tag 1, class-id=60, method-id=80), null, ""}
11:42:41,448 INFO  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-5) Restarting Consumer: tag=[amq.ctag-t5CTaRT9UXy_jsmt5Bz95w], channel=Cached Rabbit Channel: AMQChannel(amqp://sola@192.168.7.108:5672/,16), acknowledgeMode=AUTO local queue size=0
11:42:41,672 INFO  [com.gnax.sola.imagemanager.jms.ADTRequestHandler] (SimpleAsyncTaskExecutor-6) Consuming ADT Message
11:42:41,672 INFO  [com.gnax.sola.imagemanager.jms.ADTRequestHandler] (SimpleAsyncTaskExecutor-6) Error while receiving ADT Message
11:42:41,672 ERROR [com.gnax.sola.imagemanager.jms.ADTRequestHandler] (SimpleAsyncTaskExecutor-6) Unrecognized token 'fgffgfd': was expecting 
 at [Source: java.io.StringReader@1736e037; line: 1, column: 15]
11:42:41,682 INFO  [com.gnax.sola.jms.AmqpConsumer] (SimpleAsyncTaskExecutor-6) Proccessed the message from AMQP
11:42:41,922 INFO  [com.gnax.sola.imagemanager.jms.ADTRequestHandler] (SimpleAsyncTaskExecutor-6) Consuming ADT Message
11:42:41,922 INFO  [com.gnax.sola.imagemanager.jms.ADTRequestHandler] (SimpleAsyncTaskExecutor-6) Error while receiving ADT Message
11:42:41,922 ERROR [com.gnax.sola.imagemanager.jms.ADTRequestHandler] (SimpleAsyncTaskExecutor-6) Unrecognized token 'fgffgfd': was expecting 
 at [Source: java.io.StringReader@467b09b7; line: 1, column: 15]
11:42:41,932 INFO  [com.gnax.sola.jms.AmqpConsumer] (SimpleAsyncTaskExecutor-6) Proccessed the message from AMQP
11:42:41,932 WARN  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-6) Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: com.rabbitmq.client.ShutdownSignalException: channel error; reason: {#method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - unknown delivery tag 1, class-id=60, method-id=80), null, ""}
11:42:41,932 INFO  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-6) Restarting Consumer: tag=[amq.ctag-A3tGkE4oDLM0D4Lgz_4iOw], channel=Cached Rabbit Channel: AMQChannel(amqp://sola@192.168.7.108:5672/,17), acknowledgeMode=AUTO local queue size=0
11:42:41,972 WARN  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: com.rabbitmq.client.ShutdownSignalException: connection error; reason: com.rabbitmq.client.impl.UnknownChannelException: Unknown channel number 17
11:42:41,972 INFO  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Restarting Consumer: tag=[amq.ctag-uczR-I6V7HeiZnQQj3PGqw], channel=Cached Rabbit Channel: AMQChannel(amqp://sola@192.168.7.108:5672/,6), acknowledgeMode=AUTO local queue size=0
11:42:41,972 WARN  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: com.rabbitmq.client.ShutdownSignalException: connection error; reason: com.rabbitmq.client.impl.UnknownChannelException: Unknown channel number 17
11:42:41,972 WARN  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: com.rabbitmq.client.ShutdownSignalException: connection error; reason: com.rabbitmq.client.impl.UnknownChannelException: Unknown channel number 17
11:42:41,972 INFO  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Restarting Consumer: tag=[amq.ctag-w45jv1s-vMEVL7LHF4C1wA], channel=Cached Rabbit Channel: AMQChannel(amqp://sola@192.168.7.108:5672/,10), acknowledgeMode=AUTO local queue size=0
11:42:41,972 INFO  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Restarting Consumer: tag=[amq.ctag-sacFJPkjSwD8GNmpB3rW-Q], channel=Cached Rabbit Channel: AMQChannel(amqp://sola@192.168.7.108:5672/,4), acknowledgeMode=AUTO local queue size=0
11:42:41,972 WARN  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: com.rabbitmq.client.ShutdownSignalException: connection error; reason: com.rabbitmq.client.impl.UnknownChannelException: Unknown channel number 17
11:42:41,972 INFO  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Restarting Consumer: tag=[amq.ctag-d6ezJG0sAGFOJeBOKiCaaA], channel=Cached Rabbit Channel: AMQChannel(amqp://sola@192.168.7.108:5672/,2), acknowledgeMode=AUTO local queue size=0
11:42:42,032 WARN  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: com.rabbitmq.client.ShutdownSignalException: connection error; reason: com.rabbitmq.client.impl.UnknownChannelException: Unknown channel number 17
11:42:42,032 INFO  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Restarting Consumer: tag=[amq.ctag--GGgfZWMvUud7Q3kE7FqtA], channel=Cached Rabbit Channel: AMQChannel(amqp://sola@192.168.7.108:5672/,9), acknowledgeMode=AUTO local queue size=0
11:42:42,032 WARN  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: com.rabbitmq.client.ShutdownSignalException: connection error; reason: com.rabbitmq.client.impl.UnknownChannelException: Unknown channel number 17
11:42:42,032 INFO  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Restarting Consumer: tag=[amq.ctag-i4SmHrGYjAxvAV_uO1hDYA], channel=Cached Rabbit Channel: AMQChannel(amqp://sola@192.168.7.108:5672/,8), acknowledgeMode=AUTO local queue size=0
11:42:42,042 WARN  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: com.rabbitmq.client.ShutdownSignalException: connection error; reason: com.rabbitmq.client.impl.UnknownChannelException: Unknown channel number 17
11:42:42,042 INFO  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Restarting Consumer: tag=[amq.ctag-2VPna8pwyxZHBg-Zh5OicQ], channel=Cached Rabbit Channel: AMQChannel(amqp://sola@192.168.7.108:5672/,5), acknowledgeMode=AUTO local queue size=0
11:42:42,042 WARN  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: com.rabbitmq.client.ShutdownSignalException: connection error; reason: com.rabbitmq.client.impl.UnknownChannelException: Unknown channel number 17
11:42:42,042 INFO  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Restarting Consumer: tag=[amq.ctag-h1TLZWU9wI3DFBIu0k3fQA], channel=Cached Rabbit Channel: AMQChannel(amqp://sola@192.168.7.108:5672/,3), acknowledgeMode=AUTO local queue size=0
11:42:42,162 INFO  [com.gnax.sola.imagemanager.jms.ADTRequestHandler] (SimpleAsyncTaskExecutor-7) Consuming ADT Message
11:42:42,162 INFO  [com.gnax.sola.imagemanager.jms.ADTRequestHandler] (SimpleAsyncTaskExecutor-7) Error while receiving ADT Message
11:42:42,162 ERROR [com.gnax.sola.imagemanager.jms.ADTRequestHandler] (SimpleAsyncTaskExecutor-7) Unrecognized token 'fgffgfd': was expecting 
 at [Source: java.io.StringReader@1bbecd4d; line: 1, column: 15]
11:42:42,162 INFO  [com.gnax.sola.jms.AmqpConsumer] (SimpleAsyncTaskExecutor-7) Proccessed the message from AMQP
11:42:42,682 WARN  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: com.rabbitmq.client.ShutdownSignalException: connection error; reason: com.rabbitmq.client.impl.UnknownChannelException: Unknown channel number 17
11:42:42,683 INFO  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) Restarting Consumer: tag=[amq.ctag-6lQWkrIIeM1etGSo8wcf6g], channel=Cached Rabbit Channel: AMQChannel(amqp://sola@192.168.7.108:5672/,7), acknowledgeMode=AUTO local queue size=0
11:42:43,181 WARN  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-7) Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: com.rabbitmq.client.ShutdownSignalException: channel error; reason: {#method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - unknown delivery tag 1, class-id=60, method-id=80), null, ""}
11:42:43,181 INFO  [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-7) Restarting Consumer: tag=[amq.ctag-QNs_wFdj_MjVeX768J1udg], channel=Cached Rabbit Channel: AMQChannel(amqp://sola@192.168.7.108:5672/,2), acknowledgeMode=AUTO local queue size=0

1 个答案:

答案 0 :(得分:0)

启用DEBUG日志记录以查看重试活动;如果你仍然无法搞清楚,请发布日志。