Search in sources :

Example 11 with TcpNetClientConnectionFactory

use of org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory in project spring-integration by spring-projects.

the class ConnectionFacforyTests method shouldReturnNetFlavor.

@Test
public void shouldReturnNetFlavor() throws Exception {
    AbstractServerConnectionFactory server = Tcp.netServer(0).get();
    assertTrue(server instanceof TcpNetServerConnectionFactory);
    AbstractClientConnectionFactory client = Tcp.netClient("localhost", server.getPort()).get();
    assertTrue(client instanceof TcpNetClientConnectionFactory);
}
Also used : AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) TcpNetServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) Test(org.junit.Test)

Example 12 with TcpNetClientConnectionFactory

use of org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory in project spring-integration by spring-projects.

the class TcpSendingMessageHandlerTests method testNetStxEtx.

@Test
public void testNetStxEtx() 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 TcpNetClientConnectionFactory("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());
    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) 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) ByteArrayStxEtxSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Test(org.junit.Test)

Example 13 with TcpNetClientConnectionFactory

use of org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory in project spring-integration by spring-projects.

the class TcpInboundGatewayTests method testNetClientMode.

@Test
public void testNetClientMode() throws Exception {
    final AtomicInteger port = new AtomicInteger();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final CountDownLatch latch3 = new CountDownLatch(1);
    final AtomicBoolean done = new AtomicBoolean();
    new SimpleAsyncTaskExecutor().execute(() -> {
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0, 10);
            port.set(server.getLocalPort());
            latch1.countDown();
            Socket socket = server.accept();
            socket.getOutputStream().write("Test1\r\nTest2\r\n".getBytes());
            byte[] bytes = new byte[12];
            readFully(socket.getInputStream(), bytes);
            assertEquals("Echo:Test1\r\n", new String(bytes));
            readFully(socket.getInputStream(), bytes);
            assertEquals("Echo:Test2\r\n", new String(bytes));
            latch2.await();
            socket.close();
            server.close();
            done.set(true);
            latch3.countDown();
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port.get());
    ccf.setSingleUse(false);
    TcpInboundGateway gateway = new TcpInboundGateway();
    gateway.setConnectionFactory(ccf);
    final QueueChannel channel = new QueueChannel();
    gateway.setRequestChannel(channel);
    gateway.setClientMode(true);
    gateway.setRetryInterval(10000);
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.afterPropertiesSet();
    ServiceActivatingHandler handler = new ServiceActivatingHandler(new Service());
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setPoolSize(1);
    taskScheduler.initialize();
    gateway.setTaskScheduler(taskScheduler);
    gateway.start();
    Message<?> message = channel.receive(10000);
    assertNotNull(message);
    handler.handleMessage(message);
    message = channel.receive(10000);
    assertNotNull(message);
    handler.handleMessage(message);
    latch2.countDown();
    assertTrue(latch3.await(10, TimeUnit.SECONDS));
    assertTrue(done.get());
    gateway.stop();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) ServerSocket(java.net.ServerSocket) 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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeanFactory(org.springframework.beans.factory.BeanFactory) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Test(org.junit.Test)

Example 14 with TcpNetClientConnectionFactory

use of org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory 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 15 with TcpNetClientConnectionFactory

use of org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory in project spring-integration by spring-projects.

the class TcpOutboundGatewayTests method testFailoverGWPropagatesSocketClose.

@Test
public void testFailoverGWPropagatesSocketClose() throws Exception {
    ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(0);
    final int port = serverSocket.getLocalPort();
    AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
    ccf.setSerializer(new DefaultSerializer());
    ccf.setDeserializer(new DefaultDeserializer());
    ccf.setSoTimeout(10000);
    ccf.setSingleUse(false);
    FailoverClientConnectionFactory focf = new FailoverClientConnectionFactory(Collections.singletonList(ccf));
    focf.start();
    testGWPropagatesSocketCloseGuts(port, focf, serverSocket);
    serverSocket.close();
}
Also used : AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) DefaultSerializer(org.springframework.core.serializer.DefaultSerializer) DefaultDeserializer(org.springframework.core.serializer.DefaultDeserializer) ServerSocket(java.net.ServerSocket) FailoverClientConnectionFactory(org.springframework.integration.ip.tcp.connection.FailoverClientConnectionFactory) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Aggregations

TcpNetClientConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory)25 Test (org.junit.Test)23 ServerSocket (java.net.ServerSocket)21 IOException (java.io.IOException)16 Socket (java.net.Socket)16 CountDownLatch (java.util.concurrent.CountDownLatch)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 DefaultDeserializer (org.springframework.core.serializer.DefaultDeserializer)14 DefaultSerializer (org.springframework.core.serializer.DefaultSerializer)14 QueueChannel (org.springframework.integration.channel.QueueChannel)14 AbstractClientConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory)14 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)10 SocketException (java.net.SocketException)9 AbstractConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory)9 MessagingException (org.springframework.messaging.MessagingException)9 ObjectInputStream (java.io.ObjectInputStream)8 ObjectOutputStream (java.io.ObjectOutputStream)8 EOFException (java.io.EOFException)5 SocketTimeoutException (java.net.SocketTimeoutException)5