1

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();
    }

}
Shailesh
  • 657
  • 2
  • 13
  • 27

1 Answers1

0

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

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • ok thanks for reply...! Now I have connected to single server, similarly i want to connect another server or open another connection for another so that I can remain connected to 2 servers ? how to do it ? – Shailesh Sep 01 '16 at 13:16
  • You need a second connection factory and outbound gateway. – Gary Russell Sep 01 '16 at 13:17
  • means for each connection I need one connection factory and one outbound gateway right ? – Shailesh Sep 01 '16 at 13:28
  • For each different server/port, yes. If you have lots, you can create them programmatically instead of defining as beans. – Gary Russell Sep 01 '16 at 13:30
  • thank you very much sir. if any sample or reference would be very helpful..! – Shailesh Sep 01 '16 at 13:34
  • My answer to [this question](http://stackoverflow.com/questions/32826864/spring-multiple-imapadapter) shows one technique - it creates many IMAP email adapters. – Gary Russell Sep 01 '16 at 13:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122428/discussion-between-shailesh-and-gary-russell). – Shailesh Sep 01 '16 at 13:54