Search in sources :

Example 16 with TcpNetClientConnectionFactory

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

the class TcpOutboundGatewayTests method testGoodNetTimeout.

@Test
public void testGoodNetTimeout() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean done = new AtomicBoolean();
    final AtomicReference<ServerSocket> serverSocket = new AtomicReference<>();
    this.executor.execute(() -> {
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
            serverSocket.set(server);
            latch.countDown();
            int i = 0;
            Socket socket = server.accept();
            while (true) {
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                ois.readObject();
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                Thread.sleep(1000);
                oos.writeObject("Reply" + (i++));
            }
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    ccf.setSerializer(new DefaultSerializer());
    ccf.setDeserializer(new DefaultDeserializer());
    ccf.setSoTimeout(10000);
    ccf.setSingleUse(false);
    ccf.start();
    final TcpOutboundGateway gateway = new TcpOutboundGateway();
    gateway.setConnectionFactory(ccf);
    gateway.setRequestTimeout(1);
    QueueChannel replyChannel = new QueueChannel();
    gateway.setRequiresReply(true);
    gateway.setOutputChannel(replyChannel);
    @SuppressWarnings("unchecked") Future<Integer>[] results = (Future<Integer>[]) new Future<?>[2];
    for (int i = 0; i < 2; i++) {
        final int j = i;
        results[j] = (this.executor.submit(() -> {
            gateway.handleMessage(MessageBuilder.withPayload("Test" + j).build());
            return 0;
        }));
    }
    Set<String> replies = new HashSet<>();
    int timeouts = 0;
    for (int i = 0; i < 2; i++) {
        try {
            results[i].get();
        } catch (ExecutionException e) {
            if (timeouts > 0) {
                fail("Unexpected " + e.getMessage());
            } else {
                assertNotNull(e.getCause());
                assertTrue(e.getCause() instanceof MessageTimeoutException);
            }
            timeouts++;
            continue;
        }
        Message<?> m = replyChannel.receive(10000);
        assertNotNull(m);
        replies.add((String) m.getPayload());
    }
    if (timeouts < 1) {
        fail("Expected ExecutionException");
    }
    for (int i = 0; i < 1; i++) {
        assertTrue(replies.remove("Reply" + i));
    }
    done.set(true);
    gateway.stop();
    ccf.stop();
    serverSocket.get().close();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) ObjectOutputStream(java.io.ObjectOutputStream) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) DefaultSerializer(org.springframework.core.serializer.DefaultSerializer) DefaultDeserializer(org.springframework.core.serializer.DefaultDeserializer) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) EOFException(java.io.EOFException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Future(java.util.concurrent.Future) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ObjectInputStream(java.io.ObjectInputStream) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 17 with TcpNetClientConnectionFactory

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

the class TcpOutboundGatewayTests method buildCF.

private AbstractClientConnectionFactory buildCF(final int port) {
    AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
    ccf.setSerializer(new DefaultSerializer());
    ccf.setDeserializer(new DefaultDeserializer());
    ccf.setSoTimeout(10000);
    ccf.setSingleUse(false);
    return ccf;
}
Also used : AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) DefaultSerializer(org.springframework.core.serializer.DefaultSerializer) DefaultDeserializer(org.springframework.core.serializer.DefaultDeserializer) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory)

Example 18 with TcpNetClientConnectionFactory

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

the class TcpOutboundGatewayTests method testGoodNetSingle.

@Test
public void testGoodNetSingle() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean done = new AtomicBoolean();
    final AtomicReference<ServerSocket> serverSocket = new AtomicReference<>();
    this.executor.execute(() -> {
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0, 100);
            serverSocket.set(server);
            latch.countDown();
            List<Socket> sockets = new ArrayList<>();
            int i = 0;
            while (true) {
                Socket socket = server.accept();
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                ois.readObject();
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                oos.writeObject("Reply" + (i++));
                sockets.add(socket);
            }
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    ccf.setSerializer(new DefaultSerializer());
    ccf.setDeserializer(new DefaultDeserializer());
    ccf.setSoTimeout(10000);
    ccf.setSingleUse(true);
    ccf.start();
    TcpOutboundGateway gateway = new TcpOutboundGateway();
    gateway.setConnectionFactory(ccf);
    QueueChannel replyChannel = new QueueChannel();
    gateway.setRequiresReply(true);
    gateway.setOutputChannel(replyChannel);
    // check the default remote timeout
    assertEquals("10000", TestUtils.getPropertyValue(gateway, "remoteTimeoutExpression.literalValue"));
    gateway.setSendTimeout(123);
    gateway.setRemoteTimeout(60000);
    gateway.setSendTimeout(61000);
    // ensure this did NOT change the remote timeout
    assertEquals("60000", TestUtils.getPropertyValue(gateway, "remoteTimeoutExpression.literalValue"));
    gateway.setRequestTimeout(60000);
    for (int i = 100; i < 200; i++) {
        gateway.handleMessage(MessageBuilder.withPayload("Test" + i).build());
    }
    Set<String> replies = new HashSet<String>();
    for (int i = 100; i < 200; i++) {
        Message<?> m = replyChannel.receive(10000);
        assertNotNull(m);
        replies.add((String) m.getPayload());
    }
    for (int i = 0; i < 100; i++) {
        assertTrue(replies.remove("Reply" + i));
    }
    done.set(true);
    ccf.stop();
    serverSocket.get().close();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) ArrayList(java.util.ArrayList) 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) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) EOFException(java.io.EOFException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) 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) HashSet(java.util.HashSet) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 19 with TcpNetClientConnectionFactory

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

the class TcpOutboundGatewayTests method testNetGWPropagatesSocketTimeout.

@Test
public void testNetGWPropagatesSocketTimeout() 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(100);
    ccf.setSingleUse(false);
    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) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 20 with TcpNetClientConnectionFactory

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

the class TcpOutboundGatewayTests method testCachingFailover.

@Test
public void testCachingFailover() throws Exception {
    final AtomicReference<ServerSocket> serverSocket = new AtomicReference<ServerSocket>();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean done = new AtomicBoolean();
    final CountDownLatch serverLatch = new CountDownLatch(1);
    this.executor.execute(() -> {
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
            serverSocket.set(server);
            latch.countDown();
            while (!done.get()) {
                Socket socket = server.accept();
                while (!socket.isClosed()) {
                    try {
                        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                        String request = (String) ois.readObject();
                        logger.debug("Read " + request);
                        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                        oos.writeObject("bar");
                        logger.debug("Replied to " + request);
                        serverLatch.countDown();
                    } catch (IOException e1) {
                        logger.debug("error on write " + e1.getClass().getSimpleName());
                        socket.close();
                    }
                }
            }
        } catch (Exception e2) {
            if (!done.get()) {
                e2.printStackTrace();
            }
        }
    });
    assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    // Failover
    AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
    TcpConnectionSupport mockConn1 = makeMockConnection();
    when(factory1.getConnection()).thenReturn(mockConn1);
    doThrow(new IOException("fail")).when(mockConn1).send(Mockito.any(Message.class));
    AbstractClientConnectionFactory factory2 = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    factory2.setSerializer(new DefaultSerializer());
    factory2.setDeserializer(new DefaultDeserializer());
    factory2.setSoTimeout(10000);
    factory2.setSingleUse(false);
    List<AbstractClientConnectionFactory> factories = new ArrayList<AbstractClientConnectionFactory>();
    factories.add(factory1);
    factories.add(factory2);
    FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory(factories);
    failoverFactory.start();
    // Cache
    CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(failoverFactory, 2);
    cachingFactory.start();
    TcpOutboundGateway gateway = new TcpOutboundGateway();
    gateway.setConnectionFactory(cachingFactory);
    PollableChannel outputChannel = new QueueChannel();
    gateway.setOutputChannel(outputChannel);
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.afterPropertiesSet();
    gateway.start();
    GenericMessage<String> message = new GenericMessage<String>("foo");
    gateway.handleMessage(message);
    Message<?> reply = outputChannel.receive(0);
    assertNotNull(reply);
    assertEquals("bar", reply.getPayload());
    done.set(true);
    gateway.stop();
    verify(mockConn1).send(Mockito.any(Message.class));
    factory2.stop();
    serverSocket.get().close();
}
Also used : Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) TcpConnectionSupport(org.springframework.integration.ip.tcp.connection.TcpConnectionSupport) ArrayList(java.util.ArrayList) ObjectOutputStream(java.io.ObjectOutputStream) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) DefaultSerializer(org.springframework.core.serializer.DefaultSerializer) GenericMessage(org.springframework.messaging.support.GenericMessage) DefaultDeserializer(org.springframework.core.serializer.DefaultDeserializer) BeanFactory(org.springframework.beans.factory.BeanFactory) CachingClientConnectionFactory(org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactory) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) FailoverClientConnectionFactory(org.springframework.integration.ip.tcp.connection.FailoverClientConnectionFactory) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) EOFException(java.io.EOFException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PollableChannel(org.springframework.messaging.PollableChannel) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ObjectInputStream(java.io.ObjectInputStream) 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