2012-12-04 34 views
2

我正在使用SimpleMessageListenerContainer異步消費消息。該消息調用服務層上的方法,並且我故意拋出異常以測試事務的回滾。我希望消息被髮送回隊列的末尾,但它不會發生。有人可以幫助我嗎?當事務回滾時在隊列末尾重新排隊消息

回答

0

確保您已將setChannelTransacted設置爲true,並在SimpleMessageListenerContainer上設置RabbitTransactionManager。如果您發佈了您的配置,這將會很有幫助。另外,拋出一個異常時,確保你沒有拋出一個AMQPRejectAndDontRequeueException異常,因爲它顯式地告訴spring-amqp不管你的整體配置如何都不要求。

@Bean 
public SimpleMessageListenerContainer messageListenerContainer() 
{ 
    SimpleMessageListenerContainer result = new SimpleMessageListenerContainer(); 
    result.setConnectionFactory(connectionFactory); 
    result.setTransactionManager(rabbitTxManager); 
    result.setChannelTransacted(true); 
    result.setMessageListener(myMessageListener); 
    result.setConcurrentConsumers(1); 
    result.setQueues(myQueues); 
    return result; 
}