Search in sources :

Example 1 with CachingClientConnectionFactory

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

the class TcpOutboundGatewayTests method testCachedGWPropagatesSocketClose.

@Test
public void testCachedGWPropagatesSocketClose() 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);
    CachingClientConnectionFactory cccf = new CachingClientConnectionFactory(ccf, 1);
    cccf.start();
    testGWPropagatesSocketCloseGuts(port, cccf, 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) CachingClientConnectionFactory(org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactory) ServerSocket(java.net.ServerSocket) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 2 with CachingClientConnectionFactory

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

the class TcpOutboundGatewayTests method testGoodNetGWTimeoutCached.

@Test
public void testGoodNetGWTimeoutCached() throws Exception {
    ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(0);
    final int port = serverSocket.getLocalPort();
    AbstractClientConnectionFactory ccf = buildCF(port);
    CachingClientConnectionFactory cccf = new CachingClientConnectionFactory(ccf, 1);
    cccf.start();
    testGoodNetGWTimeoutGuts(port, cccf, serverSocket);
    serverSocket.close();
}
Also used : AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) CachingClientConnectionFactory(org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactory) ServerSocket(java.net.ServerSocket) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 3 with CachingClientConnectionFactory

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

Example 4 with CachingClientConnectionFactory

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

the class TcpOutboundGatewayTests method testFailoverCached.

@Test
public void testFailoverCached() throws Exception {
    final AtomicReference<ServerSocket> serverSocket = new AtomicReference<>();
    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));
    // Cache
    AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
    TcpConnectionSupport mockConn1 = makeMockConnection();
    when(factory1.getConnection()).thenReturn(mockConn1);
    when(factory1.isSingleUse()).thenReturn(true);
    doThrow(new IOException("fail")).when(mockConn1).send(Mockito.any(Message.class));
    CachingClientConnectionFactory cachingFactory1 = new CachingClientConnectionFactory(factory1, 1);
    AbstractClientConnectionFactory factory2 = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    factory2.setSerializer(new DefaultSerializer());
    factory2.setDeserializer(new DefaultDeserializer());
    factory2.setSoTimeout(10000);
    factory2.setSingleUse(true);
    CachingClientConnectionFactory cachingFactory2 = new CachingClientConnectionFactory(factory2, 1);
    // Failover
    List<AbstractClientConnectionFactory> factories = new ArrayList<AbstractClientConnectionFactory>();
    factories.add(cachingFactory1);
    factories.add(cachingFactory2);
    FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory(factories);
    failoverFactory.setSingleUse(true);
    failoverFactory.afterPropertiesSet();
    failoverFactory.start();
    TcpOutboundGateway gateway = new TcpOutboundGateway();
    gateway.setConnectionFactory(failoverFactory);
    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

ServerSocket (java.net.ServerSocket)4 Test (org.junit.Test)4 AbstractClientConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory)4 CachingClientConnectionFactory (org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactory)4 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)4 DefaultDeserializer (org.springframework.core.serializer.DefaultDeserializer)3 DefaultSerializer (org.springframework.core.serializer.DefaultSerializer)3 TcpNetClientConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory)3 EOFException (java.io.EOFException)2 IOException (java.io.IOException)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Socket (java.net.Socket)2 SocketTimeoutException (java.net.SocketTimeoutException)2 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutionException (java.util.concurrent.ExecutionException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2