Example usage for org.springframework.integration.ip.tcp.connection CachingClientConnectionFactory setSingleUse

List of usage examples for org.springframework.integration.ip.tcp.connection CachingClientConnectionFactory setSingleUse

Introduction

In this page you can find the example usage for org.springframework.integration.ip.tcp.connection CachingClientConnectionFactory setSingleUse.

Prototype

@Override
public void setSingleUse(boolean singleUse) 

Source Link

Document

Ignored on this factory; connections are always cached in the pool.

Usage

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

@Test //INT-3650
public void testRealConnection() throws Exception {
    TcpNetServerConnectionFactory in = new TcpNetServerConnectionFactory(0);
    final CountDownLatch latch1 = new CountDownLatch(2);
    final CountDownLatch latch2 = new CountDownLatch(102);
    final List<String> connectionIds = new ArrayList<String>();
    in.registerListener(message -> {//from w  w  w.  jav  a2  s . com
        connectionIds.add((String) message.getHeaders().get(IpHeaders.CONNECTION_ID));
        latch1.countDown();
        latch2.countDown();
        return false;
    });
    in.start();
    TestingUtilities.waitListening(in, null);
    int port = in.getPort();
    TcpNetClientConnectionFactory out = new TcpNetClientConnectionFactory("localhost", port);
    CachingClientConnectionFactory cache = new CachingClientConnectionFactory(out, 1);
    cache.setSingleUse(false);
    cache.setConnectionWaitTimeout(100);
    cache.start();
    TcpConnectionSupport connection1 = cache.getConnection();
    connection1.send(new GenericMessage<String>("foo"));
    connection1.close();
    TcpConnectionSupport connection2 = cache.getConnection();
    connection2.send(new GenericMessage<String>("foo"));
    connection2.close();
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    assertSame(connectionIds.get(0), connectionIds.get(1));
    for (int i = 0; i < 100; i++) {
        TcpConnectionSupport connection = cache.getConnection();
        connection.send(new GenericMessage<String>("foo"));
        connection.close();
    }
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    assertSame(connectionIds.get(0), connectionIds.get(101));
    in.stop();
    cache.stop();
}