Search in sources :

Example 6 with AbstractClientConnectionFactory

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

the class ConnectionFacforyTests method test.

@Test
public void test() throws Exception {
    ApplicationEventPublisher publisher = e -> {
    };
    AbstractServerConnectionFactory server = Tcp.netServer(0).backlog(2).soTimeout(5000).get();
    final AtomicReference<Message<?>> received = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    server.registerListener(m -> {
        received.set(new ObjectToStringTransformer().transform(m));
        latch.countDown();
        return false;
    });
    server.setApplicationEventPublisher(publisher);
    server.afterPropertiesSet();
    server.start();
    TestingUtilities.waitListening(server, null);
    AbstractClientConnectionFactory client = Tcp.netClient("localhost", server.getPort()).get();
    client.setApplicationEventPublisher(publisher);
    client.afterPropertiesSet();
    client.start();
    client.getConnection().send(new GenericMessage<>("foo"));
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals("foo", received.get().getPayload());
    client.stop();
    server.stop();
}
Also used : TcpNioClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) TimeUnit(java.util.concurrent.TimeUnit) ObjectToStringTransformer(org.springframework.integration.transformer.ObjectToStringTransformer) CountDownLatch(java.util.concurrent.CountDownLatch) TestingUtilities(org.springframework.integration.ip.util.TestingUtilities) AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) TcpNetServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) TcpNioServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert.assertEquals(org.junit.Assert.assertEquals) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) ObjectToStringTransformer(org.springframework.integration.transformer.ObjectToStringTransformer) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 7 with AbstractClientConnectionFactory

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

the class TcpInboundGatewayTests method testNetClientMode.

@Test
public void testNetClientMode() throws Exception {
    final AtomicInteger port = new AtomicInteger();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final CountDownLatch latch3 = new CountDownLatch(1);
    final AtomicBoolean done = new AtomicBoolean();
    new SimpleAsyncTaskExecutor().execute(() -> {
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0, 10);
            port.set(server.getLocalPort());
            latch1.countDown();
            Socket socket = server.accept();
            socket.getOutputStream().write("Test1\r\nTest2\r\n".getBytes());
            byte[] bytes = new byte[12];
            readFully(socket.getInputStream(), bytes);
            assertEquals("Echo:Test1\r\n", new String(bytes));
            readFully(socket.getInputStream(), bytes);
            assertEquals("Echo:Test2\r\n", new String(bytes));
            latch2.await();
            socket.close();
            server.close();
            done.set(true);
            latch3.countDown();
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port.get());
    ccf.setSingleUse(false);
    TcpInboundGateway gateway = new TcpInboundGateway();
    gateway.setConnectionFactory(ccf);
    final QueueChannel channel = new QueueChannel();
    gateway.setRequestChannel(channel);
    gateway.setClientMode(true);
    gateway.setRetryInterval(10000);
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.afterPropertiesSet();
    ServiceActivatingHandler handler = new ServiceActivatingHandler(new Service());
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setPoolSize(1);
    taskScheduler.initialize();
    gateway.setTaskScheduler(taskScheduler);
    gateway.start();
    Message<?> message = channel.receive(10000);
    assertNotNull(message);
    handler.handleMessage(message);
    message = channel.receive(10000);
    assertNotNull(message);
    handler.handleMessage(message);
    latch2.countDown();
    assertTrue(latch3.await(10, TimeUnit.SECONDS));
    assertTrue(done.get());
    gateway.stop();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) ServerSocket(java.net.ServerSocket) CountDownLatch(java.util.concurrent.CountDownLatch) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) IOException(java.io.IOException) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeanFactory(org.springframework.beans.factory.BeanFactory) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Test(org.junit.Test)

Example 8 with AbstractClientConnectionFactory

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

the class TcpReceivingChannelAdapterTests method testNetClientMode.

@Test
public void testNetClientMode() throws Exception {
    final AtomicReference<ServerSocket> serverSocket = new AtomicReference<>();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final AtomicBoolean done = new AtomicBoolean();
    new SimpleAsyncTaskExecutor().execute(() -> {
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0, 10);
            serverSocket.set(server);
            latch1.countDown();
            Socket socket = server.accept();
            socket.getOutputStream().write("Test1\r\nTest2\r\n".getBytes());
            latch2.await();
            socket.close();
            server.close();
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    noopPublisher(ccf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    ccf.setSerializer(serializer);
    ccf.setDeserializer(serializer);
    ccf.setSoTimeout(Integer.MAX_VALUE);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(ccf);
    adapter.setClientMode(true);
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    adapter.setRetryInterval(10000);
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setPoolSize(1);
    taskScheduler.initialize();
    adapter.setTaskScheduler(taskScheduler);
    adapter.start();
    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()));
    adapter.stop();
    adapter.start();
    adapter.stop();
    latch2.countDown();
    ccf.stop();
    serverSocket.get().close();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) QueueChannel(org.springframework.integration.channel.QueueChannel) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) IOException(java.io.IOException) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BeanFactory(org.springframework.beans.factory.BeanFactory) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 9 with AbstractClientConnectionFactory

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

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

the class TcpOutboundGatewayTests method testGoodNetGWTimeoutCached.

@Test
public void testGoodNetGWTimeoutCached() throws Exception {
    ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(0);
    final int port = serverSocket.getLocalPort();
    AbstractClientConnectionFactory ccf = buildCF(port);
    CachingClientConnectionFactory cccf = new CachingClientConnectionFactory(ccf, 1);
    cccf.start();
    testGoodNetGWTimeoutGuts(port, cccf, serverSocket);
    serverSocket.close();
}
Also used : AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) CachingClientConnectionFactory(org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactory) ServerSocket(java.net.ServerSocket) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Aggregations

AbstractClientConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory)22 Test (org.junit.Test)21 ServerSocket (java.net.ServerSocket)17 TcpNetClientConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory)15 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)15 DefaultDeserializer (org.springframework.core.serializer.DefaultDeserializer)14 DefaultSerializer (org.springframework.core.serializer.DefaultSerializer)14 CountDownLatch (java.util.concurrent.CountDownLatch)9 QueueChannel (org.springframework.integration.channel.QueueChannel)8 IOException (java.io.IOException)7 Socket (java.net.Socket)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 EOFException (java.io.EOFException)5 ObjectInputStream (java.io.ObjectInputStream)5 ObjectOutputStream (java.io.ObjectOutputStream)5 SocketTimeoutException (java.net.SocketTimeoutException)5 ExecutionException (java.util.concurrent.ExecutionException)5 MessageTimeoutException (org.springframework.integration.MessageTimeoutException)5 TcpNioClientConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory)5