Search in sources :

Example 16 with ByteArrayCrLfSerializer

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

the class TcpNioConnectionReadTests method testReadCrLf.

@SuppressWarnings("unchecked")
@Test
public void testReadCrLf() throws Exception {
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    final List<Message<?>> responses = new ArrayList<Message<?>>();
    final Semaphore semaphore = new Semaphore(0);
    AbstractServerConnectionFactory scf = getConnectionFactory(serializer, message -> {
        responses.add(message);
        semaphore.release();
        return false;
    });
    // Fire up the sender.
    CountDownLatch done = SocketTestUtils.testSendCrLf(scf.getPort(), latch);
    latch.countDown();
    assertTrue(semaphore.tryAcquire(1, 10000, TimeUnit.MILLISECONDS));
    assertTrue(semaphore.tryAcquire(1, 10000, TimeUnit.MILLISECONDS));
    assertEquals("Did not receive data", 2, responses.size());
    assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, new String(((Message<byte[]>) responses.get(0)).getPayload()));
    assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, new String(((Message<byte[]>) responses.get(1)).getPayload()));
    scf.stop();
    done.countDown();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) ArrayList(java.util.ArrayList) Semaphore(java.util.concurrent.Semaphore) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 17 with ByteArrayCrLfSerializer

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

the class TcpNioConnectionWriteTests method testWriteCrLf.

@Test
public void testWriteCrLf() 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(false, 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)

Example 18 with ByteArrayCrLfSerializer

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

the class SocketSupportTests method testNioClientAndServerSSLDifferentContextsLargeDataWithReply.

@Test
public void testNioClientAndServerSSLDifferentContextsLargeDataWithReply() throws Exception {
    // SSL activity in the console
    System.setProperty("javax.net.debug", "all");
    TcpNioServerConnectionFactory server = new TcpNioServerConnectionFactory(0);
    TcpSSLContextSupport serverSslContextSupport = new DefaultTcpSSLContextSupport("server.ks", "server.truststore.ks", "secret", "secret");
    DefaultTcpNioSSLConnectionSupport serverTcpNioConnectionSupport = new DefaultTcpNioSSLConnectionSupport(serverSslContextSupport);
    server.setTcpNioConnectionSupport(serverTcpNioConnectionSupport);
    final List<Message<?>> messages = new ArrayList<Message<?>>();
    final CountDownLatch latch = new CountDownLatch(2);
    final Replier replier = new Replier();
    server.registerSender(replier);
    server.registerListener(message -> {
        messages.add(message);
        try {
            replier.send(message);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        latch.countDown();
        return false;
    });
    ByteArrayCrLfSerializer deserializer = new ByteArrayCrLfSerializer();
    deserializer.setMaxMessageSize(120000);
    server.setDeserializer(deserializer);
    final AtomicReference<String> serverConnectionId = new AtomicReference<>();
    server.setApplicationEventPublisher(e -> {
        if (e instanceof TcpConnectionOpenEvent) {
            serverConnectionId.set(((TcpConnectionEvent) e).getConnectionId());
        }
    });
    server.start();
    TestingUtilities.waitListening(server, null);
    TcpNioClientConnectionFactory client = new TcpNioClientConnectionFactory("localhost", server.getPort());
    TcpSSLContextSupport clientSslContextSupport = new DefaultTcpSSLContextSupport("client.ks", "client.truststore.ks", "secret", "secret");
    DefaultTcpNioSSLConnectionSupport clientTcpNioConnectionSupport = new DefaultTcpNioSSLConnectionSupport(clientSslContextSupport);
    client.setTcpNioConnectionSupport(clientTcpNioConnectionSupport);
    client.registerListener(message -> {
        messages.add(message);
        latch.countDown();
        return false;
    });
    client.setDeserializer(deserializer);
    client.setApplicationEventPublisher(e -> {
    });
    client.start();
    TcpConnection connection = client.getConnection();
    assertEquals(30, TestUtils.getPropertyValue(connection, "handshakeTimeout"));
    byte[] bytes = new byte[100000];
    connection.send(new GenericMessage<String>("Hello, world!" + new String(bytes)));
    assertTrue(latch.await(60, TimeUnit.SECONDS));
    byte[] payload = (byte[]) messages.get(0).getPayload();
    assertEquals(13 + bytes.length, payload.length);
    assertEquals("Hello, world!", new String(payload).substring(0, 13));
    payload = (byte[]) messages.get(1).getPayload();
    assertEquals(13 + bytes.length, payload.length);
    assertEquals("Hello, world!", new String(payload).substring(0, 13));
    Map<?, ?> connections = TestUtils.getPropertyValue(server, "connections", Map.class);
    Object serverConnection = connections.get(serverConnectionId.get());
    assertNotNull(serverConnection);
    assertEquals(30, TestUtils.getPropertyValue(serverConnection, "handshakeTimeout"));
    client.stop();
    server.stop();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) SocketException(java.net.SocketException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) Test(org.junit.Test)

Example 19 with ByteArrayCrLfSerializer

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

the class TcpSendingMessageHandlerTests method testNioSingleUseNoInbound.

@Test
public void testNioSingleUseNoInbound() throws Exception {
    final AtomicReference<ServerSocket> serverSocket = new AtomicReference<ServerSocket>();
    final CountDownLatch latch = new CountDownLatch(1);
    final Semaphore semaphore = new Semaphore(0);
    final AtomicBoolean done = new AtomicBoolean();
    this.executor.execute(() -> {
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
            serverSocket.set(server);
            latch.countDown();
            for (int i = 0; i < 2; i++) {
                Socket socket = server.accept();
                semaphore.release();
                byte[] b = new byte[8];
                readFully(socket.getInputStream(), b);
                semaphore.release();
                socket.close();
            }
            server.close();
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    noopPublisher(ccf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    ccf.setSerializer(serializer);
    ccf.setDeserializer(serializer);
    ccf.setSoTimeout(5000);
    ccf.start();
    ccf.setSingleUse(true);
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(ccf);
    handler.handleMessage(MessageBuilder.withPayload("Test.1").build());
    handler.handleMessage(MessageBuilder.withPayload("Test.2").build());
    assertTrue(semaphore.tryAcquire(4, 10000, TimeUnit.MILLISECONDS));
    done.set(true);
    ccf.stop();
    serverSocket.get().close();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) AbstractConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) TcpNioClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory) MessagingException(org.springframework.messaging.MessagingException) SocketException(java.net.SocketException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Test(org.junit.Test)

Example 20 with ByteArrayCrLfSerializer

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

the class TcpReceivingChannelAdapterTests method testNioSingleShared.

@Test
public void testNioSingleShared() throws Exception {
    TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(0);
    noopPublisher(scf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    scf.setSerializer(serializer);
    scf.setDeserializer(serializer);
    scf.setSingleUse(true);
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(scf);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(scf);
    scf.start();
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    TestingUtilities.waitListening(scf, null);
    int port = scf.getPort();
    Socket socket1 = SocketFactory.getDefault().createSocket("localhost", port);
    socket1.setSoTimeout(2000);
    socket1.getOutputStream().write("Test1\r\n".getBytes());
    Socket socket2 = SocketFactory.getDefault().createSocket("localhost", port);
    socket2.setSoTimeout(2000);
    socket2.getOutputStream().write("Test2\r\n".getBytes());
    Message<?> message = channel.receive(10000);
    assertNotNull(message);
    handler.handleMessage(message);
    message = channel.receive(10000);
    assertNotNull(message);
    handler.handleMessage(message);
    byte[] b = new byte[7];
    readFully(socket1.getInputStream(), b);
    assertEquals("Test1\r\n", new String(b));
    readFully(socket2.getInputStream(), b);
    assertEquals("Test2\r\n", new String(b));
    scf.stop();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) QueueChannel(org.springframework.integration.channel.QueueChannel) TcpNioServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory) 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