Search in sources :

Example 1 with ByteArrayLengthHeaderSerializer

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

the class TcpSendingMessageHandlerTests method testNioLength.

@Test
public void testNioLength() throws Exception {
    final AtomicReference<ServerSocket> serverSocket = new AtomicReference<ServerSocket>();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean done = new AtomicBoolean();
    this.executor.execute(() -> {
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
            serverSocket.set(server);
            latch.countDown();
            Socket socket = server.accept();
            int i = 0;
            while (true) {
                byte[] b = new byte[8];
                readFully(socket.getInputStream(), b);
                if (!"\u0000\u0000\u0000\u0004Test".equals(new String(b))) {
                    throw new RuntimeException("Bad Data");
                }
                b = ("\u0000\u0000\u0000\u0006Reply" + (++i)).getBytes();
                socket.getOutputStream().write(b);
            }
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    noopPublisher(ccf);
    ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
    ccf.setSerializer(serializer);
    ccf.setDeserializer(serializer);
    ccf.setSoTimeout(10000);
    ccf.start();
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(ccf);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(ccf);
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    handler.handleMessage(MessageBuilder.withPayload("Test").build());
    handler.handleMessage(MessageBuilder.withPayload("Test").build());
    Set<String> results = new HashSet<String>();
    Message<?> mOut = channel.receive(10000);
    assertNotNull(mOut);
    results.add(new String((byte[]) mOut.getPayload()));
    mOut = channel.receive(10000);
    assertNotNull(mOut);
    results.add(new String((byte[]) mOut.getPayload()));
    assertTrue(results.remove("Reply1"));
    assertTrue(results.remove("Reply2"));
    done.set(true);
    ccf.stop();
    serverSocket.get().close();
}
Also used : AbstractConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory) QueueChannel(org.springframework.integration.channel.QueueChannel) ByteArrayLengthHeaderSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with ByteArrayLengthHeaderSerializer

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

the class TcpSendingMessageHandlerTests method testNetLength.

@Test
public void testNetLength() throws Exception {
    final AtomicReference<ServerSocket> serverSocket = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean done = new AtomicBoolean();
    this.executor.execute(() -> {
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
            serverSocket.set(server);
            latch.countDown();
            Socket socket = server.accept();
            int i = 0;
            while (true) {
                byte[] b = new byte[8];
                readFully(socket.getInputStream(), b);
                if (!"\u0000\u0000\u0000\u0004Test".equals(new String(b))) {
                    throw new RuntimeException("Bad Data");
                }
                b = ("\u0000\u0000\u0000\u0006Reply" + (++i)).getBytes();
                socket.getOutputStream().write(b);
            }
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    noopPublisher(ccf);
    ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
    ccf.setSerializer(serializer);
    ccf.setDeserializer(serializer);
    ccf.setSoTimeout(10000);
    ccf.start();
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(ccf);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(ccf);
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    handler.handleMessage(MessageBuilder.withPayload("Test").build());
    handler.handleMessage(MessageBuilder.withPayload("Test").build());
    Message<?> mOut = channel.receive(10000);
    assertNotNull(mOut);
    assertEquals("Reply1", new String((byte[]) mOut.getPayload()));
    mOut = channel.receive(10000);
    assertNotNull(mOut);
    assertEquals("Reply2", new String((byte[]) mOut.getPayload()));
    done.set(true);
    ccf.stop();
    serverSocket.get().close();
}
Also used : AbstractConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory) QueueChannel(org.springframework.integration.channel.QueueChannel) ByteArrayLengthHeaderSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) 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 3 with ByteArrayLengthHeaderSerializer

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

the class TcpNioConnectionReadTests method testFragmented.

@SuppressWarnings("unchecked")
@Test
public void testFragmented() throws Exception {
    ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
    final List<Message<?>> responses = new ArrayList<Message<?>>();
    final Semaphore semaphore = new Semaphore(0);
    AbstractServerConnectionFactory scf = getConnectionFactory(serializer, message -> {
        responses.add(message);
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        semaphore.release();
        return false;
    });
    int howMany = 2;
    scf.setBacklog(howMany + 5);
    // Fire up the sender.
    CountDownLatch done = SocketTestUtils.testSendFragmented(scf.getPort(), howMany, false);
    assertTrue(semaphore.tryAcquire(howMany, 20000, TimeUnit.MILLISECONDS));
    assertEquals("Expected", howMany, responses.size());
    for (int i = 0; i < howMany; i++) {
        assertEquals("Data", "xx", new String(((Message<byte[]>) responses.get(0)).getPayload()));
    }
    scf.stop();
    done.countDown();
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) ByteArrayLengthHeaderSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer) 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 4 with ByteArrayLengthHeaderSerializer

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

the class TcpNioConnectionReadTests method testCloseCleanupLengthHeader.

/**
 * Tests socket closure when mid-message
 * @throws Exception
 */
@Test
public void testCloseCleanupLengthHeader() throws Exception {
    ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
    testClosureMidMessageGuts(serializer, "\u0000\u0000\u0000\u0003xx");
}
Also used : ByteArrayLengthHeaderSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 5 with ByteArrayLengthHeaderSerializer

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

the class TcpNioConnectionReadTests method testReadLength.

@Test
public void testReadLength() throws Exception {
    ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
    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.testSendLength(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((byte[]) responses.get(0).getPayload()));
    assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, new String((byte[]) responses.get(1).getPayload()));
    scf.stop();
    done.countDown();
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) ByteArrayLengthHeaderSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer) 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)

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