Q:

How to create and hold multiple connections in Spring Integration

I have one server and number of clients, server will send response and waits for acknowledgement, additionally I want to hold that connection forever for next message and acknowledgement how should i create these connection in Spring Integration. I read about Spring integration, i couldn't find out the solution for holding the connection.
    public class ClientCall {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        ApplicationContext ctx = new AnnotationConfigApplicationContext(GatewayConfig.class);
        GatewayService gatewayService = ctx.getBean(GatewayService.class);
        //int i=0;
        Message message = new Message();        
        /*while(i<4)
        {*/
            message.setPayload("It's working");
            gatewayService.sendMessage(message);
        /*  i++;            
        }*/

    }
}

    @Service
    public interface GatewayService<T> {

    public void sendMessage(final T payload);

    public void receiveMessage(String response);

}

@EnableIntegration
@IntegrationComponentScan
@Configuration
@ComponentScan(basePackages = "com.gateway.service")
public class GatewayConfig {

    // @Value("${listen.port:6788}")
    private int port = 6785;

    @Autowired
    private GatewayService<Message> gatewayService;

    @MessagingGateway(defaultRequestChannel = "sendMessageChannel")
    public interface Gateway {
        void viaTcp(String payload);
    }

    @Bean
    public AbstractClientConnectionFactory clientCF() {
        TcpNetClientConnectionFactory clientConnectionFactory = new TcpNetClientConnectionFactory("localhost",this.port);
        clientConnectionFactory.setSingleUse(true);
        return clientConnectionFactory;
    }

    @Bean
    @ServiceActivator(inputChannel = "sendMessageChannel")
    public MessageHandler tcpOutGateway(AbstractClientConnectionFactory connectionFactory) {
        TcpOutboundGateway outGateway = new TcpOutboundGateway();
        outGateway.setConnectionFactory(connectionFactory);
        outGateway.setAsync(true);
        outGateway.setOutputChannel(receiveMessageChannel());
        outGateway.setRequiresReply(true);
        outGateway.setReplyChannel(receiveMessageChannel());
        return outGateway;
    }

    @Bean
    public MessageChannel sendMessageChannel() {
        DirectChannel channel = new DirectChannel();
        return channel;
    }

    @Bean
    public MessageChannel receiveMessageChannel() {
        DirectChannel channel = new DirectChannel();
        return channel;
    }

    @Transformer(inputChannel = "receiveMessageChannel", outputChannel = "processMessageChannel")
    public String convert(byte[] bytes) {
        return new String(bytes);
    }

    @ServiceActivator(inputChannel = "processMessageChannel")
    public void upCase(String response) {
        gatewayService.receiveMessage(response);
    }

    @Transformer(inputChannel = "errorChannel", outputChannel = "processMessageChannel")
    public void convertError(byte[] bytes) {
        String str = new String(bytes);
        System.out.println("Error: " + str);
    }

}

public class Message {

    private String payload;
  // getter setter
}

@Service
public class GatewayServiceImpl implements GatewayService<Message> {

    @Autowired
    private Gateway gateway;

    @Autowired
    private GatewayContextManger<String, Object> gatewayContextManger;

    @Override
    public void sendMessage(final Message message) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                gateway.viaTcp(message.getPayload());
            }
        }).start();
    }

    @Override
    public void receiveMessage(final String response) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                Message message = new Message();
                message.setPayload(response);
                Object obj = message;
                //Object obj = gatewayContextManger.get(message.getPayload());
                synchronized (message) {
                    obj.notify();
                    System.out.println("Message Received : "+message.getPayload());
                }
            }
        }).start();
    }

}

A:

You have: clientConnectionFactory.setSingleUse(true); This means the connection will be closed after the request; leave it false (default) to keep the connection open.

