Search in sources :

Example 6 with TcpInboundGateway

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

TcpInboundGateway (org.springframework.integration.ip.tcp.TcpInboundGateway)6 BeanFactory (org.springframework.beans.factory.BeanFactory)3 DirectChannel (org.springframework.integration.channel.DirectChannel)3 Executor (java.util.concurrent.Executor)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Test (org.junit.Test)2 Bean (org.springframework.context.annotation.Bean)2 SimpleAsyncTaskExecutor (org.springframework.core.task.SimpleAsyncTaskExecutor)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 TcpOutboundGateway (org.springframework.integration.ip.tcp.TcpOutboundGateway)2 MessageChannel (org.springframework.messaging.MessageChannel)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 ApplicationEvent (org.springframework.context.ApplicationEvent)1 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)1 BridgeHandler (org.springframework.integration.handler.BridgeHandler)1 TcpNioClientConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory)1 TcpNioServerConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory)1