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

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

Introduction

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

Prototype

public void setConnectionLimit(int connectionLimit) 

Source Link

Document

Set the connection limit when using cache mode CONNECTION.

Usage

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 w  ww  .j  a  v a  2 s  .co  m
    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();
}