Search in sources :

Example 1 with AutoRecoverConnectionNotCurrentlyOpenException

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);
}
Also used : Binding(org.springframework.amqp.core.Binding) Message(org.springframework.amqp.core.Message) MessageProperties(org.springframework.amqp.core.MessageProperties) DirectExchange(org.springframework.amqp.core.DirectExchange) AutoRecoverConnectionNotCurrentlyOpenException(org.springframework.amqp.rabbit.connection.AutoRecoverConnectionNotCurrentlyOpenException) AmqpIOException(org.springframework.amqp.AmqpIOException) ExchangeInfo(com.rabbitmq.http.client.domain.ExchangeInfo) Queue(org.springframework.amqp.core.Queue) AnonymousQueue(org.springframework.amqp.core.AnonymousQueue) Test(org.junit.jupiter.api.Test)

Aggregations

ExchangeInfo (com.rabbitmq.http.client.domain.ExchangeInfo)1 Test (org.junit.jupiter.api.Test)1 AmqpIOException (org.springframework.amqp.AmqpIOException)1 AnonymousQueue (org.springframework.amqp.core.AnonymousQueue)1 Binding (org.springframework.amqp.core.Binding)1 DirectExchange (org.springframework.amqp.core.DirectExchange)1 Message (org.springframework.amqp.core.Message)1 MessageProperties (org.springframework.amqp.core.MessageProperties)1 Queue (org.springframework.amqp.core.Queue)1 AutoRecoverConnectionNotCurrentlyOpenException (org.springframework.amqp.rabbit.connection.AutoRecoverConnectionNotCurrentlyOpenException)1