我正在使用带有RabbitMQ的Spring,并且我试图避免在发生运行时异常时重新发送消息.我试图在侦听器容器中将requeue-reject设置为false,并配置一个抛出AmqpRejectAndDontRequeueException的自定义错误处理程序.似乎两种策略都失败了,而且这些消息也会永远地重新传递.任何想法的原因?
感谢帮助.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.1.xsd">
<rabbit:connection-factory id="rabbitMQConnectionFactory" host="localhost" port="5672" username="guest" password="guest" />
<rabbit:admin connection-factory="rabbitMQConnectionFactory" />
<rabbit:template id="amqpTemplate" connection-factory="rabbitMQConnectionFactory" />
<rabbit:queue name="q1" />
<rabbit:queue name="q2" />
<rabbit:listener-container error-handler="errorHandler" connection-factory="rabbitMQConnectionFactory" concurrency="10" transaction-manager="transactionManager" requeue-rejected="false">
<rabbit:listener ref="q1Listener" method="consumeMessage" queue-names="q1" />
<rabbit:listener ref="q2Listener" method="consumeMessage" queue-names="q2" />
</rabbit:listener-container>
<bean id="errorHandler" class="ErrorHandler" />
<bean id="q1Listener" class="Q1MessageConsumerBean" />
<bean id="q2Listener" class="Q2MessageConsumerBean" />
</beans>
解决方法:
首先requeue-rejected =“false”和AmqpRejectAndDontRequeueException在目标侦听器的RuntimeException情况下执行相同的操作.
这是一个测试用例,表明requeue-rejected =“false”效果很好.
<rabbit:connection-factory id="connectionFactory" host="localhost" />
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
exchange="foo" routing-key="foo" />
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="foo" />
<rabbit:direct-exchange name="foo">
<rabbit:bindings>
<rabbit:binding queue="foo" key="foo" />
</rabbit:bindings>
</rabbit:direct-exchange>
<rabbit:listener-container connection-factory="connectionFactory" auto-startup="false" requeue-rejected="false">
<rabbit:listener ref="listener" queue-names="foo" />
</rabbit:listener-container>
<bean id="listener" class="org.mockito.Mockito" factory-method="spy">
<constructor-arg>
<bean class="org.springframework.amqp.rabbit.listener.RejectedTests$ThrowListener" />
</constructor-arg>
</bean>
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private SimpleMessageListenerContainer container;
@Autowired
private ThrowListener throwListener;
@Test
public void test() throws Exception {
rabbitTemplate.convertAndSend("foo");
container.start();
Thread.sleep(2000);
Mockito.verify(throwListener).onMessage(Mockito.any(Message.class));
}
public static class ThrowListener implements MessageListener {
@Override
public void onMessage(Message message) {
throw new RuntimeException("intentional reject");
}
}
Mockito.verify(throwListener).onMessage(Mockito.any(Message.class));确认onMessage仅在第一次传递时被调用一次.第二次传递没有发生,因为我们的消息被拒绝并从RabbitMQ Broker中删除.
我只看到了signle的地方,它不是独立于requeue-rejected =“false”而发生的 – RabbitResourceHolder#rollbackAll():
for (Long deliveryTag : deliveryTags.get(channel)) {
try {
channel.basicReject(deliveryTag, true);
} catch (IOException ex) {
throw new AmqpIOException(ex);
}
}
但是,只有TX提交和该提交的Expception才会到达此块.因此,在这种情况下,我们会导致TX回滚:
private boolean doReceiveAndExecute(BlockingQueueConsumer consumer) throws Throwable {
Channel channel = consumer.getChannel();
for (int i = 0; i < txSize; i++) {
logger.trace("Waiting for message from consumer.");
Message message = consumer.nextMessage(receiveTimeout);
if (message == null) {
break;
}
try {
executeListener(channel, message);
}
catch (ImmediateAcknowledgeAmqpException e) {
break;
}
catch (Throwable ex) {
consumer.rollbackOnExceptionIfNecessary(ex);
throw ex;
}
}
return consumer.commitIfNecessary(isChannelLocallyTransacted(channel));
注意最后一行. consumer.rollbackOnExceptionIfNecessary(前);是从侦听器抛出异常的情况.否则,我们到达最后一行并等待外部TX提交.
如果这是你的情况,请告诉我们.