Search in sources :

Example 21 with AbstractServerConnectionFactory

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

the class TcpReceivingChannelAdapterTests method testNetShared.

@Test
public void testNetShared() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(0);
    noopPublisher(scf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    scf.setSerializer(serializer);
    scf.setDeserializer(serializer);
    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 socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.setSoTimeout(2000);
    socket.getOutputStream().write("Test\r\n".getBytes());
    socket.getOutputStream().write("Test\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[6];
    readFully(socket.getInputStream(), b);
    assertEquals("Test\r\n", new String(b));
    readFully(socket.getInputStream(), b);
    assertEquals("Test\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)

Example 22 with AbstractServerConnectionFactory

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

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

the class TcpReceivingChannelAdapterTests method testNetSingleNoOutboundInterceptors.

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

Example 24 with AbstractServerConnectionFactory

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

the class IpIntegrationTests method testTcpAdapters.

@Test
public void testTcpAdapters() throws Exception {
    ApplicationEventPublisher publisher = e -> {
    };
    AbstractServerConnectionFactory server = Tcp.netServer(0).backlog(2).soTimeout(5000).id("server").get();
    assertEquals("server", server.getComponentName());
    server.setApplicationEventPublisher(publisher);
    server.afterPropertiesSet();
    TcpReceivingChannelAdapter inbound = Tcp.inboundAdapter(server).get();
    QueueChannel received = new QueueChannel();
    inbound.setOutputChannel(received);
    inbound.afterPropertiesSet();
    inbound.start();
    TestingUtilities.waitListening(server, null);
    AbstractClientConnectionFactory client = Tcp.netClient("localhost", server.getPort()).id("client").get();
    assertEquals("client", client.getComponentName());
    client.setApplicationEventPublisher(publisher);
    client.afterPropertiesSet();
    TcpSendingMessageHandler handler = Tcp.outboundAdapter(client).get();
    handler.start();
    handler.handleMessage(new GenericMessage<>("foo"));
    Message<?> receivedMessage = received.receive(10000);
    assertNotNull(receivedMessage);
    assertEquals("foo", Transformers.objectToString().transform(receivedMessage).getPayload());
    client.stop();
    server.stop();
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) QueueChannel(org.springframework.integration.channel.QueueChannel) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) IntegrationFlowContext(org.springframework.integration.dsl.context.IntegrationFlowContext) Assert.assertThat(org.junit.Assert.assertThat) TcpReceivingChannelAdapter(org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter) MulticastSendingMessageHandler(org.springframework.integration.ip.udp.MulticastSendingMessageHandler) MessageBuilder(org.springframework.integration.support.MessageBuilder) TestingUtilities(org.springframework.integration.ip.util.TestingUtilities) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) SpringRunner(org.springframework.test.context.junit4.SpringRunner) TcpSendingMessageHandler(org.springframework.integration.ip.tcp.TcpSendingMessageHandler) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ApplicationListener(org.springframework.context.ApplicationListener) EnableIntegration(org.springframework.integration.config.EnableIntegration) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) MessageChannel(org.springframework.messaging.MessageChannel) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) TimeUnit(java.util.concurrent.TimeUnit) Configuration(org.springframework.context.annotation.Configuration) CountDownLatch(java.util.concurrent.CountDownLatch) UdpServerListeningEvent(org.springframework.integration.ip.udp.UdpServerListeningEvent) Matchers.equalTo(org.hamcrest.Matchers.equalTo) AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) TcpCodecs(org.springframework.integration.ip.tcp.serializer.TcpCodecs) UnicastReceivingChannelAdapter(org.springframework.integration.ip.udp.UnicastReceivingChannelAdapter) Bean(org.springframework.context.annotation.Bean) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert.assertEquals(org.junit.Assert.assertEquals) Transformers(org.springframework.integration.dsl.Transformers) AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) TcpSendingMessageHandler(org.springframework.integration.ip.tcp.TcpSendingMessageHandler) QueueChannel(org.springframework.integration.channel.QueueChannel) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) TcpReceivingChannelAdapter(org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter) Test(org.junit.Test)

Example 25 with AbstractServerConnectionFactory

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

the class TcpSendingMessageHandlerTests method testOutboundChannelAdapterWithinChain.

@Test
public void testOutboundChannelAdapterWithinChain() throws Exception {
    AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("TcpOutboundChannelAdapterWithinChainTests-context.xml", this.getClass());
    AbstractServerConnectionFactory scf = ctx.getBean(AbstractServerConnectionFactory.class);
    TestingUtilities.waitListening(scf, null);
    ctx.getBean(AbstractClientConnectionFactory.class).setPort(scf.getPort());
    ctx.getBeansOfType(ConsumerEndpointFactoryBean.class).values().forEach(c -> c.start());
    MessageChannel channelAdapterWithinChain = ctx.getBean("tcpOutboundChannelAdapterWithinChain", MessageChannel.class);
    PollableChannel inbound = ctx.getBean("inbound", PollableChannel.class);
    String testPayload = "Hello, world!";
    channelAdapterWithinChain.send(new GenericMessage<String>(testPayload));
    Message<?> m = inbound.receive(1000);
    assertNotNull(m);
    assertEquals(testPayload, new String((byte[]) m.getPayload()));
    ctx.close();
}
Also used : AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) PollableChannel(org.springframework.messaging.PollableChannel) 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