Example usage for org.springframework.integration.ip.tcp.connection TcpNioClientConnectionFactory setSoTimeout

List of usage examples for org.springframework.integration.ip.tcp.connection TcpNioClientConnectionFactory setSoTimeout

Introduction

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

Prototype

public void setSoTimeout(int soTimeout) 

Source Link

Usage

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

@Test
public void testWriteTimeout() 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 {/*  w  w  w.j  av  a2 s .co  m*/
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
            logger.debug(testName.getMethodName() + " starting server for " + server.getLocalPort());
            serverSocket.set(server);
            latch.countDown();
            Socket s = server.accept();
            // block so we fill the buffer
            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(new byte[1000000]).build());
    } catch (Exception e) {
        assertTrue(
                "Expected SocketTimeoutException, got " + e.getClass().getSimpleName() + ":" + e.getMessage(),
                e instanceof SocketTimeoutException);
    }
    done.countDown();
    serverSocket.get().close();
}

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 {//  w  w w .  ja  v a2s.c o m
            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();
}