Example usage for org.springframework.amqp.rabbit.connection ChannelListener ChannelListener

List of usage examples for org.springframework.amqp.rabbit.connection ChannelListener ChannelListener

Introduction

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

Prototype

ChannelListener

Source Link

Usage

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

@Test
public void testWithChannelListener() 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(mockChannel.isOpen()).thenReturn(true);
    when(mockConnection.createChannel()).thenReturn(mockChannel);

    final AtomicInteger called = new AtomicInteger(0);
    AbstractConnectionFactory connectionFactory = createConnectionFactory(mockConnectionFactory);
    connectionFactory.setChannelListeners(Arrays.asList(new ChannelListener() {

        @Override/*from w  w w .  jav a  2s . c om*/
        public void onCreate(Channel channel, boolean transactional) {
            called.incrementAndGet();
        }
    }));
    ((CachingConnectionFactory) connectionFactory).setChannelCacheSize(1);

    Connection con = connectionFactory.createConnection();
    Channel channel = con.createChannel(false);
    assertEquals(1, called.get());
    channel.close();

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

    connectionFactory.createConnection();
    con.createChannel(false);
    assertEquals(1, called.get());

    connectionFactory.destroy();
    verify(mockConnection, atLeastOnce()).close(anyInt());

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

}