Search in sources :

Example 1 with AbstractServerConnectionFactory

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

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

the class TcpInboundGatewayTests method testErrorFlow.

@Test
public void testErrorFlow() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(0);
    scf.setSingleUse(true);
    TcpInboundGateway gateway = new TcpInboundGateway();
    gateway.setConnectionFactory(scf);
    SubscribableChannel errorChannel = new DirectChannel();
    final String errorMessage = "An error occurred";
    errorChannel.subscribe(message -> {
        MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
        replyChannel.send(new GenericMessage<String>(errorMessage));
    });
    gateway.setErrorChannel(errorChannel);
    scf.start();
    TestingUtilities.waitListening(scf, 20000L);
    int port = scf.getPort();
    final SubscribableChannel channel = new DirectChannel();
    gateway.setRequestChannel(channel);
    gateway.setBeanFactory(mock(BeanFactory.class));
    ServiceActivatingHandler handler = new ServiceActivatingHandler(new FailingService());
    channel.subscribe(handler);
    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());
    byte[] bytes = new byte[errorMessage.length() + 2];
    readFully(socket1.getInputStream(), bytes);
    assertEquals(errorMessage + "\r\n", new String(bytes));
    readFully(socket2.getInputStream(), bytes);
    assertEquals(errorMessage + "\r\n", new String(bytes));
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) MessageChannel(org.springframework.messaging.MessageChannel) TcpNetServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 3 with AbstractServerConnectionFactory

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

the class TcpReceivingChannelAdapterTests method testNet.

@Test
public void testNet() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(0);
    noopPublisher(scf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    scf.setSerializer(serializer);
    scf.setDeserializer(serializer);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(scf);
    scf.start();
    TestingUtilities.waitListening(scf, null);
    int port = scf.getPort();
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.getOutputStream().write("Test1\r\n".getBytes());
    socket.getOutputStream().write("Test2\r\n".getBytes());
    Message<?> message = channel.receive(10000);
    assertNotNull(message);
    assertEquals("Test1", new String((byte[]) message.getPayload()));
    message = channel.receive(10000);
    assertNotNull(message);
    assertEquals("Test2", new String((byte[]) message.getPayload()));
    scf.stop();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) QueueChannel(org.springframework.integration.channel.QueueChannel) TcpNetServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 4 with AbstractServerConnectionFactory

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

the class TcpReceivingChannelAdapterTests method testNetSingleNoOutbound.

@Test
public void testNetSingleNoOutbound() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(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(10000);
    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) TcpNetServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with AbstractServerConnectionFactory

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

the class TcpReceivingChannelAdapterTests method testNetSingleShared.

@Test
public void testNetSingleShared() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(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) TcpNetServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Aggregations

AbstractServerConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory)26 Test (org.junit.Test)25 TcpNetServerConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory)14 Socket (java.net.Socket)13 QueueChannel (org.springframework.integration.channel.QueueChannel)12 ServerSocket (java.net.ServerSocket)10 BeanFactory (org.springframework.beans.factory.BeanFactory)8 TcpNioServerConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory)8 ServiceActivatingHandler (org.springframework.integration.handler.ServiceActivatingHandler)6 AbstractClientConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)4 ByteArrayCrLfSerializer (org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer)4 DatagramSocket (java.net.DatagramSocket)3 MessageChannel (org.springframework.messaging.MessageChannel)3 PollableChannel (org.springframework.messaging.PollableChannel)3 HashSet (java.util.HashSet)2 TimeUnit (java.util.concurrent.TimeUnit)2 Log (org.apache.commons.logging.Log)2 Assert.assertEquals (org.junit.Assert.assertEquals)2