Example usage for org.springframework.integration.ip.tcp.connection TcpConnectionSupport close

List of usage examples for org.springframework.integration.ip.tcp.connection TcpConnectionSupport close

Introduction

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

Prototype

@Override
public void close() 

Source Link

Document

Closes this connection.

Usage

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

public CachingClientConnectionFactory(AbstractClientConnectionFactory target, int poolSize) {
    super("", 0);
    // override single-use to true to force "close" after use
    target.setSingleUse(true);/*from   w w w.jav a 2s. c  om*/
    this.targetConnectionFactory = target;
    pool = new SimplePool<TcpConnectionSupport>(poolSize,
            new SimplePool.PoolItemCallback<TcpConnectionSupport>() {

                public TcpConnectionSupport createForPool() {
                    try {
                        return targetConnectionFactory.getConnection();
                    } catch (Exception e) {
                        throw new MessagingException("Failed to obtain connection", e);
                    }
                }

                public boolean isStale(TcpConnectionSupport connection) {
                    return !connection.isOpen();
                }

                public void removedFromPool(TcpConnectionSupport connection) {
                    connection.close();
                }
            });
}

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 .  j a  va2 s.  c  o m*/
        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();
}