Source:
public void testDeclareDelayedExchange() throws Exception { DirectExchange exchange = new DirectExchange("test.delayed.exchange"); exchange.setDelayed(true); Queue queue = new Queue(UUID.randomUUID().toString(), true, false, false); Binding binding = new Binding(queue.getName(), DestinationType.QUEUE, exchange.getName(), queue.getName(), null); try { this.rabbitAdmin.declareExchange(exchange); } catch (AmqpIOException e) { if (RabbitUtils.isExchangeDeclarationFailure(e) && e.getCause().getCause().getMessage().contains("invalid exchange type 'x-delayed-message'")) { Assume.assumeTrue("Broker does not have the delayed message exchange plugin installed", false); } else { throw e; } } this.rabbitAdmin.declareQueue(queue); this.rabbitAdmin.declareBinding(binding); RabbitTemplate template = new RabbitTemplate(this.connectionFactory); template.setReceiveTimeout(10000); template.convertAndSend(exchange.getName(), queue.getName(), "foo", new MessagePostProcessor() { @Override public Message postProcessMessage(Message message) throws AmqpException { message.getMessageProperties().setXDelay(1000); return message; } }); MessageProperties properties = new MessageProperties(); properties.setXDelay(1000); template.send(exchange.getName(), queue.getName(), MessageBuilder.withBody("foo".getBytes()).andProperties(properties).build()); long t1 = System.currentTimeMillis(); assertNotNull(template.receive(queue.getName())); assertNotNull(template.receive(queue.getName())); assertTrue(System.currentTimeMillis() - t1 > 999); this.rabbitAdmin.deleteQueue(queue.getName()); this.rabbitAdmin.deleteExchange(exchange.getName()); }

Target:
public void testDeclareDelayedExchange() throws Exception { DirectExchange exchange = new DirectExchange("test.delayed.exchange"); exchange.setDelayed(true); Queue queue = new Queue(UUID.randomUUID().toString(), true, false, false); Binding binding = new Binding(queue.getName(), DestinationType.QUEUE, exchange.getName(), queue.getName(), null); try { this.rabbitAdmin.declareExchange(exchange); } catch (AmqpIOException e) { if (RabbitUtils.isExchangeDeclarationFailure(e) && e.getCause().getCause().getMessage().contains("exchange type 'x-delayed-message'")) { Assume.assumeTrue("Broker does not have the delayed message exchange plugin installed", false); } else { throw e; } } this.rabbitAdmin.declareQueue(queue); this.rabbitAdmin.declareBinding(binding); RabbitTemplate template = new RabbitTemplate(this.connectionFactory); template.setReceiveTimeout(10000); template.convertAndSend(exchange.getName(), queue.getName(), "foo", new MessagePostProcessor() { @Override public Message postProcessMessage(Message message) throws AmqpException { message.getMessageProperties().setDelay(1000); return message; } }); MessageProperties properties = new MessageProperties(); properties.setDelay(500); template.send(exchange.getName(), queue.getName(), MessageBuilder.withBody("foo".getBytes()).andProperties(properties).build()); long t1 = System.currentTimeMillis(); Message received = template.receive(queue.getName()); assertNotNull(received); assertEquals(Integer.valueOf(500), received.getMessageProperties().getReceivedDelay()); received = template.receive(queue.getName()); assertNotNull(received); assertEquals(Integer.valueOf(1000), received.getMessageProperties().getReceivedDelay()); assertTrue(System.currentTimeMillis() - t1 > 999); this.rabbitAdmin.deleteQueue(queue.getName()); this.rabbitAdmin.deleteExchange(exchange.getName()); }