rabbitmq消息重试

如何实现消息重试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<rabbit:connection-factory id="connectionFactory" />
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" />
<rabbit:admin connection-factory="connectionFactory" />
<bean id="messageConverter" class="org.springframework.amqp.support.converter.SimpleMessageConverter"/>
<rabbit:queue name="microlesson.reward.msg.queue" />
<rabbit:direct-exchange name="business.common.order.confirmed.direct.exchange">
<rabbit:bindings>
<rabbit:binding key="1245" queue="microlesson.reward.msg.queue"/>
</rabbit:bindings>
</rabbit:direct-exchange>
<rabbit:listener-container prefetch="20"
concurrency="10"
connection-factory="connectionFactory"
message-converter="messageConverter"
requeue-rejected="false"
advice-chain="retryInterceptor">
<rabbit:listener ref="rewordMsgListener" queue-names="microlesson.reward.msg.queue"/>
</rabbit:listener-container>
<bean id="rewordMsgListener" class="com.ximalaya.microlesson.listener.listener.RewardMsgListener"/>
<bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">
<property name="backOffPolicy">
<bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
<property name="initialInterval" value="10000"/>
<property name="maxInterval" value="600000"/>
</bean>
</property>
<property name="retryPolicy">
<bean class="org.springframework.retry.policy.SimpleRetryPolicy">
<property name="maxAttempts" value="7"/>
</bean>
</property>
</bean>
<!--retry Interceptor -->
<bean id="retryInterceptor"
class="org.springframework.amqp.rabbit.config.StatelessRetryOperationsInterceptorFactoryBean">
<property name="messageRecoverer" ref="messageRecoverer"/>
<property name="retryOperations" ref="retryTemplate"/>
</bean>
  • requeue-rejected设为false表示一条消息即使没有被ack,也不会再重新发送(默认是会重新加入队列)

如何实现消息延时

RabbitMQ可以针对QueueMessage设置x-message-tt,来控制消息的生存时间,如果超时,则消息变为dead letter

参考

rabbitmq 消息可靠投递及消费机制

热评文章