Q:

How to make the TCP Server run till it is stopped

Sample https://github.com/spring-projects/spring-integration-samples/tree/master/basic/tcp-client-server is great to build a TCP Server application. Its simple and runs on JVM. It does not need any Application Server.
Sample uses command line input to run the program. I want the server to accept data only from the Socket port and not through the command line. If I remove the command line input, main thread is finishing and the program no longer accepts input from the port. I have to keep the main thread running all the time.
I am thinking some thing like this:
    boolean isGatewayStopped = false;
    while (!isGatewayStopped) {
        try {
            Thread.sleep(5000);
            isGatewayStopped = getGatewayStatus();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

I have two questions:

is there a clean way of making the Main thread continue to run?
How to know that Gateway is stopped? If the user sends TCP data as "quit" then gateway can be stopped. is there any way to know that gateway is stopped?

Thanks

A:

Another solution is like this:
public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
        System.out.println("Hit 'Enter' to terminate");
        System.in.read();
        ctx.close();
}

You start the ApplicationContext and wait for the stop from the console input.
EDIT
For the case when you would like to stop program via an event in the application, you can register ApplicationListener and wait on the barrier before existing from the main:
CountDownLatch exitLatch = new CountDownLatch(1);
ctx.addApplicationListener(ContextClosedEvent e -> exitLatch.countDown())
exitLatch.await();

Now you should just come up with some logic in your application to call ctx.stop() there.

