@Test
public void testWithId() throws Exception {
// 2 messsages; each retried twice by retry interceptor
this.latch = new CountDownLatch(6);
ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("retry-context.xml", this.getClass());
RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
ConnectionFactory connectionFactory = ctx.getBean(ConnectionFactory.class);
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
container.setMessageListener(new MessageListenerAdapter(new POJO()));
container.setQueueNames("retry.test.queue");
StatefulRetryOperationsInterceptorFactoryBean fb = new StatefulRetryOperationsInterceptorFactoryBean();
// use an external template so we can share his cache
RetryTemplate retryTemplate = new RetryTemplate();
RetryContextCache cache = new MapRetryContextCache();
retryTemplate.setRetryContextCache(cache);
fb.setRetryOperations(retryTemplate);
fb.setMessageRecoverer(new RejectAndDontRequeueRecoverer());
// give him a reference to the retry cache so he can clean it up
MissingMessageIdAdvice missingIdAdvice = new MissingMessageIdAdvice(cache);
Advice retryInterceptor = fb.getObject();
// add both advices
container.setAdviceChain(new Advice[] {missingIdAdvice, retryInterceptor});
container.start();
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType("text/plain");
messageProperties.setMessageId("foo");
Message message = new Message("Hello, world!".getBytes(), messageProperties);
template.send("retry.test.exchange", "retry.test.binding", message);
template.send("retry.test.exchange", "retry.test.binding", message);
assertTrue(latch.await(10, TimeUnit.SECONDS));
Thread.sleep(2000);
assertEquals(0, ((Map) new DirectFieldAccessor(cache).getPropertyValue("map")).size());
container.stop();
ctx.close();