Example usage for org.springframework.amqp.rabbit.listener SimpleMessageListenerContainer isRunning

List of usage examples for org.springframework.amqp.rabbit.listener SimpleMessageListenerContainer isRunning

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.listener SimpleMessageListenerContainer isRunning.

Prototype

@Override
public final boolean isRunning() 

Source Link

Document

Determine whether this container is currently running, that is, whether it has been started and not stopped yet.

Usage

From source file:com.bbytes.zorba.messaging.rabbitmq.listener.impl.QueueNotificationHandler.java

@Override
public void handleZorbaRequest(ZorbaRequest request) throws MessagingException {
    if (request == null)
        return;/*from ww  w .  ja v  a  2 s.  c  om*/
    ZorbaData<String, Serializable> data = request.getData();
    if (data == null) {
        LOG.error("No data received along with request " + request.getId());
        return;
    }
    String queueName = (String) data.get("queueName");
    if (queueName == null || queueName.isEmpty()) {
        LOG.error("No Queue Name received along with request " + request.getId());
        return;
    }
    String event = (String) data.get("event");
    if (event.equals("CREATED")) {
        LOG.debug("New Queue Created. Adding Listener to that");
        final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setAutoStartup(false);
        container.setConnectionFactory(rabbitConnectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(
                new ZorbaRuntimeRequestHandlerImpl(jsonMessageConverter, zorbaRquestDelegationService));
        container.setConcurrentConsumers(3);
        queueListeners.put(queueName, container);
        // start the container
        Thread thread = new Thread() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                container.start();
            }
        };
        LOG.debug("Starting container for the queue " + queueName);
        thread.start();

    } else if (event.equals("DELETED")) {
        LOG.debug("Deleting container for the queue " + queueName);
        SimpleMessageListenerContainer container = queueListeners.get(queueName);
        if (container == null) {
            LOG.warn("SimpleMessageListenerContainer is null. Why?");
            return;
        }
        if (container.isRunning()) {
            container.stop();
        }
        container.destroy();
        queueListeners.remove(queueName);
    }

}

From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerLongTests.java

@Test
public void testAddQueuesAndStartInCycle() throws Exception {
    final SingleConnectionFactory connectionFactory = new SingleConnectionFactory("localhost");
    final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setMessageListener((MessageListener) message -> {
    });//  w  w  w. j a v a  2  s .c  o  m
    container.setConcurrentConsumers(2);
    container.afterPropertiesSet();

    RabbitAdmin admin = new RabbitAdmin(connectionFactory);
    for (int i = 0; i < 20; i++) {
        AnonymousQueue anonymousQueue = new AnonymousQueue();
        admin.declareQueue(anonymousQueue);
        container.addQueueNames(anonymousQueue.getName());
        if (!container.isRunning()) {
            container.start();
        }
    }

    int n = 0;
    while (n++ < 100 && container.getActiveConsumerCount() != 2) {
        Thread.sleep(100);
    }
    assertEquals(2, container.getActiveConsumerCount());
    container.stop();
    connectionFactory.destroy();
}

From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java

@SuppressWarnings("unchecked")
@Test//w  w w . ja v a  2 s .c om
public void testAddQueuesAndStartInCycle() throws Exception {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    Connection connection = mock(Connection.class);
    Channel channel1 = mock(Channel.class);
    when(channel1.isOpen()).thenReturn(true);
    when(connectionFactory.createConnection()).thenReturn(connection);
    when(connection.createChannel(false)).thenReturn(channel1);
    final AtomicInteger count = new AtomicInteger();
    doAnswer(invocation -> {
        Consumer cons = (Consumer) invocation.getArguments()[6];
        String consumerTag = "consFoo" + count.incrementAndGet();
        cons.handleConsumeOk(consumerTag);
        return consumerTag;
    }).when(channel1).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(),
            any(Consumer.class));

    final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setMessageListener((MessageListener) message -> {
    });
    container.afterPropertiesSet();

    for (int i = 0; i < 10; i++) {
        container.addQueueNames("foo" + i);
        if (!container.isRunning()) {
            container.start();
        }
    }
    container.stop();
}

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

@Test
public void testConsumerPropertiesWithUserInfrastructureNoBind() throws Exception {
    RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());
    Queue queue = new Queue("propsUser1.infra");
    admin.declareQueue(queue);/* w w  w . j  a v a  2s.c  o m*/
    DirectExchange exchange = new DirectExchange("propsUser1");
    admin.declareExchange(exchange);
    admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("foo"));

    RabbitTestBinder binder = getBinder();
    ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties();
    properties.getExtension().setDeclareExchange(false);
    properties.getExtension().setBindQueue(false);

    Binding<MessageChannel> consumerBinding = binder.bindConsumer("propsUser1", "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("/", exchange.getName());
    assertThat(bindings.size()).isEqualTo(1);
}

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  .  ja v a2s. 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   www. j av  a  2  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");
}