Search in sources :

Example 31 with ByteArrayCrLfSerializer

use of org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer in project spring-integration by spring-projects.

the class TcpNioConnectionWriteTests method testWriteCrLfDirect.

@Test
public void testWriteCrLfDirect() throws Exception {
    final String testString = "abcdef";
    ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
    final int port = server.getLocalPort();
    server.setSoTimeout(10000);
    final CountDownLatch latch = new CountDownLatch(1);
    Thread t = new Thread(() -> {
        AbstractConnectionFactory ccf = null;
        try {
            ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
            ccf = getClientConnectionFactory(true, port, serializer);
            TcpConnection connection = ccf.getConnection();
            connection.send(MessageBuilder.withPayload(testString.getBytes()).build());
            latch.await(10, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ccf != null) {
                ccf.stop();
            }
        }
    });
    t.setDaemon(true);
    t.start();
    Socket socket = server.accept();
    socket.setSoTimeout(5000);
    InputStream is = socket.getInputStream();
    byte[] buff = new byte[testString.length() + 2];
    readFully(is, buff);
    assertEquals(testString, new String(buff, 0, testString.length()));
    assertEquals('\r', buff[testString.length()]);
    assertEquals('\n', buff[testString.length() + 1]);
    server.close();
    latch.countDown();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) InputStream(java.io.InputStream) ServerSocket(java.net.ServerSocket) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Aggregations

ByteArrayCrLfSerializer (org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer)31 Test (org.junit.Test)29 Socket (java.net.Socket)26 ServerSocket (java.net.ServerSocket)22 CountDownLatch (java.util.concurrent.CountDownLatch)17 QueueChannel (org.springframework.integration.channel.QueueChannel)17 IOException (java.io.IOException)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 SocketException (java.net.SocketException)9 Semaphore (java.util.concurrent.Semaphore)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 AbstractConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory)8 MessagingException (org.springframework.messaging.MessagingException)8 HashSet (java.util.HashSet)7 ArrayList (java.util.ArrayList)6 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)6 AbstractServerConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory)5 TcpNetClientConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory)5 TcpNetServerConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory)5 TcpNioServerConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory)5