Search in sources :

Example 1 with TcpReceivingChannelAdapter

use of org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter in project spring-integration-samples by spring-projects.

the class DynamicTcpClientApplication method inOne.

@Bean
public TcpReceivingChannelAdapter inOne(TcpNetServerConnectionFactory cfOne) {
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(cfOne);
    adapter.setOutputChannel(outputChannel());
    return adapter;
}
Also used : TcpReceivingChannelAdapter(org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter) Bean(org.springframework.context.annotation.Bean)

Example 2 with TcpReceivingChannelAdapter

use of org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter in project spring-integration-samples by spring-projects.

the class DynamicTcpClientApplication method inTwo.

@Bean
public TcpReceivingChannelAdapter inTwo(TcpNetServerConnectionFactory cfTwo) {
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(cfTwo);
    adapter.setOutputChannel(outputChannel());
    return adapter;
}
Also used : TcpReceivingChannelAdapter(org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter) Bean(org.springframework.context.annotation.Bean)

Example 3 with TcpReceivingChannelAdapter

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

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

the class ConnectionFactoryTests method testObtainConnectionIds.

public void testObtainConnectionIds(AbstractServerConnectionFactory serverFactory) throws Exception {
    final List<IpIntegrationEvent> events = Collections.synchronizedList(new ArrayList<IpIntegrationEvent>());
    int expectedEvents = serverFactory instanceof TcpNetServerConnectionFactory ? // Listening, + OPEN, CLOSE, EXCEPTION for each side
    7 : // Listening, + OPEN, CLOSE (but we *might* get exceptions, depending on timing).
    5;
    final CountDownLatch serverListeningLatch = new CountDownLatch(1);
    final CountDownLatch eventLatch = new CountDownLatch(expectedEvents);
    ApplicationEventPublisher publisher = new ApplicationEventPublisher() {

        @Override
        public void publishEvent(ApplicationEvent event) {
            LogFactory.getLog(this.getClass()).trace("Received: " + event);
            events.add((IpIntegrationEvent) event);
            if (event instanceof TcpConnectionServerListeningEvent) {
                serverListeningLatch.countDown();
            }
            eventLatch.countDown();
        }

        @Override
        public void publishEvent(Object event) {
        }
    };
    serverFactory.setBeanName("serverFactory");
    serverFactory.setApplicationEventPublisher(publisher);
    serverFactory = spy(serverFactory);
    final CountDownLatch serverConnectionInitLatch = new CountDownLatch(1);
    doAnswer(invocation -> {
        Object result = invocation.callRealMethod();
        serverConnectionInitLatch.countDown();
        return result;
    }).when(serverFactory).wrapConnection(any(TcpConnectionSupport.class));
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.setPoolSize(10);
    scheduler.afterPropertiesSet();
    BeanFactory bf = mock(BeanFactory.class);
    when(bf.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
    when(bf.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class)).thenReturn(scheduler);
    serverFactory.setBeanFactory(bf);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setOutputChannel(new NullChannel());
    adapter.setConnectionFactory(serverFactory);
    adapter.start();
    assertTrue("Listening event not received", serverListeningLatch.await(10, TimeUnit.SECONDS));
    assertThat(events.get(0), instanceOf(TcpConnectionServerListeningEvent.class));
    assertThat(((TcpConnectionServerListeningEvent) events.get(0)).getPort(), equalTo(serverFactory.getPort()));
    int port = serverFactory.getPort();
    TcpNetClientConnectionFactory clientFactory = new TcpNetClientConnectionFactory("localhost", port);
    clientFactory.registerListener(message -> false);
    clientFactory.setBeanName("clientFactory");
    clientFactory.setApplicationEventPublisher(publisher);
    clientFactory.start();
    TcpConnectionSupport client = clientFactory.getConnection();
    List<String> clients = clientFactory.getOpenConnectionIds();
    assertEquals(1, clients.size());
    assertTrue(clients.contains(client.getConnectionId()));
    assertTrue("Server connection failed to register", serverConnectionInitLatch.await(10, TimeUnit.SECONDS));
    List<String> servers = serverFactory.getOpenConnectionIds();
    assertEquals(1, servers.size());
    assertTrue(serverFactory.closeConnection(servers.get(0)));
    servers = serverFactory.getOpenConnectionIds();
    assertEquals(0, servers.size());
    int n = 0;
    clients = clientFactory.getOpenConnectionIds();
    while (n++ < 100 && clients.size() > 0) {
        Thread.sleep(100);
        clients = clientFactory.getOpenConnectionIds();
    }
    assertEquals(0, clients.size());
    assertTrue(eventLatch.await(10, TimeUnit.SECONDS));
    assertThat("Expected at least " + expectedEvents + " events; got: " + events.size() + " : " + events, events.size(), greaterThanOrEqualTo(expectedEvents));
    FooEvent event = new FooEvent(client, "foo");
    client.publishEvent(event);
    assertThat("Expected at least " + expectedEvents + " events; got: " + events.size() + " : " + events, events.size(), greaterThanOrEqualTo(expectedEvents + 1));
    try {
        event = new FooEvent(mock(TcpConnectionSupport.class), "foo");
        client.publishEvent(event);
        fail("Expected exception");
    } catch (IllegalArgumentException e) {
        assertTrue("Can only publish events with this as the source".equals(e.getMessage()));
    }
    SocketAddress address = serverFactory.getServerSocketAddress();
    if (address instanceof InetSocketAddress) {
        InetSocketAddress inetAddress = (InetSocketAddress) address;
        assertEquals(port, inetAddress.getPort());
    }
    serverFactory.stop();
    scheduler.shutdown();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IpIntegrationEvent(org.springframework.integration.ip.event.IpIntegrationEvent) ApplicationEvent(org.springframework.context.ApplicationEvent) TcpReceivingChannelAdapter(org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) BeanFactory(org.springframework.beans.factory.BeanFactory) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) NullChannel(org.springframework.integration.channel.NullChannel)

Example 5 with TcpReceivingChannelAdapter

use of org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter in project faf-java-server by FAForever.

the class LegacyAdapterConfig method tcpReceivingChannelAdapter.

/**
 * TCP inbound adapter that accepts connections and messages from clients.
 */
@Bean
public TcpReceivingChannelAdapter tcpReceivingChannelAdapter() {
    TcpReceivingChannelAdapter tcpReceivingChannelAdapter = new TcpReceivingChannelAdapter();
    tcpReceivingChannelAdapter.setConnectionFactory(tcpServerConnectionFactory());
    tcpReceivingChannelAdapter.setOutputChannel(legacyInbound());
    tcpReceivingChannelAdapter.setErrorChannelName(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME);
    return tcpReceivingChannelAdapter;
}
Also used : TcpReceivingChannelAdapter(org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter) Bean(org.springframework.context.annotation.Bean)

Aggregations

TcpReceivingChannelAdapter (org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter)5 Bean (org.springframework.context.annotation.Bean)4 CountDownLatch (java.util.concurrent.CountDownLatch)2 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)2 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 TimeUnit (java.util.concurrent.TimeUnit)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertThat (org.junit.Assert.assertThat)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Test (org.junit.Test)1 RunWith (org.junit.runner.RunWith)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Qualifier (org.springframework.beans.factory.annotation.Qualifier)1 ApplicationEvent (org.springframework.context.ApplicationEvent)1 ApplicationListener (org.springframework.context.ApplicationListener)1