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

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

Introduction

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

Prototype

public void setAutoStartup(boolean autoStartup) 

Source Link

Document

Set whether to automatically start the container after initialization.

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   w  ww.  j  av a2 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

private void testChangeConsumerCountGuts(boolean transacted) throws Exception {
    final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost");
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory);
    try {/* www.j  a va  2s . com*/
        container.setMessageListener(new MessageListenerAdapter(this));
        container.setQueueNames("foo");
        container.setAutoStartup(false);
        container.setConcurrentConsumers(2);
        container.setChannelTransacted(transacted);
        container.afterPropertiesSet();
        assertEquals(2, ReflectionTestUtils.getField(container, "concurrentConsumers"));
        container.start();
        waitForNConsumers(container, 2);
        container.setConcurrentConsumers(1);
        waitForNConsumers(container, 1);
        container.setMaxConcurrentConsumers(3);
        RabbitTemplate template = new RabbitTemplate(singleConnectionFactory);
        for (int i = 0; i < 20; i++) {
            template.convertAndSend("foo", "foo");
        }
        waitForNConsumers(container, 2); // increased consumers due to work
        waitForNConsumers(container, 1, 20000); // should stop the extra consumer after 10 seconds idle
        container.setConcurrentConsumers(3);
        waitForNConsumers(container, 3);
        container.stop();
        waitForNConsumers(container, 0);
        singleConnectionFactory.destroy();
    } finally {
        container.stop();
    }
}

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

@Test
public void testDefaultConsumerCount() throws Exception {
    final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost");
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory);
    container.setMessageListener(new MessageListenerAdapter(this));
    container.setQueueNames("foo");
    container.setAutoStartup(false);
    container.afterPropertiesSet();/*  ww  w.  jav a2 s  .  c o m*/
    assertEquals(1, ReflectionTestUtils.getField(container, "concurrentConsumers"));
    container.stop();
    singleConnectionFactory.destroy();
}