Search in sources :

Example 1 with TcpInboundGateway

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

the class FailoverClientConnectionFactoryTests method testFailoverCachedWithGateway.

@SuppressWarnings("unchecked")
@Test
public void testFailoverCachedWithGateway() throws Exception {
    final TcpNetServerConnectionFactory server = new TcpNetServerConnectionFactory(0);
    server.setBeanName("server");
    server.afterPropertiesSet();
    DirectChannel inChannel = new DirectChannel();
    inChannel.setBeanName("inChannel");
    TcpInboundGateway inbound = new TcpInboundGateway();
    inbound.setConnectionFactory(server);
    inbound.setRequestChannel(inChannel);
    inbound.afterPropertiesSet();
    inChannel.subscribe(new BridgeHandler());
    inbound.start();
    TestingUtilities.waitListening(server, 10000L);
    int port = server.getPort();
    AbstractClientConnectionFactory client = new TcpNetClientConnectionFactory("localhost", port);
    client.setBeanName("client");
    // Cache
    CachingClientConnectionFactory cachingClient = new CachingClientConnectionFactory(client, 2);
    cachingClient.setBeanName("cache");
    cachingClient.afterPropertiesSet();
    // Failover
    List<AbstractClientConnectionFactory> clientFactories = new ArrayList<AbstractClientConnectionFactory>();
    clientFactories.add(cachingClient);
    FailoverClientConnectionFactory failoverClient = new FailoverClientConnectionFactory(clientFactories);
    failoverClient.setSingleUse(true);
    failoverClient.afterPropertiesSet();
    TcpOutboundGateway outbound = new TcpOutboundGateway();
    outbound.setConnectionFactory(failoverClient);
    QueueChannel replyChannel = new QueueChannel();
    replyChannel.setBeanName("replyChannel");
    outbound.setReplyChannel(replyChannel);
    outbound.setBeanFactory(mock(BeanFactory.class));
    outbound.afterPropertiesSet();
    outbound.start();
    outbound.handleMessage(new GenericMessage<String>("foo"));
    Message<byte[]> result = (Message<byte[]>) replyChannel.receive(10000);
    assertNotNull(result);
    assertEquals("foo", new String(result.getPayload()));
    // INT-4024 - second reply had bad connection id
    outbound.handleMessage(new GenericMessage<String>("foo"));
    result = (Message<byte[]>) replyChannel.receive(10000);
    assertNotNull(result);
    assertEquals("foo", new String(result.getPayload()));
    inbound.stop();
    outbound.stop();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectChannel(org.springframework.integration.channel.DirectChannel) BridgeHandler(org.springframework.integration.handler.BridgeHandler) ArrayList(java.util.ArrayList) TcpOutboundGateway(org.springframework.integration.ip.tcp.TcpOutboundGateway) TcpInboundGateway(org.springframework.integration.ip.tcp.TcpInboundGateway) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 2 with TcpInboundGateway

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

the class Application method tcpGate.

@Bean
TcpInboundGateway tcpGate() {
    TcpInboundGateway gateway = new TcpInboundGateway();
    gateway.setConnectionFactory(cf());
    gateway.setRequestChannel(requestChannel());
    return gateway;
}
Also used : TcpInboundGateway(org.springframework.integration.ip.tcp.TcpInboundGateway) Bean(org.springframework.context.annotation.Bean)

Example 3 with TcpInboundGateway

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

the class Application method tcpGate.

@Bean
TcpInboundGateway tcpGate() {
    TcpInboundGateway gateway = new TcpInboundGateway();
    gateway.setConnectionFactory(cf());
    gateway.setRequestChannel(requestChannel());
    return gateway;
}
Also used : TcpInboundGateway(org.springframework.integration.ip.tcp.TcpInboundGateway) Bean(org.springframework.context.annotation.Bean)

Example 4 with TcpInboundGateway

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

the class ConnectionEventTests method testInboundGatewayNoConnectionEvents.

@Test
public void testInboundGatewayNoConnectionEvents() {
    TcpInboundGateway gw = new TcpInboundGateway();
    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);
        }
    });
    gw.setConnectionFactory(scf);
    DirectChannel requestChannel = new DirectChannel();
    requestChannel.subscribe(message -> ((MessageChannel) message.getHeaders().getReplyChannel()).send(message));
    gw.setRequestChannel(requestChannel);
    gw.start();
    Message<String> message = MessageBuilder.withPayload("foo").setHeader(IpHeaders.CONNECTION_ID, "bar").build();
    gw.onMessage(message);
    assertNotNull(theEvent.get());
    TcpConnectionFailedCorrelationEvent event = (TcpConnectionFailedCorrelationEvent) theEvent.get();
    assertEquals("bar", event.getConnectionId());
    assertSame(message, ((MessagingException) event.getCause()).getFailedMessage());
    gw.stop();
    scf.stop();
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) TcpInboundGateway(org.springframework.integration.ip.tcp.TcpInboundGateway) 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) Test(org.junit.Test)

Example 5 with TcpInboundGateway

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

the class FailoverClientConnectionFactoryTests method setupAndStartServers.

private Holder setupAndStartServers(AbstractServerConnectionFactory server1, AbstractServerConnectionFactory server2) {
    Executor exec = new SimpleAsyncTaskExecutor();
    server1.setTaskExecutor(exec);
    server2.setTaskExecutor(exec);
    server1.setBeanName("server1");
    server2.setBeanName("server2");
    server1.setApplicationEventPublisher(NULL_PUBLISHER);
    server2.setApplicationEventPublisher(NULL_PUBLISHER);
    TcpInboundGateway gateway1 = new TcpInboundGateway();
    gateway1.setConnectionFactory(server1);
    SubscribableChannel channel = new DirectChannel();
    final AtomicReference<String> connectionId = new AtomicReference<String>();
    channel.subscribe(message -> {
        connectionId.set((String) message.getHeaders().get(IpHeaders.CONNECTION_ID));
        ((MessageChannel) message.getHeaders().getReplyChannel()).send(message);
    });
    gateway1.setRequestChannel(channel);
    gateway1.setBeanFactory(mock(BeanFactory.class));
    gateway1.afterPropertiesSet();
    gateway1.start();
    TcpInboundGateway gateway2 = new TcpInboundGateway();
    gateway2.setConnectionFactory(server2);
    gateway2.setRequestChannel(channel);
    gateway2.setBeanFactory(mock(BeanFactory.class));
    gateway2.afterPropertiesSet();
    gateway2.start();
    TestingUtilities.waitListening(server1, null);
    TestingUtilities.waitListening(server2, null);
    Holder holder = new Holder();
    holder.exec = exec;
    holder.connectionId = connectionId;
    holder.server1 = server1;
    holder.gateway2 = gateway2;
    return holder;
}
Also used : SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) Executor(java.util.concurrent.Executor) MessageChannel(org.springframework.messaging.MessageChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) TcpInboundGateway(org.springframework.integration.ip.tcp.TcpInboundGateway) BeanFactory(org.springframework.beans.factory.BeanFactory) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) AtomicReference(java.util.concurrent.atomic.AtomicReference) SubscribableChannel(org.springframework.messaging.SubscribableChannel)

Aggregations

TcpInboundGateway (org.springframework.integration.ip.tcp.TcpInboundGateway)6 BeanFactory (org.springframework.beans.factory.BeanFactory)3 DirectChannel (org.springframework.integration.channel.DirectChannel)3 Executor (java.util.concurrent.Executor)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Test (org.junit.Test)2 Bean (org.springframework.context.annotation.Bean)2 SimpleAsyncTaskExecutor (org.springframework.core.task.SimpleAsyncTaskExecutor)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 TcpOutboundGateway (org.springframework.integration.ip.tcp.TcpOutboundGateway)2 MessageChannel (org.springframework.messaging.MessageChannel)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 ApplicationEvent (org.springframework.context.ApplicationEvent)1 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)1 BridgeHandler (org.springframework.integration.handler.BridgeHandler)1 TcpNioClientConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory)1 TcpNioServerConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory)1