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

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

Introduction

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

Prototype

@Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) 

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 {/*from   www  .ja v  a 2s  . c o  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.  j  a  v  a2  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 ww. j a  v  a 2s. 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);
        } 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();
}

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

@Test
public void testCleanup() throws Exception {
    TcpNioClientConnectionFactory factory = new TcpNioClientConnectionFactory("localhost", 0);
    factory.setApplicationEventPublisher(nullPublisher);
    factory.setNioHarvestInterval(100);/*  ww  w .  ja  v  a  2s.  c  o  m*/
    Map<SocketChannel, TcpNioConnection> connections = new HashMap<SocketChannel, TcpNioConnection>();
    SocketChannel chan1 = mock(SocketChannel.class);
    SocketChannel chan2 = mock(SocketChannel.class);
    SocketChannel chan3 = mock(SocketChannel.class);
    TcpNioConnection conn1 = mock(TcpNioConnection.class);
    TcpNioConnection conn2 = mock(TcpNioConnection.class);
    TcpNioConnection conn3 = mock(TcpNioConnection.class);
    connections.put(chan1, conn1);
    connections.put(chan2, conn2);
    connections.put(chan3, conn3);
    final List<Field> fields = new ArrayList<Field>();
    ReflectionUtils.doWithFields(SocketChannel.class, field -> {
        field.setAccessible(true);
        fields.add(field);
    }, field -> field.getName().equals("open"));
    Field field = fields.get(0);
    // Can't use Mockito because isOpen() is final
    ReflectionUtils.setField(field, chan1, true);
    ReflectionUtils.setField(field, chan2, true);
    ReflectionUtils.setField(field, chan3, true);
    Selector selector = mock(Selector.class);
    HashSet<SelectionKey> keys = new HashSet<SelectionKey>();
    when(selector.selectedKeys()).thenReturn(keys);
    factory.processNioSelections(1, selector, null, connections);
    assertEquals(3, connections.size()); // all open

    ReflectionUtils.setField(field, chan1, false);
    factory.processNioSelections(1, selector, null, connections);
    assertEquals(3, connections.size()); // interval didn't pass
    Thread.sleep(110);
    factory.processNioSelections(1, selector, null, connections);
    assertEquals(2, connections.size()); // first is closed

    ReflectionUtils.setField(field, chan2, false);
    factory.processNioSelections(1, selector, null, connections);
    assertEquals(2, connections.size()); // interval didn't pass
    Thread.sleep(110);
    factory.processNioSelections(1, selector, null, connections);
    assertEquals(1, connections.size()); // second is closed

    ReflectionUtils.setField(field, chan3, false);
    factory.processNioSelections(1, selector, null, connections);
    assertEquals(1, connections.size()); // interval didn't pass
    Thread.sleep(110);
    factory.processNioSelections(1, selector, null, connections);
    assertEquals(0, connections.size()); // third is closed

    assertEquals(0, TestUtils.getPropertyValue(factory, "connections", Map.class).size());
}