Example usage for org.springframework.amqp.rabbit.connection Connection close

List of usage examples for org.springframework.amqp.rabbit.connection Connection close

Introduction

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

Prototype

@Override
void close() throws AmqpException;

Source Link

Document

Close this connection and all its channels with the com.rabbitmq.client.AMQP#REPLY_SUCCESS close code and message 'OK'.

Usage

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

@Test
public void testWithListener() throws Exception {

    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class);

    when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString()))
            .thenReturn(mockConnection);

    final AtomicInteger called = new AtomicInteger(0);
    AbstractConnectionFactory connectionFactory = createConnectionFactory(mockConnectionFactory);
    connectionFactory.setConnectionListeners(Collections.singletonList(new ConnectionListener() {

        @Override//w  w  w. j  ava2 s  .  c om
        public void onCreate(Connection connection) {
            called.incrementAndGet();
        }

        @Override
        public void onClose(Connection connection) {
            called.decrementAndGet();
        }

    }));

    Log logger = spy(TestUtils.getPropertyValue(connectionFactory, "logger", Log.class));
    doReturn(true).when(logger).isInfoEnabled();
    new DirectFieldAccessor(connectionFactory).setPropertyValue("logger", logger);
    Connection con = connectionFactory.createConnection();
    assertEquals(1, called.get());
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    verify(logger).info(captor.capture());
    assertThat(captor.getValue(), containsString("Created new connection: SimpleConnection"));

    con.close();
    assertEquals(1, called.get());
    verify(mockConnection, never()).close(anyInt());

    connectionFactory.createConnection();
    assertEquals(1, called.get());

    connectionFactory.destroy();
    assertEquals(0, called.get());
    verify(mockConnection, atLeastOnce()).close(anyInt());

    verify(mockConnectionFactory, times(1)).newConnection(any(ExecutorService.class), anyString());

}

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

@Test
public void testWithListenerRegisteredAfterOpen() throws Exception {

    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class);

    when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString()))
            .thenReturn(mockConnection);

    final AtomicInteger called = new AtomicInteger(0);
    AbstractConnectionFactory connectionFactory = createConnectionFactory(mockConnectionFactory);
    Connection con = connectionFactory.createConnection();
    assertEquals(0, called.get());// w w w  .ja  v a 2s .c o  m

    connectionFactory.setConnectionListeners(Collections.singletonList(new ConnectionListener() {

        @Override
        public void onCreate(Connection connection) {
            called.incrementAndGet();
        }

        @Override
        public void onClose(Connection connection) {
            called.decrementAndGet();
        }

    }));
    assertEquals(1, called.get());

    con.close();
    assertEquals(1, called.get());
    verify(mockConnection, never()).close(anyInt());

    connectionFactory.createConnection();
    assertEquals(1, called.get());

    connectionFactory.destroy();
    assertEquals(0, called.get());
    verify(mockConnection, atLeastOnce()).close(anyInt());

    verify(mockConnectionFactory, times(1)).newConnection(any(ExecutorService.class), anyString());

}

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

@Test
public void testWithConnectionFactoryDefaults() throws Exception {
    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class);
    Channel mockChannel = mock(Channel.class);

    when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString()))
            .thenReturn(mockConnection);
    when(mockConnection.createChannel()).thenReturn(mockChannel);
    when(mockChannel.isOpen()).thenReturn(true);
    when(mockConnection.isOpen()).thenReturn(true);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setExecutor(mock(ExecutorService.class));
    Connection con = ccf.createConnection();

    Channel channel = con.createChannel(false);
    channel.close(); // should be ignored, and placed into channel cache.
    con.close(); // should be ignored

    Connection con2 = ccf.createConnection();
    /*//w  w  w.java2  s.c  o m
     * will retrieve same channel object that was just put into channel cache
     */
    Channel channel2 = con2.createChannel(false);
    channel2.close(); // should be ignored
    con2.close(); // should be ignored

    assertSame(con, con2);
    assertSame(channel, channel2);
    verify(mockConnection, never()).close();
    verify(mockChannel, never()).close();
}

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

@Test
public void testPublisherConnection() throws Exception {
    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class);
    Channel mockChannel = mock(Channel.class);

    when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString()))
            .thenReturn(mockConnection);
    when(mockConnection.createChannel()).thenReturn(mockChannel);
    when(mockChannel.isOpen()).thenReturn(true);
    when(mockConnection.isOpen()).thenReturn(true);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setExecutor(mock(ExecutorService.class));
    Connection con = ccf.getPublisherConnectionFactory().createConnection();

    Channel channel = con.createChannel(false);
    channel.close(); // should be ignored, and placed into channel cache.
    con.close(); // should be ignored

    Connection con2 = ccf.getPublisherConnectionFactory().createConnection();
    /*/*from   www.  j  a v  a  2s . c o m*/
     * will retrieve same channel object that was just put into channel cache
     */
    Channel channel2 = con2.createChannel(false);
    channel2.close(); // should be ignored
    con2.close(); // should be ignored

    assertSame(con, con2);
    assertSame(channel, channel2);
    verify(mockConnection, never()).close();
    verify(mockChannel, never()).close();

    assertNull(TestUtils.getPropertyValue(ccf, "connection.target"));
    assertNotNull(TestUtils.getPropertyValue(ccf, "publisherConnectionFactory.connection.target"));
    assertSame(con, TestUtils.getPropertyValue(ccf, "publisherConnectionFactory.connection"));
}

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

@Test
public void testWithConnectionFactoryCacheSize() throws Exception {
    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class);
    Channel mockChannel1 = mock(Channel.class);
    Channel mockChannel2 = mock(Channel.class);
    Channel mockTxChannel = mock(Channel.class);

    when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString()))
            .thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);
    when(mockConnection.createChannel()).thenReturn(mockChannel1, mockChannel2, mockTxChannel);

    when(mockChannel1.basicGet("foo", false)).thenReturn(new GetResponse(null, null, null, 1));
    when(mockChannel2.basicGet("bar", false)).thenReturn(new GetResponse(null, null, null, 1));
    when(mockChannel1.isOpen()).thenReturn(true);
    when(mockChannel2.isOpen()).thenReturn(true);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setExecutor(mock(ExecutorService.class));
    ccf.setChannelCacheSize(2);/*from w  w  w.  j  a  v  a 2  s.c  om*/

    Connection con = ccf.createConnection();

    Channel channel1 = con.createChannel(false);
    Channel channel2 = con.createChannel(false);

    ChannelProxy txChannel = (ChannelProxy) con.createChannel(true);
    assertTrue(txChannel.isTransactional());
    verify(mockTxChannel).txSelect();
    txChannel.close();

    channel1.basicGet("foo", true);
    channel2.basicGet("bar", true);

    channel1.close(); // should be ignored, and add last into channel cache.
    channel2.close(); // should be ignored, and add last into channel cache.

    Channel ch1 = con.createChannel(false); // remove first entry in cache
    // (channel1)
    Channel ch2 = con.createChannel(false); // remove first entry in cache
    // (channel2)

    assertNotSame(ch1, ch2);
    assertSame(ch1, channel1);
    assertSame(ch2, channel2);

    ch1.close();
    ch2.close();

    verify(mockConnection, times(3)).createChannel();

    con.close(); // should be ignored

    verify(mockConnection, never()).close();
    verify(mockChannel1, never()).close();
    verify(mockChannel2, never()).close();

}

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

@Test
public void testCacheSizeExceeded() throws Exception {
    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class);
    Channel mockChannel1 = mock(Channel.class);
    Channel mockChannel2 = mock(Channel.class);
    Channel mockChannel3 = mock(Channel.class);

    when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString()))
            .thenReturn(mockConnection);
    when(mockConnection.createChannel()).thenReturn(mockChannel1).thenReturn(mockChannel2)
            .thenReturn(mockChannel3);/*  w  w w . ja va 2 s.c o m*/
    when(mockConnection.isOpen()).thenReturn(true);

    // Called during physical close
    when(mockChannel1.isOpen()).thenReturn(true);
    when(mockChannel2.isOpen()).thenReturn(true);
    when(mockChannel3.isOpen()).thenReturn(true);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setExecutor(mock(ExecutorService.class));
    ccf.setChannelCacheSize(1);

    Connection con = ccf.createConnection();

    Channel channel1 = con.createChannel(false);
    // cache size is 1, but the other connection is not released yet so this
    // creates a new one
    Channel channel2 = con.createChannel(false);
    assertNotSame(channel1, channel2);

    // should be ignored, and added last into channel cache.
    channel1.close();
    // should be physically closed
    channel2.close();

    // remove first entry in cache (channel1)
    Channel ch1 = con.createChannel(false);
    // create a new channel
    Channel ch2 = con.createChannel(false);

    assertNotSame(ch1, ch2);
    assertSame(ch1, channel1);
    assertNotSame(ch2, channel2);

    ch1.close();
    ch2.close();

    verify(mockConnection, times(3)).createChannel();

    con.close(); // should be ignored

    verify(mockConnection, never()).close();
    verify(mockChannel1, never()).close();
    verify(mockChannel2, atLeastOnce()).close();
    verify(mockChannel3, atLeastOnce()).close();

}

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

@Test
public void testCheckoutLimit() throws Exception {
    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class);
    Channel mockChannel1 = mock(Channel.class);

    when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString()))
            .thenReturn(mockConnection);
    when(mockConnection.createChannel()).thenReturn(mockChannel1);
    when(mockConnection.isOpen()).thenReturn(true);

    // Called during physical close
    when(mockChannel1.isOpen()).thenReturn(true);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setExecutor(mock(ExecutorService.class));
    ccf.setChannelCacheSize(1);/*w  ww.j  a va2s. c  om*/
    ccf.setChannelCheckoutTimeout(10);

    Connection con = ccf.createConnection();

    Channel channel1 = con.createChannel(false);

    try {
        con.createChannel(false);
        fail("Exception expected");
    } catch (AmqpTimeoutException e) {
    }

    // should be ignored, and added last into channel cache.
    channel1.close();

    // remove first entry in cache (channel1)
    Channel ch1 = con.createChannel(false);

    assertSame(ch1, channel1);

    ch1.close();

    verify(mockConnection, times(1)).createChannel();

    con.close(); // should be ignored

    verify(mockConnection, never()).close();
    verify(mockChannel1, never()).close();

    ccf.destroy();
}

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

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

    when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString()))
            .thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);

    final CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setExecutor(mock(ExecutorService.class));
    ccf.setCacheMode(CacheMode.CONNECTION);
    ccf.setConnectionCacheSize(1);/*from www .j  a  v a  2  s . c om*/
    ccf.setConnectionLimit(1);
    ccf.setChannelCheckoutTimeout(10);

    Connection con1 = ccf.createConnection();

    try {
        ccf.createConnection();
        fail("Exception expected");
    } catch (AmqpTimeoutException e) {
    }

    // should be ignored, and added to cache
    con1.close();

    Connection con2 = ccf.createConnection();
    assertSame(con1, con2);

    final CountDownLatch latch2 = new CountDownLatch(1);
    final CountDownLatch latch1 = new CountDownLatch(1);
    final AtomicReference<Connection> connection = new AtomicReference<Connection>();
    ccf.setChannelCheckoutTimeout(30000);
    Executors.newSingleThreadExecutor().execute(() -> {
        latch1.countDown();
        connection.set(ccf.createConnection());
        latch2.countDown();
    });

    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    Thread.sleep(100);
    con2.close();
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    assertSame(con2, connection.get());

    ccf.destroy();
}

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

private void testCheckoutsWithRefreshedConnectionGuts(CacheMode mode) 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);
    Channel mockChannel3 = mock(Channel.class);
    Channel mockChannel4 = mock(Channel.class);

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

    when(mockChannel1.isOpen()).thenReturn(true);
    when(mockChannel2.isOpen()).thenReturn(true);
    when(mockChannel3.isOpen()).thenReturn(true);
    when(mockChannel4.isOpen()).thenReturn(true);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setExecutor(mock(ExecutorService.class));
    ccf.setChannelCacheSize(2);//from w ww  .j av a  2 s.  c  o m
    ccf.setChannelCheckoutTimeout(10);
    ccf.setCacheMode(mode);

    ccf.addConnectionListener(connection -> {
        try {
            // simulate admin
            connection.createChannel(false).close();
        } catch (Exception e) {
            fail(e.getMessage());
        }
    });

    Connection con = ccf.createConnection();

    Channel channel1 = con.createChannel(false);
    assertEquals(1, ((Semaphore) TestUtils.getPropertyValue(ccf, "checkoutPermits", Map.class).values()
            .iterator().next()).availablePermits());
    channel1.close();
    con.close();

    assertEquals(2, ((Semaphore) TestUtils.getPropertyValue(ccf, "checkoutPermits", Map.class).values()
            .iterator().next()).availablePermits());

    when(mockConnection1.isOpen()).thenReturn(false);
    when(mockChannel1.isOpen()).thenReturn(false);
    when(mockChannel2.isOpen()).thenReturn(false);

    con.createChannel(false).close();
    con = ccf.createConnection();
    con.createChannel(false).close();
    con.createChannel(false).close();
    con.createChannel(false).close();
    con.createChannel(false).close();
    con.createChannel(false).close();

    verify(mockConnection1, times(1)).createChannel();
    verify(mockConnection2, times(2)).createChannel();

    con.close();

    verify(mockConnection2, never()).close();

    assertEquals(2, ((Semaphore) TestUtils.getPropertyValue(ccf, "checkoutPermits", Map.class).values()
            .iterator().next()).availablePermits());

    ccf.destroy();

    assertEquals(2, ((Semaphore) TestUtils.getPropertyValue(ccf, "checkoutPermits", Map.class).values()
            .iterator().next()).availablePermits());

}

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

@Test
public void testReleaseWithForcedPhysicalClose() throws Exception {
    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    com.rabbitmq.client.Connection mockConnection1 = mock(com.rabbitmq.client.Connection.class);
    Channel mockChannel1 = mock(Channel.class);

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

    when(mockChannel1.isOpen()).thenReturn(true);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setExecutor(mock(ExecutorService.class));
    ccf.setChannelCacheSize(1);// w  w w . j ava  2  s  .c  om
    ccf.setChannelCheckoutTimeout(10);

    Connection con = ccf.createConnection();

    Channel channel1 = con.createChannel(false);
    assertEquals(0, ((Semaphore) TestUtils.getPropertyValue(ccf, "checkoutPermits", Map.class).values()
            .iterator().next()).availablePermits());
    channel1.close();
    con.close();

    assertEquals(1, ((Semaphore) TestUtils.getPropertyValue(ccf, "checkoutPermits", Map.class).values()
            .iterator().next()).availablePermits());

    channel1 = con.createChannel(false);
    RabbitUtils.setPhysicalCloseRequired(channel1, true);
    assertEquals(0, ((Semaphore) TestUtils.getPropertyValue(ccf, "checkoutPermits", Map.class).values()
            .iterator().next()).availablePermits());

    channel1.close();
    RabbitUtils.setPhysicalCloseRequired(channel1, false);
    con.close();
    verify(mockChannel1).close();
    verify(mockConnection1, never()).close();

    assertEquals(1, ((Semaphore) TestUtils.getPropertyValue(ccf, "checkoutPermits", Map.class).values()
            .iterator().next()).availablePermits());

    ccf.destroy();

    assertEquals(1, ((Semaphore) TestUtils.getPropertyValue(ccf, "checkoutPermits", Map.class).values()
            .iterator().next()).availablePermits());

}