springspring-bootspring-integrationspring-dsl

Spring Integration - Two permanents connections with RoundRobin and Failover


It's possible (using the existing components of Spring-Integration) to have two permanents connections to two differents TCP Servers, that allow us to make a round-robin between the connections, and on the case of fails, try with the other connection?

We are using the FailoverClientConnectionFactory, with two TcpNioClientConnectionFactory (each one to connect to a different server). But that means that our application it's working at 50%. Because it's using a shared connection, and we could make a round-robin to use the two TCP servers.

I'm struggling with this scenario. We are considered to make our own CustomRoundRobinFailoverConnectionFactory or use the IntegrationFlow library, but maybe it's a better way to do it.


EDIT 1 - SOLUTION

@Bean
@ServiceActivator(inputChannel="outboundChannel")
public MessageHandler outboundGatewayOne() {
    TcpOutboundGateway tcpOutboundGateway = new TcpOutboundGateway();
    tcpOutboundGateway.setConnectionFactory(failoverClientConnectionFactoryOne);
    return tcpOutboundGateway;
}

@Bean
@ServiceActivator(inputChannel="outboundChannel")
public MessageHandler outboundGatewayTwo() {
    TcpOutboundGateway tcpOutboundGateway = new TcpOutboundGateway();
    tcpOutboundGateway.setConnectionFactory(failoverClientConnectionFactoryTwo);
    return tcpOutboundGateway;
}

@Bean
public MessageChannel outboundChannel() {
    return new DirectChannel();
}

@MessagingGateway(defaultRequestChannel="outboundChannel")
public interface TcpClientGateway {
    byte[] send(byte[] message);
}

So this is working. But now, I want to avoid having the two outboundGateways hardcoded. It's a better way to do it? I tried using the same outboundGateway for the two connection factories but doesn't work.


Solution

  • One simple solution is to configure 2 outbound endpoints with 2 FailoverClientConnectionFactory instances.

    Change the order of the target factories.

    FCCF1 -> server1, server2
    FCCF2 -> server2, server1
    

    Use the same channel (DirectChannel) as the input channel to the endpoints.

    Messages sent to that channel will be round-robin distributed to the two endpoints.