container.setQueueNames(testQueueName);
container.afterPropertiesSet();
container.start();
Message message = MessageBuilder.withBody("foo".getBytes())
.setContentType("text/plain")
.setContentEncoding("junk")
.build();
template.send("", testQueueName, message);
Message rejected = template.receive(dlq.getName());
int n = 0;
while (n++ < 100 && rejected == null) {
Thread.sleep(100);
rejected = template.receive(dlq.getName());
}
assertTrue("Message did not arrive in DLQ", n < 100);
assertEquals("foo", new String(rejected.getBody()));
// Verify that the exception strategy has access to the message
final AtomicReference<Message> failed = new AtomicReference<Message>();
ConditionalRejectingErrorHandler eh = new ConditionalRejectingErrorHandler(new FatalExceptionStrategy() {
@Override
public boolean isFatal(Throwable t) {
if (t instanceof ListenerExecutionFailedException) {
failed.set(((ListenerExecutionFailedException) t).getFailedMessage());
}
return t instanceof ListenerExecutionFailedException
&& t.getCause() instanceof MessageConversionException;
}
});
container.setErrorHandler(eh);
template.send("", testQueueName, message);
rejected = template.receive(dlq.getName());
n = 0;
while (n++ < 100 && rejected == null) {
Thread.sleep(100);
rejected = template.receive(dlq.getName());
}
assertTrue("Message did not arrive in DLQ", n < 100);
assertEquals("foo", new String(rejected.getBody()));
assertNotNull(failed.get());
container.stop();
Exception e = new ListenerExecutionFailedException("foo", new MessageConversionException("bar"));