Search in sources :

Example 6 with ByteArrayStxEtxSerializer

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

the class TcpSendingMessageHandlerTests method testNioStxEtx.

@Test
public void testNioStxEtx() 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[6];
                readFully(socket.getInputStream(), b);
                b = ("\u0002Reply" + (++i) + "\u0003").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);
    ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer();
    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) 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) ByteArrayStxEtxSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with ByteArrayStxEtxSerializer

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

the class TcpNioConnectionReadTests method testReadStxEtxOverflow.

@Test
public void testReadStxEtxOverflow() throws Exception {
    ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer();
    serializer.setMaxMessageSize(1024);
    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.testSendStxEtxOverflow(scf.getPort());
    whileOpen(semaphore, added);
    assertEquals(1, added.size());
    assertTrue(errorMessageLetch.await(10, TimeUnit.SECONDS));
    assertThat(errorMessageRef.get().getMessage(), anyOf(containsString("Connection is closed"), containsString("ETX not found before max message length: 1024")));
    assertTrue(semaphore.tryAcquire(10000, TimeUnit.MILLISECONDS));
    assertTrue(removed.size() > 0);
    scf.stop();
    done.countDown();
}
Also used : ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) ByteArrayStxEtxSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer) ErrorMessage(org.springframework.messaging.support.ErrorMessage) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)7 ByteArrayStxEtxSerializer (org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Socket (java.net.Socket)5 IOException (java.io.IOException)4 ServerSocket (java.net.ServerSocket)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 InputStream (java.io.InputStream)3 SocketException (java.net.SocketException)2 ArrayList (java.util.ArrayList)2 Semaphore (java.util.concurrent.Semaphore)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 AbstractConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory)2 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)2 MessagingException (org.springframework.messaging.MessagingException)2 ErrorMessage (org.springframework.messaging.support.ErrorMessage)2 PipedInputStream (java.io.PipedInputStream)1 HashSet (java.util.HashSet)1 Log (org.apache.commons.logging.Log)1