Example usage for org.springframework.amqp.rabbit.connection AbstractConnectionFactory setChannelListeners

List of usage examples for org.springframework.amqp.rabbit.connection AbstractConnectionFactory setChannelListeners

Introduction

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

Prototype

public void setChannelListeners(List<? extends ChannelListener> listeners) 

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

}