Search in sources :

Example 11 with TcpNioServerConnectionFactory

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

the class TcpSyslogReceivingChannelAdapter method onInit.

@Override
protected void onInit() {
    super.onInit();
    if (this.connectionFactory == null) {
        this.connectionFactory = new TcpNioServerConnectionFactory(getPort());
        this.connectionFactory.setDeserializer(new ByteArrayLfSerializer());
        this.connectionFactory.setBeanFactory(getBeanFactory());
        if (this.applicationEventPublisher != null) {
            this.connectionFactory.setApplicationEventPublisher(this.applicationEventPublisher);
        }
        this.connectionFactory.afterPropertiesSet();
    }
    this.connectionFactory.registerListener(this);
}
Also used : ByteArrayLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayLfSerializer) TcpNioServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory)

Example 12 with TcpNioServerConnectionFactory

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

the class TcpInboundGatewayTests method testNioNotSingle.

@Test
public void testNioNotSingle() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNioServerConnectionFactory(0);
    scf.setSingleUse(false);
    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());
    Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.getOutputStream().write("Test1\r\n".getBytes());
    socket.getOutputStream().write("Test2\r\n".getBytes());
    handler.handleMessage(channel.receive(10000));
    handler.handleMessage(channel.receive(10000));
    Set<String> results = new HashSet<String>();
    byte[] bytes = new byte[12];
    readFully(socket.getInputStream(), bytes);
    results.add(new String(bytes));
    readFully(socket.getInputStream(), bytes);
    results.add(new String(bytes));
    assertTrue(results.remove("Echo:Test1\r\n"));
    assertTrue(results.remove("Echo:Test2\r\n"));
}
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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with TcpNioServerConnectionFactory

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

the class TcpReceivingChannelAdapterTests method testNioSingleShared.

@Test
public void testNioSingleShared() throws Exception {
    TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(0);
    noopPublisher(scf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    scf.setSerializer(serializer);
    scf.setDeserializer(serializer);
    scf.setSingleUse(true);
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(scf);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(scf);
    scf.start();
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    TestingUtilities.waitListening(scf, null);
    int port = scf.getPort();
    Socket socket1 = SocketFactory.getDefault().createSocket("localhost", port);
    socket1.setSoTimeout(2000);
    socket1.getOutputStream().write("Test1\r\n".getBytes());
    Socket socket2 = SocketFactory.getDefault().createSocket("localhost", port);
    socket2.setSoTimeout(2000);
    socket2.getOutputStream().write("Test2\r\n".getBytes());
    Message<?> message = channel.receive(10000);
    assertNotNull(message);
    handler.handleMessage(message);
    message = channel.receive(10000);
    assertNotNull(message);
    handler.handleMessage(message);
    byte[] b = new byte[7];
    readFully(socket1.getInputStream(), b);
    assertEquals("Test1\r\n", new String(b));
    readFully(socket2.getInputStream(), b);
    assertEquals("Test2\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) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 14 with TcpNioServerConnectionFactory

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

the class TcpReceivingChannelAdapterTests method testNioSingleSharedInterceptors.

@Test
public void testNioSingleSharedInterceptors() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNioServerConnectionFactory(0);
    noopPublisher(scf);
    singleSharedInterceptorsGuts(scf);
    scf.stop();
}
Also used : TcpNioServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) Test(org.junit.Test)

Example 15 with TcpNioServerConnectionFactory

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

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