Example usage for org.springframework.amqp.rabbit.connection RabbitUtils setPhysicalCloseRequired

List of usage examples for org.springframework.amqp.rabbit.connection RabbitUtils setPhysicalCloseRequired

Introduction

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

Prototype

public static void setPhysicalCloseRequired(Channel channel, boolean b) 

Source Link

Document

Sets a ThreadLocal indicating the channel MUST be physically closed.

Usage

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);//from   w  w  w .j  a v a2s  .  co  m
    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());

}

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

public void testConsumerChannelPhysicallyClosedWhenNotIsOpenGuts(boolean confirms) throws Exception {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    try {/*from   w  w  w.  ja v a 2  s  .  c  o  m*/
        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(executor);
        ccf.setPublisherConfirms(confirms);
        Connection con = ccf.createConnection();

        Channel channel = con.createChannel(false);
        RabbitUtils.setPhysicalCloseRequired(channel, true);
        when(mockChannel.isOpen()).thenReturn(false);
        final CountDownLatch physicalCloseLatch = new CountDownLatch(1);
        doAnswer(i -> {
            physicalCloseLatch.countDown();
            return null;
        }).when(mockChannel).close();
        channel.close();
        con.close(); // should be ignored

        assertTrue(physicalCloseLatch.await(10, TimeUnit.SECONDS));
    } finally {
        executor.shutdownNow();
    }
}

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

@Test
@Ignore // Test to verify log message is suppressed after patch to CCF
public void testReturnsNormalCloseDeferredClose() 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.isOpen()).thenReturn(true);
    when(mockConnection.createChannel()).thenReturn(mockChannel);
    when(mockChannel.isOpen()).thenReturn(true);
    doThrow(new ShutdownSignalException(true, false,
            new com.rabbitmq.client.AMQP.Connection.Close.Builder().replyCode(200).replyText("OK").build(),
            null)).when(mockChannel).close();

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setPublisherReturns(true);/*  ww w.j  av a2  s .c o  m*/
    ccf.setExecutor(Executors.newSingleThreadExecutor());
    Connection conn = ccf.createConnection();
    Channel channel = conn.createChannel(false);
    RabbitUtils.setPhysicalCloseRequired(channel, true);
    channel.close();
    Thread.sleep(6000);
}