Search in sources :

Example 11 with TcpNioClientConnectionFactory

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

the class TcpOutboundGatewayTests method testNioGWPropagatesSocketTimeoutSingleUse.

@Test
public void testNioGWPropagatesSocketTimeoutSingleUse() throws Exception {
    ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(0);
    final int port = serverSocket.getLocalPort();
    AbstractClientConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
    ccf.setSerializer(new DefaultSerializer());
    ccf.setDeserializer(new DefaultDeserializer());
    ccf.setSoTimeout(100);
    ccf.setSingleUse(true);
    ccf.start();
    testGWPropagatesSocketTimeoutGuts(port, ccf, 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) TcpNioClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 12 with TcpNioClientConnectionFactory

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

the class TcpOutboundGatewayTests method testNioGWPropagatesSocketClose.

@Test
public void testNioGWPropagatesSocketClose() throws Exception {
    ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(0);
    final int port = serverSocket.getLocalPort();
    AbstractClientConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
    ccf.setSerializer(new DefaultSerializer());
    ccf.setDeserializer(new DefaultDeserializer());
    ccf.setSoTimeout(10000);
    ccf.setSingleUse(false);
    ccf.start();
    testGWPropagatesSocketCloseGuts(port, ccf, 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) TcpNioClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 13 with TcpNioClientConnectionFactory

use of org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory 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)

Example 14 with TcpNioClientConnectionFactory

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

the class TcpSendingMessageHandlerTests method testNioNegotiate.

@Test
public void testNioNegotiate() 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 = 100;
            while (true) {
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Object in;
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                if (i == 100) {
                    in = ois.readObject();
                    logger.debug("read object: " + in);
                    oos.writeObject("world!");
                    ois = new ObjectInputStream(socket.getInputStream());
                    oos = new ObjectOutputStream(socket.getOutputStream());
                    Thread.sleep(500);
                }
                in = ois.readObject();
                oos.writeObject("Reply" + (i++));
            }
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("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() });
    ccf.setInterceptorFactoryChain(fc);
    ccf.start();
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(ccf);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(ccf);
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    for (int i = 0; i < 1000; i++) {
        handler.handleMessage(MessageBuilder.withPayload("Test").build());
    }
    Set<String> results = new TreeSet<String>();
    for (int i = 0; i < 1000; i++) {
        Message<?> mOut = channel.receive(10000);
        assertNotNull(mOut);
        results.add((String) mOut.getPayload());
    }
    logger.debug("results: " + results);
    for (int i = 100; i < 1100; i++) {
        assertTrue("Missing Reply" + i, results.remove("Reply" + i));
    }
    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) TcpNioClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory) 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) TreeSet(java.util.TreeSet) 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 15 with TcpNioClientConnectionFactory

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

the class DeserializationTests method testTimeoutWhileDecoding.

public void testTimeoutWhileDecoding(AbstractByteArraySerializer deserializer, String reply) throws Exception {
    ByteArrayRawSerializer serializer = new ByteArrayRawSerializer();
    TcpNioServerConnectionFactory serverNio = new TcpNioServerConnectionFactory(0);
    ByteArrayLengthHeaderSerializer lengthHeaderSerializer = new ByteArrayLengthHeaderSerializer(1);
    serverNio.setDeserializer(lengthHeaderSerializer);
    serverNio.setSerializer(serializer);
    serverNio.afterPropertiesSet();
    TcpInboundGateway in = new TcpInboundGateway();
    in.setConnectionFactory(serverNio);
    QueueChannel serverSideChannel = new QueueChannel();
    in.setRequestChannel(serverSideChannel);
    in.setBeanFactory(mock(BeanFactory.class));
    in.afterPropertiesSet();
    in.start();
    TestingUtilities.waitListening(serverNio, null);
    TcpNioClientConnectionFactory clientNio = new TcpNioClientConnectionFactory("localhost", serverNio.getPort());
    clientNio.setSerializer(serializer);
    clientNio.setDeserializer(deserializer);
    clientNio.setSoTimeout(1000);
    clientNio.afterPropertiesSet();
    final TcpOutboundGateway out = new TcpOutboundGateway();
    out.setConnectionFactory(clientNio);
    QueueChannel outputChannel = new QueueChannel();
    out.setOutputChannel(outputChannel);
    out.setRemoteTimeout(60000);
    out.setBeanFactory(mock(BeanFactory.class));
    out.afterPropertiesSet();
    out.start();
    Runnable command = () -> {
        try {
            out.handleMessage(MessageBuilder.withPayload("\u0004Test").build());
        } catch (Exception e) {
        // eat SocketTimeoutException. Doesn't matter for this test
        }
    };
    Executor exec = new SimpleAsyncTaskExecutor();
    Message<?> message;
    // short reply should not be received.
    exec.execute(command);
    message = serverSideChannel.receive(10000);
    assertNotNull(message);
    assertEquals("Test", new String((byte[]) message.getPayload()));
    String shortReply = reply.substring(0, reply.length() - 1);
    ((MessageChannel) message.getHeaders().getReplyChannel()).send(new GenericMessage<String>(shortReply));
    message = outputChannel.receive(6000);
    assertNull(message);
    // good message should be received
    if ((deserializer instanceof ByteArrayRawSerializer)) {
        // restore old behavior
        clientNio.setDeserializer(new ByteArrayRawSerializer(true));
    }
    exec.execute(command);
    message = serverSideChannel.receive(10000);
    assertNotNull(message);
    assertEquals("Test", new String((byte[]) message.getPayload()));
    ((MessageChannel) message.getHeaders().getReplyChannel()).send(new GenericMessage<String>(reply));
    message = outputChannel.receive(10000);
    assertNotNull(message);
    assertEquals(reply, new String(((byte[]) message.getPayload())));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) TcpNioServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) Matchers.containsString(org.hamcrest.Matchers.containsString) TcpNioClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory) TcpOutboundGateway(org.springframework.integration.ip.tcp.TcpOutboundGateway) IOException(java.io.IOException) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) Executor(java.util.concurrent.Executor) MessageChannel(org.springframework.messaging.MessageChannel) TcpInboundGateway(org.springframework.integration.ip.tcp.TcpInboundGateway) BeanFactory(org.springframework.beans.factory.BeanFactory)

Aggregations

TcpNioClientConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory)16 Test (org.junit.Test)14 ServerSocket (java.net.ServerSocket)12 IOException (java.io.IOException)10 Socket (java.net.Socket)9 SocketException (java.net.SocketException)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 AbstractConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory)9 MessagingException (org.springframework.messaging.MessagingException)9 QueueChannel (org.springframework.integration.channel.QueueChannel)8 HashSet (java.util.HashSet)6 DefaultDeserializer (org.springframework.core.serializer.DefaultDeserializer)6 DefaultSerializer (org.springframework.core.serializer.DefaultSerializer)6 AbstractClientConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory)4 ByteArrayCrLfSerializer (org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer)4 ObjectInputStream (java.io.ObjectInputStream)3 ObjectOutputStream (java.io.ObjectOutputStream)3 Semaphore (java.util.concurrent.Semaphore)3