Search in sources :

Example 1 with TcpNioServerConnectionFactory

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

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

the class TcpInboundGatewayTests method testNioSingle.

@Test
public void testNioSingle() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNioServerConnectionFactory(0);
    scf.setSingleUse(true);
    TcpInboundGateway gateway = new TcpInboundGateway();
    gateway.setConnectionFactory(scf);
    scf.start();
    TestingUtilities.waitListening(scf, 20000L);
    int port = scf.getPort();
    final QueueChannel channel = new QueueChannel();
    gateway.setRequestChannel(channel);
    gateway.setBeanFactory(mock(BeanFactory.class));
    ServiceActivatingHandler handler = new ServiceActivatingHandler(new Service());
    handler.setChannelResolver(channelName -> channel);
    Socket socket1 = SocketFactory.getDefault().createSocket("localhost", port);
    socket1.getOutputStream().write("Test1\r\n".getBytes());
    Socket socket2 = SocketFactory.getDefault().createSocket("localhost", port);
    socket2.getOutputStream().write("Test2\r\n".getBytes());
    handler.handleMessage(channel.receive(10000));
    handler.handleMessage(channel.receive(10000));
    byte[] bytes = new byte[12];
    readFully(socket1.getInputStream(), bytes);
    assertEquals("Echo:Test1\r\n", new String(bytes));
    readFully(socket2.getInputStream(), bytes);
    assertEquals("Echo:Test2\r\n", new String(bytes));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) TcpNioServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 3 with TcpNioServerConnectionFactory

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

the class TcpReceivingChannelAdapterTests method testNioSingleNoOutbound.

@Test
public void testNioSingleNoOutbound() throws Exception {
    TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(0);
    noopPublisher(scf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    scf.setSerializer(serializer);
    scf.setDeserializer(serializer);
    scf.setSingleUse(true);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(scf);
    scf.start();
    TestingUtilities.waitListening(scf, null);
    int port = scf.getPort();
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.getOutputStream().write("Test1\r\n".getBytes());
    socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.getOutputStream().write("Test2\r\n".getBytes());
    Message<?> message = channel.receive(60000);
    assertNotNull(message);
    // with single use, results may come back in a different order
    Set<String> results = new HashSet<String>();
    results.add(new String((byte[]) message.getPayload()));
    message = channel.receive(10000);
    assertNotNull(message);
    results.add(new String((byte[]) message.getPayload()));
    assertTrue(results.contains("Test1"));
    assertTrue(results.contains("Test2"));
    scf.stop();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) QueueChannel(org.springframework.integration.channel.QueueChannel) TcpNioServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with TcpNioServerConnectionFactory

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

the class TcpReceivingChannelAdapterTests method testNio.

@Test
public void testNio() throws Exception {
    TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(0);
    noopPublisher(scf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    scf.setSerializer(serializer);
    scf.setDeserializer(serializer);
    scf.setSoTimeout(5000);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(scf);
    scf.start();
    TestingUtilities.waitListening(scf, null);
    int port = scf.getPort();
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
    for (int i = 0; i < 1000; i++) {
        socket.getOutputStream().write(("Test" + i + "\r\n").getBytes());
    }
    Set<String> results = new HashSet<String>();
    for (int i = 0; i < 1000; i++) {
        Message<?> message = channel.receive(10000);
        assertNotNull(message);
        results.add(new String((byte[]) message.getPayload()));
    }
    for (int i = 0; i < 1000; i++) {
        assertTrue(results.remove("Test" + i));
    }
    scf.stop();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) QueueChannel(org.springframework.integration.channel.QueueChannel) TcpNioServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with TcpNioServerConnectionFactory

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

the class TcpReceivingChannelAdapterTests method testNioSingleSharedMany.

@Test
public void testNioSingleSharedMany() throws Exception {
    TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(0);
    noopPublisher(scf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    scf.setSerializer(serializer);
    scf.setDeserializer(serializer);
    scf.setSingleUse(true);
    scf.setBacklog(100);
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(scf);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(scf);
    Executor te = new SimpleAsyncTaskExecutor();
    scf.setTaskExecutor(te);
    scf.start();
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    TestingUtilities.waitListening(scf, null);
    int port = scf.getPort();
    List<Socket> sockets = new LinkedList<Socket>();
    for (int i = 100; i < 200; i++) {
        Socket socket1 = SocketFactory.getDefault().createSocket("localhost", port);
        socket1.setSoTimeout(2000);
        socket1.getOutputStream().write(("Test" + i + "\r\n").getBytes());
        sockets.add(socket1);
    }
    for (int i = 100; i < 200; i++) {
        Message<?> message = channel.receive(60000);
        assertNotNull(message);
        handler.handleMessage(message);
    }
    byte[] b = new byte[9];
    for (int i = 100; i < 200; i++) {
        readFully(sockets.remove(0).getInputStream(), b);
        assertEquals("Test" + i + "\r\n", new String(b));
    }
    scf.stop();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) QueueChannel(org.springframework.integration.channel.QueueChannel) TcpNioServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) LinkedList(java.util.LinkedList) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) Executor(java.util.concurrent.Executor) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Aggregations

TcpNioServerConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory)17 Test (org.junit.Test)13 QueueChannel (org.springframework.integration.channel.QueueChannel)9 Socket (java.net.Socket)8 ServerSocket (java.net.ServerSocket)7 AbstractServerConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory)7 ByteArrayCrLfSerializer (org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer)5 BeanFactory (org.springframework.beans.factory.BeanFactory)4 HashSet (java.util.HashSet)3 TcpNioClientConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory)3 Executor (java.util.concurrent.Executor)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 SimpleAsyncTaskExecutor (org.springframework.core.task.SimpleAsyncTaskExecutor)2 ServiceActivatingHandler (org.springframework.integration.handler.ServiceActivatingHandler)2 IOException (java.io.IOException)1 DatagramSocket (java.net.DatagramSocket)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Log (org.apache.commons.logging.Log)1