use of org.springframework.amqp.rabbit.connection.AutoRecoverConnectionNotCurrentlyOpenException in project spring-amqp by spring-projects.
the class RabbitAdminIntegrationTests method testDeclareDelayedExchange.
@Test
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);
String exchangeName = exchange.getName();
Binding binding = new Binding(queue.getName(), DestinationType.QUEUE, exchangeName, 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'")) {
return;
} else {
throw e;
}
} catch (@SuppressWarnings("unused") AutoRecoverConnectionNotCurrentlyOpenException e) {
return;
}
this.rabbitAdmin.declareQueue(queue);
this.rabbitAdmin.declareBinding(binding);
RabbitTemplate template = new RabbitTemplate(this.connectionFactory);
template.setReceiveTimeout(10000);
template.convertAndSend(exchangeName, queue.getName(), "foo", message -> {
message.getMessageProperties().setDelay(1000);
return message;
});
MessageProperties properties = new MessageProperties();
properties.setDelay(500);
template.send(exchangeName, queue.getName(), MessageBuilder.withBody("foo".getBytes()).andProperties(properties).build());
long t1 = System.currentTimeMillis();
Message received = template.receive(queue.getName());
assertThat(received).isNotNull();
assertThat(received.getMessageProperties().getReceivedDelay()).isEqualTo(Integer.valueOf(500));
received = template.receive(queue.getName());
assertThat(received).isNotNull();
assertThat(received.getMessageProperties().getReceivedDelay()).isEqualTo(Integer.valueOf(1000));
assertThat(System.currentTimeMillis() - t1).isGreaterThan(950L);
ExchangeInfo exchange2 = getExchange(exchangeName);
assertThat(exchange2.getArguments().get("x-delayed-type")).isEqualTo(ExchangeTypes.DIRECT);
assertThat(exchange2.getType()).isEqualTo("x-delayed-message");
this.rabbitAdmin.deleteQueue(queue.getName());
this.rabbitAdmin.deleteExchange(exchangeName);
}
Aggregations