Example usage for org.springframework.amqp.core Exchange isDurable

List of usage examples for org.springframework.amqp.core Exchange isDurable

Introduction

In this page you can find the example usage for org.springframework.amqp.core Exchange isDurable.

Prototype

boolean isDurable();

Source Link

Document

A durable exchange will survive a server restart.

Usage

From source file:org.springframework.amqp.rabbit.core.RabbitAdmin.java

/**
 * Declares all the exchanges, queues and bindings in the enclosing application context, if any. It should be safe
 * (but unnecessary) to call this method more than once.
 *///ww w  . ja  v a  2  s . com
public void initialize() {

    if (this.applicationContext == null) {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug(
                    "no ApplicationContext has been set, cannot auto-declare Exchanges, Queues, and Bindings");
        }
        return;
    }

    logger.debug("Initializing declarations");
    final Collection<Exchange> exchanges = applicationContext.getBeansOfType(Exchange.class).values();
    final Collection<Queue> queues = applicationContext.getBeansOfType(Queue.class).values();
    final Collection<Binding> bindings = applicationContext.getBeansOfType(Binding.class).values();

    for (Exchange exchange : exchanges) {
        if (!exchange.isDurable()) {
            logger.warn("Auto-declaring a non-durable Exchange (" + exchange.getName()
                    + "). It will be deleted by the broker if it shuts down, and can be redeclared by closing and reopening the connection.");
        }
        if (exchange.isAutoDelete()) {
            logger.warn("Auto-declaring an auto-delete Exchange (" + exchange.getName()
                    + "). It will be deleted by the broker if not in use (if all bindings are deleted), but will only be redeclared if the connection is closed and reopened.");
        }
    }

    for (Queue queue : queues) {
        if (!queue.isDurable()) {
            logger.warn("Auto-declaring a non-durable Queue (" + queue.getName()
                    + "). It will be redeclared if the broker stops and is restarted while the connection factory is alive, but all messages will be lost.");
        }
        if (queue.isAutoDelete()) {
            logger.warn("Auto-declaring an auto-delete Queue (" + queue.getName()
                    + "). It will be deleted by the broker if not in use, and all messages will be lost.  Redeclared when the connection is closed and reopened.");
        }
        if (queue.isExclusive()) {
            logger.warn("Auto-declaring an exclusive Queue (" + queue.getName()
                    + "). It cannot be accessed by consumers on another connection, and will be redeclared if the connection is reopened.");
        }
    }

    rabbitTemplate.execute(new ChannelCallback<Object>() {
        public Object doInRabbit(Channel channel) throws Exception {
            declareExchanges(channel, exchanges.toArray(new Exchange[exchanges.size()]));
            declareQueues(channel, queues.toArray(new Queue[queues.size()]));
            declareBindings(channel, bindings.toArray(new Binding[bindings.size()]));
            return null;
        }
    });
    logger.debug("Declarations finished");

}

From source file:org.springframework.amqp.rabbit.core.RabbitAdmin.java

private void declareExchanges(final Channel channel, final Exchange... exchanges) throws IOException {
    for (final Exchange exchange : exchanges) {
        if (logger.isDebugEnabled()) {
            logger.debug("declaring Exchange '" + exchange.getName() + "'");
        }/*  www .j  a va  2s  . co  m*/

        if (!isDeclaringDefaultExchange(exchange)) {
            channel.exchangeDeclare(exchange.getName(), exchange.getType(), exchange.isDurable(),
                    exchange.isAutoDelete(), exchange.getArguments());
        }
    }
}

From source file:org.springframework.cloud.stream.binder.rabbit.RabbitBinderTests.java

@Test
public void testConsumerPropertiesWithUserInfrastructureCustomExchangeAndRK() throws Exception {
    RabbitTestBinder binder = getBinder();
    ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties();
    properties.getExtension().setExchangeType(ExchangeTypes.DIRECT);
    properties.getExtension().setBindingRoutingKey("foo");
    //      properties.getExtension().setDelayedExchange(true); // requires delayed message exchange plugin; tested locally

    Binding<MessageChannel> consumerBinding = binder.bindConsumer("propsUser2", "infra",
            createBindableChannel("input", new BindingProperties()), properties);
    Lifecycle endpoint = extractEndpoint(consumerBinding);
    SimpleMessageListenerContainer container = TestUtils.getPropertyValue(endpoint, "messageListenerContainer",
            SimpleMessageListenerContainer.class);
    assertThat(container.isRunning()).isTrue();
    consumerBinding.unbind();/* w w w  .  j  a v  a2  s . c o m*/
    assertThat(container.isRunning()).isFalse();
    RabbitManagementTemplate rmt = new RabbitManagementTemplate();
    List<org.springframework.amqp.core.Binding> bindings = rmt.getBindingsForExchange("/", "propsUser2");
    int n = 0;
    while (n++ < 100 && bindings == null || bindings.size() < 1) {
        Thread.sleep(100);
        bindings = rmt.getBindingsForExchange("/", "propsUser2");
    }
    assertThat(bindings.size()).isEqualTo(1);
    assertThat(bindings.get(0).getExchange()).isEqualTo("propsUser2");
    assertThat(bindings.get(0).getDestination()).isEqualTo("propsUser2.infra");
    assertThat(bindings.get(0).getRoutingKey()).isEqualTo("foo");

    //      // TODO: AMQP-696
    //      // Exchange exchange = rmt.getExchange("propsUser2");
    //      ExchangeInfo ei = rmt.getClient().getExchange("/", "propsUser2"); // requires delayed message exchange plugin
    //      assertThat(ei.getType()).isEqualTo("x-delayed-message");
    //      assertThat(ei.getArguments().get("x-delayed-type")).isEqualTo("direct");

    Exchange exchange = rmt.getExchange("propsUser2");
    while (n++ < 100 && exchange == null) {
        Thread.sleep(100);
        exchange = rmt.getExchange("propsUser2");
    }
    assertThat(exchange).isInstanceOf(DirectExchange.class);
    assertThat(exchange.isDurable()).isEqualTo(true);
    assertThat(exchange.isAutoDelete()).isEqualTo(false);
}

From source file:org.springframework.cloud.stream.binder.rabbit.RabbitBinderTests.java

@Test
public void testConsumerPropertiesWithUserInfrastructureCustomQueueArgs() throws Exception {
    RabbitTestBinder binder = getBinder();
    ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties();
    RabbitConsumerProperties extProps = properties.getExtension();
    extProps.setExchangeType(ExchangeTypes.DIRECT);
    extProps.setExchangeDurable(false);//from   w  ww. j a  v  a2s  .co  m
    extProps.setExchangeAutoDelete(true);
    extProps.setBindingRoutingKey("foo");
    extProps.setExpires(30_000);
    extProps.setLazy(true);
    extProps.setMaxLength(10_000);
    extProps.setMaxLengthBytes(100_000);
    extProps.setMaxPriority(10);
    extProps.setTtl(2_000);
    extProps.setAutoBindDlq(true);
    extProps.setDeadLetterQueueName("customDLQ");
    extProps.setDeadLetterExchange("customDLX");
    extProps.setDeadLetterRoutingKey("customDLRK");
    extProps.setDlqDeadLetterExchange("propsUser3");
    extProps.setDlqDeadLetterRoutingKey("propsUser3");
    extProps.setDlqExpires(60_000);
    extProps.setDlqLazy(true);
    extProps.setDlqMaxLength(20_000);
    extProps.setDlqMaxLengthBytes(40_000);
    extProps.setDlqMaxPriority(8);
    extProps.setDlqTtl(1_000);

    Binding<MessageChannel> consumerBinding = binder.bindConsumer("propsUser3", "infra",
            createBindableChannel("input", new BindingProperties()), properties);
    Lifecycle endpoint = extractEndpoint(consumerBinding);
    SimpleMessageListenerContainer container = TestUtils.getPropertyValue(endpoint, "messageListenerContainer",
            SimpleMessageListenerContainer.class);
    assertThat(container.isRunning()).isTrue();
    consumerBinding.unbind();
    assertThat(container.isRunning()).isFalse();
    RabbitManagementTemplate rmt = new RabbitManagementTemplate();
    List<org.springframework.amqp.core.Binding> bindings = rmt.getBindingsForExchange("/", "propsUser3");
    int n = 0;
    while (n++ < 100 && bindings == null || bindings.size() < 1) {
        Thread.sleep(100);
        bindings = rmt.getBindingsForExchange("/", "propsUser3");
    }
    assertThat(bindings.size()).isEqualTo(1);
    assertThat(bindings.get(0).getExchange()).isEqualTo("propsUser3");
    assertThat(bindings.get(0).getDestination()).isEqualTo("propsUser3.infra");
    assertThat(bindings.get(0).getRoutingKey()).isEqualTo("foo");

    Exchange exchange = rmt.getExchange("propsUser3");
    n = 0;
    while (n++ < 100 && exchange == null) {
        Thread.sleep(100);
        exchange = rmt.getExchange("propsUser3");
    }
    assertThat(exchange).isInstanceOf(DirectExchange.class);
    assertThat(exchange.isDurable()).isEqualTo(false);
    assertThat(exchange.isAutoDelete()).isEqualTo(true);

    //      Queue queue = rmt.getQueue("propsUser3"); AMQP-698
    QueueInfo queue = rmt.getClient().getQueue("/", "propsUser3.infra");
    n = 0;
    while (n++ < 100 && queue == null) {
        Thread.sleep(100);
        queue = rmt.getClient().getQueue("/", "propsUser3.infra");
    }
    assertThat(queue).isNotNull();
    Map<String, Object> args = queue.getArguments();
    assertThat(args.get("x-expires")).isEqualTo(30_000);
    assertThat(args.get("x-max-length")).isEqualTo(10_000);
    assertThat(args.get("x-max-length-bytes")).isEqualTo(100_000);
    assertThat(args.get("x-max-priority")).isEqualTo(10);
    assertThat(args.get("x-message-ttl")).isEqualTo(2_000);
    assertThat(args.get("x-dead-letter-exchange")).isEqualTo("customDLX");
    assertThat(args.get("x-dead-letter-routing-key")).isEqualTo("customDLRK");
    assertThat(args.get("x-queue-mode")).isEqualTo("lazy");

    queue = rmt.getClient().getQueue("/", "customDLQ");

    n = 0;
    while (n++ < 100 && queue == null) {
        Thread.sleep(100);
        queue = rmt.getClient().getQueue("/", "customDLQ");
    }
    assertThat(queue).isNotNull();
    args = queue.getArguments();
    assertThat(args.get("x-expires")).isEqualTo(60_000);
    assertThat(args.get("x-max-length")).isEqualTo(20_000);
    assertThat(args.get("x-max-length-bytes")).isEqualTo(40_000);
    assertThat(args.get("x-max-priority")).isEqualTo(8);
    assertThat(args.get("x-message-ttl")).isEqualTo(1_000);
    assertThat(args.get("x-dead-letter-exchange")).isEqualTo("propsUser3");
    assertThat(args.get("x-dead-letter-routing-key")).isEqualTo("propsUser3");
    assertThat(args.get("x-queue-mode")).isEqualTo("lazy");
}