packagehuangbowen.net;importhuangbowen.net.jms.MessageSender;importorg.junit.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.jms.core.BrowserCallback;importorg.springframework.jms.core.JmsTemplate;importorg.springframework.test.annotation.DirtiesContext;importorg.springframework.test.context.ContextConfiguration;importorg.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;importjavax.jms.JMSException;importjavax.jms.QueueBrowser;importjavax.jms.Session;importjava.util.Enumeration;importstaticorg.hamcrest.core.Is.is;importstaticorg.junit.Assert.assertThat;@ContextConfiguration(locations={"/retry/activeMQConnection.xml"})@DirtiesContextpublicclassReDeliveryFunctionIntegrationTestextendsAbstractJUnit4SpringContextTests{privatefinalstaticStringDLQ="DLQ.bar";@AutowiredpublicJmsTemplatejmsTemplate;@AutowiredpublicMessageSendermessageSender;privateintgetMessagesInDLQ(){returnjmsTemplate.browse(DLQ,newBrowserCallback<Integer>(){@OverridepublicIntegerdoInJms(Sessionsession,QueueBrowserbrowser)throwsJMSException{Enumerationmessages=browser.getEnumeration();inttotal=0;while(messages.hasMoreElements()){messages.nextElement();total++;}returntotal;}});}@TestpublicvoidshouldRetryIfExceptionHappened()throwsException{assertThat(getMessagesInDLQ(),is(0));messageSender.send("this is a message");Thread.sleep(5000);assertThat(getMessagesInDLQ(),is(1));}}
send: this is a message
Received: this is a message
there is JMS exception: process failed
Received: this is a message
there is JMS exception: process failed
Received: this is a message
there is JMS exception: process failed
Received: this is a message
there is JMS exception: process failed
Received: this is a message
there is JMS exception: process failed
Process finished with exit code 0