Example usage for org.springframework.amqp.core ExchangeTypes DIRECT

List of usage examples for org.springframework.amqp.core ExchangeTypes DIRECT

Introduction

In this page you can find the example usage for org.springframework.amqp.core ExchangeTypes DIRECT.

Prototype

String DIRECT

To view the source code for org.springframework.amqp.core ExchangeTypes DIRECT.

Click Source Link

Document

Direct exchange.

Usage

From source file:com.github.larsq.spring.embeddedamqp.SimpleAmqpMessageContainer.java

protected Address defineAddress(String exchangeName, String routingKey) {
    if (exchangeName != null && routingKey != null) {
        return new Address(ExchangeTypes.DIRECT, exchangeName, routingKey);
    }//from w w w.j a  va 2s  . c  om

    if (exchangeName == null) {
        return new Address(ExchangeTypes.TOPIC, null, routingKey);
    }

    if (routingKey == null) {
        return new Address(ExchangeTypes.FANOUT, exchangeName, null);
    }

    //both are null
    throw new NullPointerException("both exchange and routing key cannot be null");
}

From source file:org.springframework.amqp.rabbit.annotation.RabbitListenerAnnotationBeanPostProcessor.java

private void declareExchangeAndBinding(QueueBinding binding, String queueName) {
    org.springframework.amqp.rabbit.annotation.Exchange bindingExchange = binding.exchange();
    String exchangeName = resolveExpressionAsString(bindingExchange.value(), "@Exchange.exchange");
    Assert.isTrue(StringUtils.hasText(exchangeName), "Exchange name required; binding queue " + queueName);
    String exchangeType = resolveExpressionAsString(bindingExchange.type(), "@Exchange.type");
    String routingKey = resolveExpressionAsString(binding.key(), "@QueueBinding.key");
    Exchange exchange;// w  ww  .  j  av a 2  s  .c o  m
    Binding actualBinding;
    if (exchangeType.equals(ExchangeTypes.DIRECT)) {
        exchange = directExchange(bindingExchange, exchangeName);
        actualBinding = new Binding(queueName, DestinationType.QUEUE, exchangeName, routingKey,
                resolveArguments(binding.arguments()));
    } else if (exchangeType.equals(ExchangeTypes.FANOUT)) {
        exchange = fanoutExchange(bindingExchange, exchangeName);
        actualBinding = new Binding(queueName, DestinationType.QUEUE, exchangeName, "",
                resolveArguments(binding.arguments()));
    } else if (exchangeType.equals(ExchangeTypes.TOPIC)) {
        exchange = topicExchange(bindingExchange, exchangeName);
        actualBinding = new Binding(queueName, DestinationType.QUEUE, exchangeName, routingKey,
                resolveArguments(binding.arguments()));
    } else if (exchangeType.equals(ExchangeTypes.HEADERS)) {
        exchange = headersExchange(bindingExchange, exchangeName);
        actualBinding = new Binding(queueName, DestinationType.QUEUE, exchangeName, routingKey,
                resolveArguments(binding.arguments()));
    } else {
        throw new BeanInitializationException("Unexpected exchange type: " + exchangeType);
    }
    AbstractExchange abstractExchange = (AbstractExchange) exchange;
    abstractExchange.setInternal(resolveExpressionAsBoolean(bindingExchange.internal()));
    abstractExchange.setIgnoreDeclarationExceptions(
            resolveExpressionAsBoolean(bindingExchange.ignoreDeclarationExceptions()));
    ((AbstractDeclarable) actualBinding)
            .setIgnoreDeclarationExceptions(resolveExpressionAsBoolean(binding.ignoreDeclarationExceptions()));
    ((ConfigurableBeanFactory) this.beanFactory).registerSingleton(exchangeName + ++this.increment, exchange);
    ((ConfigurableBeanFactory) this.beanFactory)
            .registerSingleton(exchangeName + "." + queueName + ++this.increment, actualBinding);
}

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();/*from w  w  w.ja  va 2 s. co  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);// w w w  . j a  v  a2  s .  c  o 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");
}