Example usage for org.springframework.integration.ip.tcp.connection TcpConnection isOpen

List of usage examples for org.springframework.integration.ip.tcp.connection TcpConnection isOpen

Introduction

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

Prototype

boolean isOpen();

Source Link

Usage

From source file:org.springframework.integration.ip.addons.CachingTcpConnectionFactory.java

protected TcpConnection retrieveConnection(int timeout) throws Exception {
    TcpConnection connection;
    if (timeout > 0) {
        connection = this.available.poll(timeout, TimeUnit.MILLISECONDS);
    } else {/*from w  w w . j  a va  2s . com*/
        connection = this.available.poll();
    }
    if (connection == null) {
        return null;
    }
    if (!connection.isOpen()) {
        if (logger.isDebugEnabled()) {
            logger.debug(connection.getConnectionId() + " is closed, trying another");
        }
        return null;
    }
    synchronized (this.targetConnectionFactory) {
        this.inUse.add(connection);
    }
    if (logger.isDebugEnabled() && connection != null) {
        logger.debug("Retrieved " + connection.getConnectionId() + " from cache");
    }
    return connection;
}

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

private void testCloseOnTimeoutGuts(AbstractClientConnectionFactory cf) throws Exception {
    cf.setSoTimeout(100);/*  w  w  w  .  j a va  2  s  .c o m*/
    CachingClientConnectionFactory cccf = new CachingClientConnectionFactory(cf, 1);
    cccf.start();
    TcpConnection connection = cccf.getConnection();
    int n = 0;
    while (n++ < 100 && connection.isOpen()) {
        Thread.sleep(100);
    }
    assertFalse(connection.isOpen());
    cccf.stop();
}

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

@Override
protected TcpConnection obtainConnection() throws Exception {
    TcpConnection connection = this.getTheConnection();
    if (connection != null && connection.isOpen()) {
        return connection;
    }//from  w  w w .j a  va2 s. co  m
    return new FailoverTcpConnection(this.factories);
}

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

@Test
public void testReadTimeout() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch done = new CountDownLatch(1);
    final AtomicReference<ServerSocket> serverSocket = new AtomicReference<ServerSocket>();
    Executors.newSingleThreadExecutor().execute(() -> {
        try {//from w  ww.j a va2 s.c  om
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
            logger.debug(testName.getMethodName() + " starting server for " + server.getLocalPort());
            serverSocket.set(server);
            latch.countDown();
            Socket socket = server.accept();
            byte[] b = new byte[6];
            readFully(socket.getInputStream(), b);
            // block to cause timeout on read.
            done.await(10, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    TcpNioClientConnectionFactory factory = new TcpNioClientConnectionFactory("localhost",
            serverSocket.get().getLocalPort());
    factory.setApplicationEventPublisher(nullPublisher);
    factory.setSoTimeout(1000);
    factory.start();
    try {
        TcpConnection connection = factory.getConnection();
        connection.send(MessageBuilder.withPayload("Test").build());
        int n = 0;
        while (connection.isOpen()) {
            Thread.sleep(100);
            if (n++ > 200) {
                break;
            }
        }
        assertTrue(!connection.isOpen());
    } catch (Exception e) {
        fail("Unexpected exception " + e);
    }
    done.countDown();
    serverSocket.get().close();
}

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

@Test
public void testMemoryLeak() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<ServerSocket> serverSocket = new AtomicReference<ServerSocket>();
    Executors.newSingleThreadExecutor().execute(() -> {
        try {/* w  w  w  .  j a v  a2s  .  c  om*/
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
            logger.debug(testName.getMethodName() + " starting server for " + server.getLocalPort());
            serverSocket.set(server);
            latch.countDown();
            Socket socket = server.accept();
            byte[] b = new byte[6];
            readFully(socket.getInputStream(), b);
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    TcpNioClientConnectionFactory factory = new TcpNioClientConnectionFactory("localhost",
            serverSocket.get().getLocalPort());
    factory.setApplicationEventPublisher(nullPublisher);
    factory.setNioHarvestInterval(100);
    factory.start();
    try {
        TcpConnection connection = factory.getConnection();
        Map<SocketChannel, TcpNioConnection> connections = factory.getConnections();
        assertEquals(1, connections.size());
        connection.close();
        assertTrue(!connection.isOpen());
        TestUtils.getPropertyValue(factory, "selector", Selector.class).wakeup();
        int n = 0;
        while (connections.size() > 0) {
            Thread.sleep(100);
            if (n++ > 100) {
                break;
            }
        }
        assertEquals(0, connections.size());
    } catch (Exception e) {
        e.printStackTrace();
        fail("Unexpected exception " + e);
    }
    factory.stop();
    serverSocket.get().close();
}