Example usage for org.springframework.amqp.rabbit.connection CachingConnectionFactory getCacheProperties

List of usage examples for org.springframework.amqp.rabbit.connection CachingConnectionFactory getCacheProperties

Introduction

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

Prototype

@ManagedAttribute
    public Properties getCacheProperties() 

Source Link

Usage

From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java

@Test
public void testWithConnectionFactoryCachedConnection() throws Exception {
    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);

    final List<com.rabbitmq.client.Connection> mockConnections = new ArrayList<com.rabbitmq.client.Connection>();
    final List<Channel> mockChannels = new ArrayList<Channel>();

    doAnswer(new Answer<com.rabbitmq.client.Connection>() {

        private int connectionNumber;

        @Override//  w  w w .  j a  v a2 s.co  m
        public com.rabbitmq.client.Connection answer(InvocationOnMock invocation) throws Throwable {
            com.rabbitmq.client.Connection connection = mock(com.rabbitmq.client.Connection.class);
            doAnswer(new Answer<Channel>() {

                private int channelNumber;

                @Override
                public Channel answer(InvocationOnMock invocation) throws Throwable {
                    Channel channel = mock(Channel.class);
                    when(channel.isOpen()).thenReturn(true);
                    int channelNumnber = ++this.channelNumber;
                    when(channel.toString())
                            .thenReturn("mockChannel" + connectionNumber + ":" + channelNumnber);
                    mockChannels.add(channel);
                    return channel;
                }
            }).when(connection).createChannel();
            int connectionNumber = ++this.connectionNumber;
            when(connection.toString()).thenReturn("mockConnection" + connectionNumber);
            when(connection.isOpen()).thenReturn(true);
            mockConnections.add(connection);
            return connection;
        }
    }).when(mockConnectionFactory).newConnection(any(ExecutorService.class), anyString());

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setExecutor(mock(ExecutorService.class));
    ccf.setCacheMode(CacheMode.CONNECTION);
    ccf.afterPropertiesSet();

    Set<?> allocatedConnections = TestUtils.getPropertyValue(ccf, "allocatedConnections", Set.class);
    assertEquals(0, allocatedConnections.size());
    BlockingQueue<?> idleConnections = TestUtils.getPropertyValue(ccf, "idleConnections", BlockingQueue.class);
    assertEquals(0, idleConnections.size());

    final AtomicReference<com.rabbitmq.client.Connection> createNotification = new AtomicReference<com.rabbitmq.client.Connection>();
    final AtomicReference<com.rabbitmq.client.Connection> closedNotification = new AtomicReference<com.rabbitmq.client.Connection>();
    ccf.setConnectionListeners(Collections.singletonList(new ConnectionListener() {

        @Override
        public void onCreate(Connection connection) {
            assertNull(createNotification.get());
            createNotification.set(targetDelegate(connection));
        }

        @Override
        public void onClose(Connection connection) {
            assertNull(closedNotification.get());
            closedNotification.set(targetDelegate(connection));
        }
    }));

    Connection con1 = ccf.createConnection();
    verifyConnectionIs(mockConnections.get(0), con1);
    assertEquals(1, allocatedConnections.size());
    assertEquals(0, idleConnections.size());
    assertNotNull(createNotification.get());
    assertSame(mockConnections.get(0), createNotification.getAndSet(null));

    Channel channel1 = con1.createChannel(false);
    verifyChannelIs(mockChannels.get(0), channel1);
    channel1.close();
    //AMQP-358
    verify(mockChannels.get(0), never()).close();

    con1.close(); // should be ignored, and placed into connection cache.
    verify(mockConnections.get(0), never()).close();
    assertEquals(1, allocatedConnections.size());
    assertEquals(1, idleConnections.size());
    assertNull(closedNotification.get());

    /*
     * will retrieve same connection that was just put into cache, and reuse single channel from cache as well
     */
    Connection con2 = ccf.createConnection();
    verifyConnectionIs(mockConnections.get(0), con2);
    Channel channel2 = con2.createChannel(false);
    verifyChannelIs(mockChannels.get(0), channel2);
    channel2.close();
    verify(mockChannels.get(0), never()).close();
    con2.close();
    verify(mockConnections.get(0), never()).close();
    assertEquals(1, allocatedConnections.size());
    assertEquals(1, idleConnections.size());
    assertNull(createNotification.get());

    /*
     * Now check for multiple connections/channels
     */
    con1 = ccf.createConnection();
    verifyConnectionIs(mockConnections.get(0), con1);
    con2 = ccf.createConnection();
    verifyConnectionIs(mockConnections.get(1), con2);
    channel1 = con1.createChannel(false);
    verifyChannelIs(mockChannels.get(0), channel1);
    channel2 = con2.createChannel(false);
    verifyChannelIs(mockChannels.get(1), channel2);
    assertEquals(2, allocatedConnections.size());
    assertEquals(0, idleConnections.size());
    assertNotNull(createNotification.get());
    assertSame(mockConnections.get(1), createNotification.getAndSet(null));

    // put mock1 in cache
    channel1.close();
    verify(mockChannels.get(1), never()).close();
    con1.close();
    verify(mockConnections.get(0), never()).close();
    assertEquals(2, allocatedConnections.size());
    assertEquals(1, idleConnections.size());
    assertNull(closedNotification.get());

    Connection con3 = ccf.createConnection();
    assertNull(createNotification.get());
    verifyConnectionIs(mockConnections.get(0), con3);
    Channel channel3 = con3.createChannel(false);
    verifyChannelIs(mockChannels.get(0), channel3);

    assertEquals(2, allocatedConnections.size());
    assertEquals(0, idleConnections.size());

    channel2.close();
    con2.close();
    assertEquals(2, allocatedConnections.size());
    assertEquals(1, idleConnections.size());
    channel3.close();
    con3.close();
    assertEquals(2, allocatedConnections.size());
    assertEquals(2, idleConnections.size());
    assertEquals("1", ccf.getCacheProperties().get("openConnections"));
    /*
     *  Cache size is 1; con3 (mock1) should have been a real close.
     *  con2 (mock2) should still be in the cache.
     */
    verify(mockConnections.get(0)).close(30000);
    assertNotNull(closedNotification.get());
    assertSame(mockConnections.get(0), closedNotification.getAndSet(null));
    verify(mockChannels.get(1), never()).close();
    verify(mockConnections.get(1), never()).close(30000);
    verify(mockChannels.get(1), never()).close();
    verifyConnectionIs(mockConnections.get(1), idleConnections.iterator().next());
    /*
     * Now a closed cached connection
     */
    when(mockConnections.get(1).isOpen()).thenReturn(false);
    when(mockChannels.get(1).isOpen()).thenReturn(false);
    con3 = ccf.createConnection();
    assertNotNull(closedNotification.get());
    assertSame(mockConnections.get(1), closedNotification.getAndSet(null));
    verifyConnectionIs(mockConnections.get(2), con3);
    assertNotNull(createNotification.get());
    assertSame(mockConnections.get(2), createNotification.getAndSet(null));
    assertEquals(2, allocatedConnections.size());
    assertEquals(1, idleConnections.size());
    assertEquals("1", ccf.getCacheProperties().get("openConnections"));
    channel3 = con3.createChannel(false);
    verifyChannelIs(mockChannels.get(2), channel3);
    channel3.close();
    con3.close();
    assertNull(closedNotification.get());
    assertEquals(2, allocatedConnections.size());
    assertEquals(2, idleConnections.size());
    assertEquals("1", ccf.getCacheProperties().get("openConnections"));

    /*
     * Now a closed cached connection when creating a channel
     */
    con3 = ccf.createConnection();
    verifyConnectionIs(mockConnections.get(2), con3);
    assertNull(createNotification.get());
    assertEquals(2, allocatedConnections.size());
    assertEquals(1, idleConnections.size());
    when(mockConnections.get(2).isOpen()).thenReturn(false);
    channel3 = con3.createChannel(false);
    assertNotNull(closedNotification.getAndSet(null));
    assertNotNull(createNotification.getAndSet(null));

    verifyChannelIs(mockChannels.get(3), channel3);
    channel3.close();
    con3.close();
    assertNull(closedNotification.get());
    assertEquals(2, allocatedConnections.size());
    assertEquals(2, idleConnections.size());
    assertEquals("1", ccf.getCacheProperties().get("openConnections"));

    // destroy
    ccf.destroy();
    assertNotNull(closedNotification.get());
    verify(mockConnections.get(3)).close(30000);
}

From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java

@Test
public void testWithConnectionFactoryCachedConnectionIdleAreClosed() throws Exception {
    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);

    final List<com.rabbitmq.client.Connection> mockConnections = new ArrayList<com.rabbitmq.client.Connection>();
    final List<Channel> mockChannels = new ArrayList<Channel>();

    doAnswer(new Answer<com.rabbitmq.client.Connection>() {

        private int connectionNumber;

        @Override/*from   w  ww . j a va 2s .c o  m*/
        public com.rabbitmq.client.Connection answer(InvocationOnMock invocation) throws Throwable {
            com.rabbitmq.client.Connection connection = mock(com.rabbitmq.client.Connection.class);
            doAnswer(new Answer<Channel>() {

                private int channelNumber;

                @Override
                public Channel answer(InvocationOnMock invocation) throws Throwable {
                    Channel channel = mock(Channel.class);
                    when(channel.isOpen()).thenReturn(true);
                    int channelNumnber = ++this.channelNumber;
                    when(channel.toString()).thenReturn("mockChannel" + channelNumnber);
                    mockChannels.add(channel);
                    return channel;
                }
            }).when(connection).createChannel();
            int connectionNumber = ++this.connectionNumber;
            when(connection.toString()).thenReturn("mockConnection" + connectionNumber);
            when(connection.isOpen()).thenReturn(true);
            mockConnections.add(connection);
            return connection;
        }
    }).when(mockConnectionFactory).newConnection(any(ExecutorService.class), anyString());

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setExecutor(mock(ExecutorService.class));
    ccf.setCacheMode(CacheMode.CONNECTION);
    ccf.setConnectionCacheSize(5);
    ccf.afterPropertiesSet();

    Set<?> allocatedConnections = TestUtils.getPropertyValue(ccf, "allocatedConnections", Set.class);
    assertEquals(0, allocatedConnections.size());
    BlockingQueue<?> idleConnections = TestUtils.getPropertyValue(ccf, "idleConnections", BlockingQueue.class);
    assertEquals(0, idleConnections.size());

    Connection conn1 = ccf.createConnection();
    Connection conn2 = ccf.createConnection();
    Connection conn3 = ccf.createConnection();
    assertEquals(3, allocatedConnections.size());
    assertEquals(0, idleConnections.size());
    conn1.close();
    conn2.close();
    conn3.close();
    assertEquals(3, allocatedConnections.size());
    assertEquals(3, idleConnections.size());

    when(mockConnections.get(0).isOpen()).thenReturn(false);
    when(mockConnections.get(1).isOpen()).thenReturn(false);
    Connection conn4 = ccf.createConnection();
    assertEquals(3, allocatedConnections.size());
    assertEquals(2, idleConnections.size());
    assertSame(conn3, conn4);
    conn4.close();
    assertEquals(3, allocatedConnections.size());
    assertEquals(3, idleConnections.size());
    assertEquals("1", ccf.getCacheProperties().get("openConnections"));

    ccf.destroy();
    assertEquals(3, allocatedConnections.size());
    assertEquals(3, idleConnections.size());
    assertEquals("0", ccf.getCacheProperties().get("openConnections"));
}

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

@SuppressWarnings("unchecked")
@Test//from  w w w.  j a  v  a  2  s.  c  o m
public void testWithConnectionPerListenerThread() throws Exception {
    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    com.rabbitmq.client.Connection mockConnection1 = mock(com.rabbitmq.client.Connection.class);
    com.rabbitmq.client.Connection mockConnection2 = mock(com.rabbitmq.client.Connection.class);
    Channel mockChannel1 = mock(Channel.class);
    Channel mockChannel2 = mock(Channel.class);

    when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString()))
            .thenReturn(mockConnection1).thenReturn(mockConnection2).thenReturn(null);
    when(mockConnection1.createChannel()).thenReturn(mockChannel1).thenReturn(null);
    when(mockConnection2.createChannel()).thenReturn(mockChannel2).thenReturn(null);
    when(mockChannel1.isOpen()).thenReturn(true);
    when(mockConnection1.isOpen()).thenReturn(true);
    when(mockChannel2.isOpen()).thenReturn(true);
    when(mockConnection2.isOpen()).thenReturn(true);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setCacheMode(CacheMode.CONNECTION);

    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(ccf);
    container.setConcurrentConsumers(2);
    container.setQueueNames("foo");
    container.afterPropertiesSet();

    CountDownLatch latch1 = new CountDownLatch(2);
    CountDownLatch latch2 = new CountDownLatch(2);
    doAnswer(messageToConsumer(mockChannel1, container, false, latch1)).when(mockChannel1).basicConsume(
            anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class));
    doAnswer(messageToConsumer(mockChannel2, container, false, latch1)).when(mockChannel2).basicConsume(
            anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class));
    doAnswer(messageToConsumer(mockChannel1, container, true, latch2)).when(mockChannel1)
            .basicCancel(anyString());
    doAnswer(messageToConsumer(mockChannel2, container, true, latch2)).when(mockChannel2)
            .basicCancel(anyString());

    container.start();
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    Set<?> consumers = TestUtils.getPropertyValue(container, "consumers", Map.class).keySet();
    container.stop();
    assertTrue(latch2.await(10, TimeUnit.SECONDS));

    waitForConsumersToStop(consumers);
    Set<?> allocatedConnections = TestUtils.getPropertyValue(ccf, "allocatedConnections", Set.class);
    assertEquals(2, allocatedConnections.size());
    assertEquals("1", ccf.getCacheProperties().get("openConnections"));
}