Search in sources :

Example 1 with TcpSendingMessageHandler

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

the class LegacyAdapterConfig method tcpSendingMessageHandler.

/**
 * Message handler which sends messages to a connected client.
 */
@Bean
public TcpSendingMessageHandler tcpSendingMessageHandler() {
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(tcpServerConnectionFactory());
    handler.setStatsEnabled(true);
    return handler;
}
Also used : TcpSendingMessageHandler(org.springframework.integration.ip.tcp.TcpSendingMessageHandler) Bean(org.springframework.context.annotation.Bean)

Example 2 with TcpSendingMessageHandler

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

the class ConnectionEventTests method testOutboundChannelAdapterNoConnectionEvents.

@Test
public void testOutboundChannelAdapterNoConnectionEvents() {
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    AbstractServerConnectionFactory scf = new AbstractServerConnectionFactory(0) {

        @Override
        public void run() {
        }
    };
    final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<ApplicationEvent>();
    scf.setApplicationEventPublisher(new ApplicationEventPublisher() {

        @Override
        public void publishEvent(Object event) {
        }

        @Override
        public void publishEvent(ApplicationEvent event) {
            theEvent.set(event);
        }
    });
    handler.setConnectionFactory(scf);
    handler.start();
    Message<String> message = MessageBuilder.withPayload("foo").setHeader(IpHeaders.CONNECTION_ID, "bar").build();
    try {
        handler.handleMessage(message);
        fail("expected exception");
    } catch (MessageHandlingException e) {
        assertThat(e.getMessage(), Matchers.containsString("Unable to find outbound socket"));
    }
    assertNotNull(theEvent.get());
    TcpConnectionFailedCorrelationEvent event = (TcpConnectionFailedCorrelationEvent) theEvent.get();
    assertEquals("bar", event.getConnectionId());
    assertSame(message, ((MessagingException) event.getCause()).getFailedMessage());
}
Also used : TcpSendingMessageHandler(org.springframework.integration.ip.tcp.TcpSendingMessageHandler) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) ApplicationEvent(org.springframework.context.ApplicationEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Test(org.junit.Test)

Example 3 with TcpSendingMessageHandler

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

the class CachingClientConnectionFactoryTests method testGatewayRelease.

@SuppressWarnings("unchecked")
// INT-3722
@Test
public void testGatewayRelease() throws Exception {
    TcpNetServerConnectionFactory in = new TcpNetServerConnectionFactory(0);
    in.setApplicationEventPublisher(mock(ApplicationEventPublisher.class));
    final TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(in);
    final AtomicInteger count = new AtomicInteger(2);
    in.registerListener(message -> {
        if (!(message instanceof ErrorMessage)) {
            if (count.decrementAndGet() < 1) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
            handler.handleMessage(message);
        }
        return false;
    });
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    handler.start();
    TestingUtilities.waitListening(in, null);
    int port = in.getPort();
    TcpNetClientConnectionFactory out = new TcpNetClientConnectionFactory("localhost", port);
    out.setApplicationEventPublisher(mock(ApplicationEventPublisher.class));
    CachingClientConnectionFactory cache = new CachingClientConnectionFactory(out, 2);
    final TcpOutboundGateway gate = new TcpOutboundGateway();
    gate.setConnectionFactory(cache);
    QueueChannel outputChannel = new QueueChannel();
    gate.setOutputChannel(outputChannel);
    gate.setBeanFactory(mock(BeanFactory.class));
    gate.afterPropertiesSet();
    Log logger = spy(TestUtils.getPropertyValue(gate, "logger", Log.class));
    new DirectFieldAccessor(gate).setPropertyValue("logger", logger);
    when(logger.isDebugEnabled()).thenReturn(true);
    doAnswer(new Answer<Void>() {

        private final CountDownLatch latch = new CountDownLatch(2);

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            invocation.callRealMethod();
            String log = invocation.getArgument(0);
            if (log.startsWith("Response")) {
                new SimpleAsyncTaskExecutor().execute(() -> gate.handleMessage(new GenericMessage<>("bar")));
                // hold up the first thread until the second has added its pending reply
                latch.await(10, TimeUnit.SECONDS);
            } else if (log.startsWith("Added")) {
                latch.countDown();
            }
            return null;
        }
    }).when(logger).debug(anyString());
    gate.start();
    gate.handleMessage(new GenericMessage<String>("foo"));
    Message<byte[]> result = (Message<byte[]>) outputChannel.receive(10000);
    assertNotNull(result);
    assertEquals("foo", new String(result.getPayload()));
    result = (Message<byte[]>) outputChannel.receive(10000);
    assertNotNull(result);
    assertEquals("bar", new String(result.getPayload()));
    handler.stop();
    gate.stop();
    verify(logger, never()).error(anyString());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) BeanFactory(org.springframework.beans.factory.BeanFactory) TcpSendingMessageHandler(org.springframework.integration.ip.tcp.TcpSendingMessageHandler) Log(org.apache.commons.logging.Log) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) TcpOutboundGateway(org.springframework.integration.ip.tcp.TcpOutboundGateway) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Test(org.junit.Test)

Example 4 with TcpSendingMessageHandler

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

Aggregations

TcpSendingMessageHandler (org.springframework.integration.ip.tcp.TcpSendingMessageHandler)4 Test (org.junit.Test)3 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Bean (org.springframework.context.annotation.Bean)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 Message (org.springframework.messaging.Message)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Log (org.apache.commons.logging.Log)1 Matchers.containsString (org.hamcrest.Matchers.containsString)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