Search in sources :

Example 21 with ByteArrayCrLfSerializer

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

the class TcpReceivingChannelAdapterTests method testNetShared.

@Test
public void testNetShared() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(0);
    noopPublisher(scf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    scf.setSerializer(serializer);
    scf.setDeserializer(serializer);
    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 socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.setSoTimeout(2000);
    socket.getOutputStream().write("Test\r\n".getBytes());
    socket.getOutputStream().write("Test\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[6];
    readFully(socket.getInputStream(), b);
    assertEquals("Test\r\n", new String(b));
    readFully(socket.getInputStream(), b);
    assertEquals("Test\r\n", new String(b));
    scf.stop();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) QueueChannel(org.springframework.integration.channel.QueueChannel) TcpNetServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 22 with ByteArrayCrLfSerializer

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

the class TcpReceivingChannelAdapterTests method testNetClientMode.

@Test
public void testNetClientMode() throws Exception {
    final AtomicReference<ServerSocket> serverSocket = new AtomicReference<>();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final AtomicBoolean done = new AtomicBoolean();
    new SimpleAsyncTaskExecutor().execute(() -> {
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0, 10);
            serverSocket.set(server);
            latch1.countDown();
            Socket socket = server.accept();
            socket.getOutputStream().write("Test1\r\nTest2\r\n".getBytes());
            latch2.await();
            socket.close();
            server.close();
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    noopPublisher(ccf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    ccf.setSerializer(serializer);
    ccf.setDeserializer(serializer);
    ccf.setSoTimeout(Integer.MAX_VALUE);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(ccf);
    adapter.setClientMode(true);
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    adapter.setRetryInterval(10000);
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setPoolSize(1);
    taskScheduler.initialize();
    adapter.setTaskScheduler(taskScheduler);
    adapter.start();
    Message<?> message = channel.receive(10000);
    assertNotNull(message);
    assertEquals("Test1", new String((byte[]) message.getPayload()));
    message = channel.receive(10000);
    assertNotNull(message);
    assertEquals("Test2", new String((byte[]) message.getPayload()));
    adapter.stop();
    adapter.start();
    adapter.stop();
    latch2.countDown();
    ccf.stop();
    serverSocket.get().close();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) QueueChannel(org.springframework.integration.channel.QueueChannel) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) IOException(java.io.IOException) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BeanFactory(org.springframework.beans.factory.BeanFactory) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 23 with ByteArrayCrLfSerializer

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

the class CachingClientConnectionFactoryTests method mockedTcpNioConnection.

private TcpConnectionSupport mockedTcpNioConnection() throws Exception {
    SocketChannel socketChannel = mock(SocketChannel.class);
    new DirectFieldAccessor(socketChannel).setPropertyValue("open", false);
    doThrow(new IOException("Foo")).when(socketChannel).write(Mockito.any(ByteBuffer.class));
    when(socketChannel.socket()).thenReturn(mock(Socket.class));
    TcpNioConnection conn = new TcpNioConnection(socketChannel, false, false, new ApplicationEventPublisher() {

        @Override
        public void publishEvent(ApplicationEvent event) {
        }

        @Override
        public void publishEvent(Object event) {
        }
    }, "foo");
    conn.setMapper(new TcpMessageMapper());
    conn.setSerializer(new ByteArrayCrLfSerializer());
    return conn;
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) ApplicationEvent(org.springframework.context.ApplicationEvent) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Socket(java.net.Socket)

Example 24 with ByteArrayCrLfSerializer

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

the class CachingClientConnectionFactoryTests method mockedTcpNetConnection.

private TcpConnectionSupport mockedTcpNetConnection() throws IOException {
    Socket socket = mock(Socket.class);
    // closed when next retrieved
    when(socket.isClosed()).thenReturn(true);
    OutputStream stream = mock(OutputStream.class);
    doThrow(new IOException("Foo")).when(stream).write(any(byte[].class), anyInt(), anyInt());
    when(socket.getOutputStream()).thenReturn(stream);
    TcpNetConnection conn = new TcpNetConnection(socket, false, false, new ApplicationEventPublisher() {

        @Override
        public void publishEvent(ApplicationEvent event) {
        }

        @Override
        public void publishEvent(Object event) {
        }
    }, "foo");
    conn.setMapper(new TcpMessageMapper());
    conn.setSerializer(new ByteArrayCrLfSerializer());
    return conn;
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) OutputStream(java.io.OutputStream) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) ApplicationEvent(org.springframework.context.ApplicationEvent) IOException(java.io.IOException) Socket(java.net.Socket)

Example 25 with ByteArrayCrLfSerializer

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

the class TcpSendingMessageHandlerTests method testNioSingleUseWithInboundMany.

@Test
public void testNioSingleUseWithInboundMany() 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();
    final List<Socket> serverSockets = new ArrayList<Socket>();
    this.executor.execute(() -> {
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0, 100);
            serverSocket.set(server);
            latch.countDown();
            for (int i = 0; i < 100; i++) {
                final Socket socket = server.accept();
                serverSockets.add(socket);
                final int j = i;
                this.executor.execute(() -> {
                    semaphore.release();
                    byte[] b = new byte[9];
                    try {
                        readFully(socket.getInputStream(), b);
                        b = ("Reply" + j + "\r\n").getBytes();
                        socket.getOutputStream().write(b);
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    } finally {
                        try {
                            socket.close();
                        } catch (IOException e2) {
                        }
                    }
                });
            }
            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(10000);
    ccf.setSingleUse(true);
    ccf.setTaskExecutor(this.executor);
    ccf.start();
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(ccf);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(ccf);
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    int i = 0;
    try {
        for (i = 100; i < 200; i++) {
            handler.handleMessage(MessageBuilder.withPayload("Test" + i).build());
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception at " + i);
    }
    assertTrue(semaphore.tryAcquire(100, 20000, TimeUnit.MILLISECONDS));
    Set<String> replies = new HashSet<String>();
    for (i = 100; i < 200; i++) {
        Message<?> mOut = channel.receive(20000);
        assertNotNull(mOut);
        replies.add(new String((byte[]) mOut.getPayload()));
    }
    for (i = 0; i < 100; i++) {
        assertTrue("Reply" + i + " missing", replies.remove("Reply" + i));
    }
    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) QueueChannel(org.springframework.integration.channel.QueueChannel) ArrayList(java.util.ArrayList) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) IOException(java.io.IOException) 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)

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