Example usage for org.springframework.integration.ip.tcp.connection TcpNioConnection setTaskExecutor

List of usage examples for org.springframework.integration.ip.tcp.connection TcpNioConnection setTaskExecutor

Introduction

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

Prototype

public void setTaskExecutor(Executor taskExecutor) 

Source Link

Usage

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

@Test
public void testInsufficientThreads() throws Exception {
    final ExecutorService exec = Executors.newFixedThreadPool(2);
    Future<Object> future = exec.submit(() -> {
        SocketChannel channel = mock(SocketChannel.class);
        Socket socket = mock(Socket.class);
        Mockito.when(channel.socket()).thenReturn(socket);
        doAnswer(invocation -> {/*from w ww  .java2  s  .  c o m*/
            ByteBuffer buffer = invocation.getArgument(0);
            buffer.position(1);
            return 1;
        }).when(channel).read(Mockito.any(ByteBuffer.class));
        when(socket.getReceiveBufferSize()).thenReturn(1024);
        final TcpNioConnection connection = new TcpNioConnection(channel, false, false, nullPublisher, null);
        connection.setTaskExecutor(exec);
        connection.setPipeTimeout(200);
        Method method = TcpNioConnection.class.getDeclaredMethod("doRead");
        method.setAccessible(true);
        // Nobody reading, should timeout on 6th write.
        try {
            for (int i = 0; i < 6; i++) {
                method.invoke(connection);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw (Exception) e.getCause();
        }
        return null;
    });
    try {
        Object o = future.get(10, TimeUnit.SECONDS);
        fail("Expected exception, got " + o);
    } catch (ExecutionException e) {
        assertEquals("Timed out waiting for buffer space", e.getCause().getMessage());
    }
}

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

@Test
public void testSufficientThreads() throws Exception {
    final ExecutorService exec = Executors.newFixedThreadPool(3);
    final CountDownLatch messageLatch = new CountDownLatch(1);
    Future<Object> future = exec.submit(() -> {
        SocketChannel channel = mock(SocketChannel.class);
        Socket socket = mock(Socket.class);
        Mockito.when(channel.socket()).thenReturn(socket);
        doAnswer(invocation -> {/*from   w ww.j ava2s.co  m*/
            ByteBuffer buffer = invocation.getArgument(0);
            buffer.position(1025);
            buffer.put((byte) '\r');
            buffer.put((byte) '\n');
            return 1027;
        }).when(channel).read(Mockito.any(ByteBuffer.class));
        final TcpNioConnection connection = new TcpNioConnection(channel, false, false, null, null);
        connection.setTaskExecutor(exec);
        connection.registerListener(message -> {
            messageLatch.countDown();
            return false;
        });
        connection.setMapper(new TcpMessageMapper());
        connection.setDeserializer(new ByteArrayCrLfSerializer());
        Method method = TcpNioConnection.class.getDeclaredMethod("doRead");
        method.setAccessible(true);
        try {
            for (int i = 0; i < 20; i++) {
                method.invoke(connection);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw (Exception) e.getCause();
        }
        return null;
    });
    future.get(60, TimeUnit.SECONDS);
    assertTrue(messageLatch.await(10, TimeUnit.SECONDS));
}