Search in sources :

Example 6 with ByteArrayLengthHeaderSerializer

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

the class TcpNioConnectionReadTests method testReadLengthOverflow.

@Test
public void testReadLengthOverflow() throws Exception {
    ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
    final Semaphore semaphore = new Semaphore(0);
    final List<TcpConnection> added = new ArrayList<TcpConnection>();
    final List<TcpConnection> removed = new ArrayList<TcpConnection>();
    final CountDownLatch errorMessageLetch = new CountDownLatch(1);
    final AtomicReference<Throwable> errorMessageRef = new AtomicReference<Throwable>();
    AbstractServerConnectionFactory scf = getConnectionFactory(serializer, message -> {
        if (message instanceof ErrorMessage) {
            errorMessageRef.set(((ErrorMessage) message).getPayload());
            errorMessageLetch.countDown();
        }
        return false;
    }, new TcpSender() {

        @Override
        public void addNewConnection(TcpConnection connection) {
            added.add(connection);
            semaphore.release();
        }

        @Override
        public void removeDeadConnection(TcpConnection connection) {
            removed.add(connection);
            semaphore.release();
        }
    });
    // Fire up the sender.
    CountDownLatch done = SocketTestUtils.testSendLengthOverflow(scf.getPort());
    whileOpen(semaphore, added);
    assertEquals(1, added.size());
    assertTrue(errorMessageLetch.await(10, TimeUnit.SECONDS));
    assertThat(errorMessageRef.get().getMessage(), anyOf(containsString("Message length 2147483647 exceeds max message length: 2048"), containsString("Connection is closed")));
    assertTrue(semaphore.tryAcquire(10000, TimeUnit.MILLISECONDS));
    assertTrue(removed.size() > 0);
    scf.stop();
    done.countDown();
}
Also used : ByteArrayLengthHeaderSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) ErrorMessage(org.springframework.messaging.support.ErrorMessage) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 7 with ByteArrayLengthHeaderSerializer

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

the class TcpNioConnectionWriteTests method testWriteLengthHeaderDirect.

@Test
public void testWriteLengthHeaderDirect() 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 {
            ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
            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() + 4];
    readFully(is, buff);
    ByteBuffer buffer = ByteBuffer.wrap(buff);
    assertEquals(testString.length(), buffer.getInt());
    assertEquals(testString, new String(buff, 4, testString.length()));
    server.close();
    latch.countDown();
}
Also used : InputStream(java.io.InputStream) ByteArrayLengthHeaderSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer) ServerSocket(java.net.ServerSocket) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 8 with ByteArrayLengthHeaderSerializer

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

the class TcpNioConnectionWriteTests method testWriteLengthHeader.

@Test
public void testWriteLengthHeader() 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 {
            ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
            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() + 4];
    readFully(is, buff);
    ByteBuffer buffer = ByteBuffer.wrap(buff);
    assertEquals(testString.length(), buffer.getInt());
    assertEquals(testString, new String(buff, 4, testString.length()));
    server.close();
    latch.countDown();
}
Also used : InputStream(java.io.InputStream) ByteArrayLengthHeaderSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer) ServerSocket(java.net.ServerSocket) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 9 with ByteArrayLengthHeaderSerializer

use of org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer in project faf-java-server by FAForever.

the class LegacyAdapterConfig method tcpServerConnectionFactory.

/**
 * Non-blocking TCP connection factory that deserializes into byte array messages.
 */
@Bean
public TcpNioServerConnectionFactory tcpServerConnectionFactory() {
    ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
    serializer.setMaxMessageSize(100 * 1024);
    serializer.setApplicationEventPublisher(applicationEventPublisher);
    TcpNioServerConnectionFactory connectionFactory = new TcpNioServerConnectionFactory(serverProperties.getPort());
    connectionFactory.setDeserializer(serializer);
    connectionFactory.setSerializer(serializer);
    connectionFactory.setUsingDirectBuffers(true);
    connectionFactory.getMapper().setApplySequence(true);
    // See https://docs.spring.io/spring-integration/reference/html/ip.html#_thread_pool_task_executor_with_caller_runs_policy
    connectionFactory.setTaskExecutor(new CompositeExecutor(createNioTaskExecutor("legacy-io-"), createNioTaskExecutor("legacy-assembler-")));
    return connectionFactory;
}
Also used : CompositeExecutor(org.springframework.integration.util.CompositeExecutor) TcpNioServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory) ByteArrayLengthHeaderSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer) Bean(org.springframework.context.annotation.Bean)

Aggregations

ByteArrayLengthHeaderSerializer (org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer)9 Test (org.junit.Test)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 IOException (java.io.IOException)4 ServerSocket (java.net.ServerSocket)4 Socket (java.net.Socket)4 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)4 ArrayList (java.util.ArrayList)3 Semaphore (java.util.concurrent.Semaphore)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 ErrorMessage (org.springframework.messaging.support.ErrorMessage)3 InputStream (java.io.InputStream)2 SocketException (java.net.SocketException)2 ByteBuffer (java.nio.ByteBuffer)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 AbstractConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory)2 Message (org.springframework.messaging.Message)2 MessagingException (org.springframework.messaging.MessagingException)2