Search in sources :

Example 6 with TcpNetClientConnectionFactory

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

the class TcpSendingMessageHandlerTests method testNetNegotiateSingleNoListen.

@Test
public void testNetNegotiateSingleNoListen() 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();
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            Object in = ois.readObject();
            logger.debug("read object: " + in);
            oos.writeObject("world!");
            ois = new ObjectInputStream(socket.getInputStream());
            oos = new ObjectOutputStream(socket.getOutputStream());
            in = ois.readObject();
            logger.debug("read object: " + in);
            oos.writeObject("world!");
            ois = new ObjectInputStream(socket.getInputStream());
            oos = new ObjectOutputStream(socket.getOutputStream());
            in = ois.readObject();
            oos.writeObject("Reply");
            socket.close();
            server.close();
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    noopPublisher(ccf);
    ccf.setSerializer(new DefaultSerializer());
    ccf.setDeserializer(new DefaultDeserializer());
    ccf.setSoTimeout(10000);
    TcpConnectionInterceptorFactoryChain fc = new TcpConnectionInterceptorFactoryChain();
    fc.setInterceptors(new TcpConnectionInterceptorFactory[] { newInterceptorFactory(), newInterceptorFactory() });
    ccf.setInterceptorFactoryChain(fc);
    ccf.setSingleUse(true);
    ccf.start();
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(ccf);
    handler.handleMessage(MessageBuilder.withPayload("Test").build());
    done.set(true);
    ccf.stop();
    serverSocket.get().close();
}
Also used : AbstractConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ObjectOutputStream(java.io.ObjectOutputStream) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) MessagingException(org.springframework.messaging.MessagingException) SocketException(java.net.SocketException) IOException(java.io.IOException) DefaultSerializer(org.springframework.core.serializer.DefaultSerializer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DefaultDeserializer(org.springframework.core.serializer.DefaultDeserializer) TcpConnectionInterceptorFactoryChain(org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 7 with TcpNetClientConnectionFactory

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

the class TcpSendingMessageHandlerTests method testNetCrLfClientMode.

@Test
public void testNetCrLfClientMode() 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 = ("Reply" + (++i) + "\r\n").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);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    ccf.setSerializer(serializer);
    ccf.setDeserializer(serializer);
    ccf.setSoTimeout(Integer.MAX_VALUE);
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(ccf);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(ccf);
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    handler.setClientMode(true);
    handler.setRetryInterval(10000);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setPoolSize(1);
    taskScheduler.initialize();
    handler.setTaskScheduler(taskScheduler);
    handler.start();
    adapter.start();
    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);
    handler.stop();
    handler.start();
    handler.stop();
    adapter.stop();
    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) 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) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BeanFactory(org.springframework.beans.factory.BeanFactory) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Test(org.junit.Test)

Example 8 with TcpNetClientConnectionFactory

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

the class TcpSendingMessageHandlerTests method testNetSerial.

@Test
public void testNetSerial() 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) {
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                ois.readObject();
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                oos.writeObject("Reply" + (++i));
            }
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    noopPublisher(ccf);
    ccf.setSerializer(new DefaultSerializer());
    ccf.setDeserializer(new DefaultDeserializer());
    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", mOut.getPayload());
    mOut = channel.receive(10000);
    assertNotNull(mOut);
    assertEquals("Reply2", 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) ObjectOutputStream(java.io.ObjectOutputStream) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) MessagingException(org.springframework.messaging.MessagingException) SocketException(java.net.SocketException) IOException(java.io.IOException) DefaultSerializer(org.springframework.core.serializer.DefaultSerializer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DefaultDeserializer(org.springframework.core.serializer.DefaultDeserializer) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 9 with TcpNetClientConnectionFactory

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

the class TcpSendingMessageHandlerTests method testNetSingleUseNoInbound.

@Test
public void testNetSingleUseNoInbound() 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();
    this.executor.execute(() -> {
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
            serverSocket.set(server);
            latch.countDown();
            for (int i = 0; i < 2; i++) {
                Socket socket = server.accept();
                semaphore.release();
                byte[] b = new byte[6];
                readFully(socket.getInputStream(), b);
                semaphore.release();
                socket.close();
            }
            server.close();
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    noopPublisher(ccf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    ccf.setSerializer(serializer);
    ccf.setDeserializer(serializer);
    ccf.setSoTimeout(10000);
    ccf.start();
    ccf.setSingleUse(true);
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(ccf);
    handler.handleMessage(MessageBuilder.withPayload("Test").build());
    handler.handleMessage(MessageBuilder.withPayload("Test").build());
    assertTrue(semaphore.tryAcquire(4, 10000, TimeUnit.MILLISECONDS));
    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) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) 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 10 with TcpNetClientConnectionFactory

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

the class ParserUnitTests method testConnClient2.

@Test
public void testConnClient2() {
    assertTrue(client2 instanceof TcpNetClientConnectionFactory);
    assertEquals("localhost", client1.getHost());
    assertEquals(54, client1.getSoLinger());
    assertEquals(1234, client1.getSoReceiveBufferSize());
    assertEquals(1235, client1.getSoSendBufferSize());
    assertEquals(1236, client1.getSoTimeout());
    assertEquals(12, client1.getSoTrafficClass());
    DirectFieldAccessor dfa = new DirectFieldAccessor(client1);
    assertSame(serializer, dfa.getPropertyValue("serializer"));
    assertSame(deserializer, dfa.getPropertyValue("deserializer"));
    assertEquals(true, dfa.getPropertyValue("soTcpNoDelay"));
    assertEquals(true, dfa.getPropertyValue("singleUse"));
    assertSame(taskExecutor, dfa.getPropertyValue("taskExecutor"));
    assertNotNull(dfa.getPropertyValue("interceptorFactoryChain"));
}
Also used : DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) 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