Search in sources :

Example 1 with TcpNetClientConnectionFactory

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

the class TcpConnectionFactoryFactoryBean method createInstance.

@Override
protected AbstractConnectionFactory createInstance() throws Exception {
    if (!this.mapperSet) {
        this.mapper.setBeanFactory(this.beanFactory);
    }
    if (this.usingNio) {
        if (isServer()) {
            TcpNioServerConnectionFactory connectionFactory = new TcpNioServerConnectionFactory(this.port);
            this.setCommonAttributes(connectionFactory);
            this.setServerAttributes(connectionFactory);
            connectionFactory.setUsingDirectBuffers(this.usingDirectBuffers);
            connectionFactory.setTcpNioConnectionSupport(this.obtainNioConnectionSupport());
            this.connectionFactory = connectionFactory;
        } else {
            TcpNioClientConnectionFactory connectionFactory = new TcpNioClientConnectionFactory(this.host, this.port);
            this.setCommonAttributes(connectionFactory);
            connectionFactory.setUsingDirectBuffers(this.usingDirectBuffers);
            connectionFactory.setTcpNioConnectionSupport(this.obtainNioConnectionSupport());
            this.connectionFactory = connectionFactory;
        }
        if (this.sslHandshakeTimeout != null) {
            this.connectionFactory.setSslHandshakeTimeout(this.sslHandshakeTimeout);
        }
    } else {
        if (isServer()) {
            TcpNetServerConnectionFactory connectionFactory = new TcpNetServerConnectionFactory(this.port);
            this.setCommonAttributes(connectionFactory);
            this.setServerAttributes(connectionFactory);
            connectionFactory.setTcpSocketFactorySupport(this.obtainSocketFactorySupport());
            connectionFactory.setTcpNetConnectionSupport(this.obtainNetConnectionSupport());
            this.connectionFactory = connectionFactory;
        } else {
            TcpNetClientConnectionFactory connectionFactory = new TcpNetClientConnectionFactory(this.host, this.port);
            this.setCommonAttributes(connectionFactory);
            connectionFactory.setTcpSocketFactorySupport(this.obtainSocketFactorySupport());
            connectionFactory.setTcpNetConnectionSupport(this.obtainNetConnectionSupport());
            this.connectionFactory = connectionFactory;
        }
    }
    return this.connectionFactory;
}
Also used : TcpNioServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory) TcpNetServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) TcpNioClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory)

Example 2 with TcpNetClientConnectionFactory

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

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

the class TcpOutboundGatewayTests method testGoodNetMultiplex.

@Test
public void testGoodNetMultiplex() 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, 10);
            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());
                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();
    TcpOutboundGateway gateway = new TcpOutboundGateway();
    gateway.setConnectionFactory(ccf);
    QueueChannel replyChannel = new QueueChannel();
    gateway.setRequiresReply(true);
    gateway.setOutputChannel(replyChannel);
    for (int i = 100; i < 110; i++) {
        gateway.handleMessage(MessageBuilder.withPayload("Test" + i).build());
    }
    Set<String> replies = new HashSet<String>();
    for (int i = 100; i < 110; i++) {
        Message<?> m = replyChannel.receive(10000);
        assertNotNull(m);
        replies.add((String) m.getPayload());
    }
    for (int i = 0; i < 10; i++) {
        assertTrue(replies.remove("Reply" + i));
    }
    done.set(true);
    gateway.stop();
    ccf.stop();
    serverSocket.get().close();
}
Also used : 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) 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 4 with TcpNetClientConnectionFactory

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

the class TcpOutboundGatewayTests method testNetGWPropagatesSocketTimeoutSingleUse.

@Test
public void testNetGWPropagatesSocketTimeoutSingleUse() 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(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) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 5 with TcpNetClientConnectionFactory

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

the class TcpSendingMessageHandlerTests method testNetLength.

@Test
public void testNetLength() throws Exception {
    final AtomicReference<ServerSocket> serverSocket = new AtomicReference<>();
    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[8];
                readFully(socket.getInputStream(), b);
                if (!"\u0000\u0000\u0000\u0004Test".equals(new String(b))) {
                    throw new RuntimeException("Bad Data");
                }
                b = ("\u0000\u0000\u0000\u0006Reply" + (++i)).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);
    ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
    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) ByteArrayLengthHeaderSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer) 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) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) 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