Search in sources :

Example 6 with ByteArrayCrLfSerializer

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

the class TcpReceivingChannelAdapterTests method testNet.

@Test
public void testNet() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(0);
    noopPublisher(scf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    scf.setSerializer(serializer);
    scf.setDeserializer(serializer);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(scf);
    scf.start();
    TestingUtilities.waitListening(scf, null);
    int port = scf.getPort();
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.getOutputStream().write("Test1\r\n".getBytes());
    socket.getOutputStream().write("Test2\r\n".getBytes());
    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()));
    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) BeanFactory(org.springframework.beans.factory.BeanFactory) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 7 with ByteArrayCrLfSerializer

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

the class TcpReceivingChannelAdapterTests method testNioSingleNoOutbound.

@Test
public void testNioSingleNoOutbound() throws Exception {
    TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(0);
    noopPublisher(scf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    scf.setSerializer(serializer);
    scf.setDeserializer(serializer);
    scf.setSingleUse(true);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(scf);
    scf.start();
    TestingUtilities.waitListening(scf, null);
    int port = scf.getPort();
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.getOutputStream().write("Test1\r\n".getBytes());
    socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.getOutputStream().write("Test2\r\n".getBytes());
    Message<?> message = channel.receive(60000);
    assertNotNull(message);
    // with single use, results may come back in a different order
    Set<String> results = new HashSet<String>();
    results.add(new String((byte[]) message.getPayload()));
    message = channel.receive(10000);
    assertNotNull(message);
    results.add(new String((byte[]) message.getPayload()));
    assertTrue(results.contains("Test1"));
    assertTrue(results.contains("Test2"));
    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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with ByteArrayCrLfSerializer

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

the class TcpReceivingChannelAdapterTests method testNio.

@Test
public void testNio() throws Exception {
    TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(0);
    noopPublisher(scf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    scf.setSerializer(serializer);
    scf.setDeserializer(serializer);
    scf.setSoTimeout(5000);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(scf);
    scf.start();
    TestingUtilities.waitListening(scf, null);
    int port = scf.getPort();
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
    for (int i = 0; i < 1000; i++) {
        socket.getOutputStream().write(("Test" + i + "\r\n").getBytes());
    }
    Set<String> results = new HashSet<String>();
    for (int i = 0; i < 1000; i++) {
        Message<?> message = channel.receive(10000);
        assertNotNull(message);
        results.add(new String((byte[]) message.getPayload()));
    }
    for (int i = 0; i < 1000; i++) {
        assertTrue(results.remove("Test" + i));
    }
    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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with ByteArrayCrLfSerializer

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

the class TcpReceivingChannelAdapterTests method testNetSingleNoOutbound.

@Test
public void testNetSingleNoOutbound() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(0);
    noopPublisher(scf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    scf.setSerializer(serializer);
    scf.setDeserializer(serializer);
    scf.setSingleUse(true);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(scf);
    scf.start();
    TestingUtilities.waitListening(scf, null);
    int port = scf.getPort();
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.getOutputStream().write("Test1\r\n".getBytes());
    socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.getOutputStream().write("Test2\r\n".getBytes());
    Message<?> message = channel.receive(10000);
    assertNotNull(message);
    // with single use, results may come back in a different order
    Set<String> results = new HashSet<String>();
    results.add(new String((byte[]) message.getPayload()));
    message = channel.receive(10000);
    assertNotNull(message);
    results.add(new String((byte[]) message.getPayload()));
    assertTrue(results.contains("Test1"));
    assertTrue(results.contains("Test2"));
    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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with ByteArrayCrLfSerializer

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

the class TcpReceivingChannelAdapterTests method testNioSingleSharedMany.

@Test
public void testNioSingleSharedMany() throws Exception {
    TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(0);
    noopPublisher(scf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    scf.setSerializer(serializer);
    scf.setDeserializer(serializer);
    scf.setSingleUse(true);
    scf.setBacklog(100);
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(scf);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(scf);
    Executor te = new SimpleAsyncTaskExecutor();
    scf.setTaskExecutor(te);
    scf.start();
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    TestingUtilities.waitListening(scf, null);
    int port = scf.getPort();
    List<Socket> sockets = new LinkedList<Socket>();
    for (int i = 100; i < 200; i++) {
        Socket socket1 = SocketFactory.getDefault().createSocket("localhost", port);
        socket1.setSoTimeout(2000);
        socket1.getOutputStream().write(("Test" + i + "\r\n").getBytes());
        sockets.add(socket1);
    }
    for (int i = 100; i < 200; i++) {
        Message<?> message = channel.receive(60000);
        assertNotNull(message);
        handler.handleMessage(message);
    }
    byte[] b = new byte[9];
    for (int i = 100; i < 200; i++) {
        readFully(sockets.remove(0).getInputStream(), b);
        assertEquals("Test" + i + "\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) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) LinkedList(java.util.LinkedList) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) Executor(java.util.concurrent.Executor) 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