0

I am doing some Poc for Stilts stomp server. Need to prevent server jvm termination and do it through another jvm. Here is a code snippet

 public static void main(String[] args) {
    try {
        log.debug("Starting server...");
        StompServer<MockStompProvider> server = new StompServer<MockStompProvider>();
        server.setStompProvider( new MockStompProvider() );
        server.addConnector( new InsecureConnector() );
        server.start();
        while (true) {
            Thread.sleep(200000);
        }
    } catch (Exception ex) {
        log.error("Exception ", ex);
    }
}

There are two requirements.

  1. Is there any other way to prevent termination the above jvm without using while loop.
  2. I would like to stop jvm using command like java -jar server.jar -- stop for example a server like jetty. Jetty use ports and listens for signal for stop request. Is there any simple way.

One option for second one can be using a AnchorFile, create file when start jvm and monitor for file existence and using stop jvm remove that file.

vels4j
  • 11,208
  • 5
  • 38
  • 63

1 Answers1

1

You could do this with ServerSocket

static final int PORT = Integer.getInteger("port", 65432);
public static void main(String[] args) {
    if (args.length > 0) {
        new Socket("localhost", PORT);
        return;
    }

    try {
        log.debug("Starting server...");
        StompServer<MockStompProvider> server = new StompServer<MockStompProvider>();
        server.setStompProvider( new MockStompProvider() );
        server.addConnector( new InsecureConnector() );
        server.start();
        new ServerSocket(PORT).accept();

    } catch (Exception ex) {
        log.error("Exception ", ex);
    }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • @vels4j you can improve the error handling. e.g. detect that the service is already running and give a reasonable error if you stop without a running service. Note: this will stop if you connect from any machine ( you could check the source IP) – Peter Lawrey Mar 28 '16 at 12:44