Example usage for org.springframework.amqp.rabbit.connection SingleConnectionFactory destroy

List of usage examples for org.springframework.amqp.rabbit.connection SingleConnectionFactory destroy

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.connection SingleConnectionFactory destroy.

Prototype

@Override
public final void destroy() 

Source Link

Document

Close the underlying shared connection.

Usage

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

@Test
public void testNoFailOnStartupWithMissingBroker() throws Exception {
    SingleConnectionFactory connectionFactory = new SingleConnectionFactory("foo");
    connectionFactory.setPort(434343);//  w  w w  .  j ava2s  .  c om
    GenericApplicationContext applicationContext = new GenericApplicationContext();
    applicationContext.getBeanFactory().registerSingleton("foo", new Queue("queue"));
    RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
    rabbitAdmin.setApplicationContext(applicationContext);
    rabbitAdmin.setAutoStartup(true);
    rabbitAdmin.afterPropertiesSet();
    connectionFactory.destroy();
}

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

@Test
public void testFailOnFirstUseWithMissingBroker() throws Exception {
    SingleConnectionFactory connectionFactory = new SingleConnectionFactory("localhost");
    connectionFactory.setPort(434343);/* w w w .j a va2 s .c o m*/
    GenericApplicationContext applicationContext = new GenericApplicationContext();
    applicationContext.getBeanFactory().registerSingleton("foo", new Queue("queue"));
    RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
    rabbitAdmin.setApplicationContext(applicationContext);
    rabbitAdmin.setAutoStartup(true);
    rabbitAdmin.afterPropertiesSet();
    exception.expect(IllegalArgumentException.class);
    rabbitAdmin.declareQueue();
    connectionFactory.destroy();
}

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

@Test
public void testProperties() throws Exception {
    SingleConnectionFactory connectionFactory = new SingleConnectionFactory();
    connectionFactory.setHost("localhost");
    RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
    String queueName = "test.properties." + System.currentTimeMillis();
    try {//  w ww .  j av  a  2s  .c om
        rabbitAdmin.declareQueue(new Queue(queueName));
        new RabbitTemplate(connectionFactory).convertAndSend(queueName, "foo");
        int n = 0;
        while (n++ < 100 && messageCount(rabbitAdmin, queueName) == 0) {
            Thread.sleep(100);
        }
        assertTrue("Message count = 0", n < 100);
        Channel channel = connectionFactory.createConnection().createChannel(false);
        DefaultConsumer consumer = new DefaultConsumer(channel);
        channel.basicConsume(queueName, true, consumer);
        n = 0;
        while (n++ < 100 && messageCount(rabbitAdmin, queueName) > 0) {
            Thread.sleep(100);
        }
        assertTrue("Message count > 0", n < 100);
        Properties props = rabbitAdmin.getQueueProperties(queueName);
        assertNotNull(props.get(RabbitAdmin.QUEUE_CONSUMER_COUNT));
        assertEquals(1, props.get(RabbitAdmin.QUEUE_CONSUMER_COUNT));
        channel.close();
    } finally {
        rabbitAdmin.deleteQueue(queueName);
        connectionFactory.destroy();
    }
}

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

@Test
public void testTemporaryLogs() throws Exception {
    SingleConnectionFactory connectionFactory = new SingleConnectionFactory();
    connectionFactory.setHost("localhost");
    RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
    try {/*from w  w w  . j  a va 2 s  .com*/
        ApplicationContext ctx = mock(ApplicationContext.class);
        Map<String, Queue> queues = new HashMap<String, Queue>();
        queues.put("nonDurQ", new Queue("testq.nonDur", false, false, false));
        queues.put("adQ", new Queue("testq.ad", true, false, true));
        queues.put("exclQ", new Queue("testq.excl", true, true, false));
        queues.put("allQ", new Queue("testq.all", false, true, true));
        when(ctx.getBeansOfType(Queue.class)).thenReturn(queues);
        Map<String, Exchange> exchanges = new HashMap<String, Exchange>();
        exchanges.put("nonDurEx", new DirectExchange("testex.nonDur", false, false));
        exchanges.put("adEx", new DirectExchange("testex.ad", true, true));
        exchanges.put("allEx", new DirectExchange("testex.all", false, true));
        when(ctx.getBeansOfType(Exchange.class)).thenReturn(exchanges);
        rabbitAdmin.setApplicationContext(ctx);
        rabbitAdmin.afterPropertiesSet();
        Log logger = spy(TestUtils.getPropertyValue(rabbitAdmin, "logger", Log.class));
        doReturn(true).when(logger).isInfoEnabled();
        doAnswer(new DoesNothing()).when(logger).info(anyString());
        new DirectFieldAccessor(rabbitAdmin).setPropertyValue("logger", logger);
        connectionFactory.createConnection().close(); // force declarations
        ArgumentCaptor<String> log = ArgumentCaptor.forClass(String.class);
        verify(logger, times(7)).info(log.capture());
        List<String> logs = log.getAllValues();
        Collections.sort(logs);
        assertThat(logs.get(0), Matchers.containsString("(testex.ad) durable:true, auto-delete:true"));
        assertThat(logs.get(1), Matchers.containsString("(testex.all) durable:false, auto-delete:true"));
        assertThat(logs.get(2), Matchers.containsString("(testex.nonDur) durable:false, auto-delete:false"));
        assertThat(logs.get(3),
                Matchers.containsString("(testq.ad) durable:true, auto-delete:true, exclusive:false"));
        assertThat(logs.get(4),
                Matchers.containsString("(testq.all) durable:false, auto-delete:true, exclusive:true"));
        assertThat(logs.get(5),
                Matchers.containsString("(testq.excl) durable:true, auto-delete:false, exclusive:true"));
        assertThat(logs.get(6),
                Matchers.containsString("(testq.nonDur) durable:false, auto-delete:false, exclusive:false"));
    } finally {
        cleanQueuesAndExchanges(rabbitAdmin);
        connectionFactory.destroy();
    }
}

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 {//from  ww w .  ja  va 2  s  .c  om
        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.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 ww  .j  a  v a2s  .com*/
    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

@Test
public void testChannelTransactedOverriddenWhenTxManager() throws Exception {
    final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost");
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory);
    container.setMessageListener(new MessageListenerAdapter(this));
    container.setQueueNames("foo");
    container.setChannelTransacted(false);
    container.setTransactionManager(new TestTransactionManager());
    container.afterPropertiesSet();/*from ww w.ja  va2s.  co  m*/
    assertTrue(TestUtils.getPropertyValue(container, "transactional", Boolean.class));
    container.stop();
    singleConnectionFactory.destroy();
}

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

@Test
public void testInconsistentTransactionConfiguration() throws Exception {
    final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost");
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory);
    container.setMessageListener(new MessageListenerAdapter(this));
    container.setQueueNames("foo");
    container.setChannelTransacted(false);
    container.setAcknowledgeMode(AcknowledgeMode.NONE);
    container.setTransactionManager(new TestTransactionManager());
    expectedException.expect(IllegalStateException.class);
    container.afterPropertiesSet();//from  www  .  j a  v a  2s.com
    container.stop();
    singleConnectionFactory.destroy();
}

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

@Test
public void testInconsistentAcknowledgeConfiguration() throws Exception {
    final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost");
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory);
    container.setMessageListener(new MessageListenerAdapter(this));
    container.setQueueNames("foo");
    container.setChannelTransacted(true);
    container.setAcknowledgeMode(AcknowledgeMode.NONE);
    expectedException.expect(IllegalStateException.class);
    container.afterPropertiesSet();//  ww  w . j a  v a  2s. co m
    container.stop();
    singleConnectionFactory.destroy();
}

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);/*from  ww  w . ja  v  a 2 s. com*/
    container.afterPropertiesSet();
    assertEquals(1, ReflectionTestUtils.getField(container, "concurrentConsumers"));
    container.stop();
    singleConnectionFactory.destroy();
}