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.

1 Answer 1

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); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

@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)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.