@Override public Status getStatus() { if (ready) { return Status.OPERATIONAL; } if (server.isStarted()) { ready = true; writeTimeToFile("readyAt"); } return ready ? Status.OPERATIONAL : Status.DOWN; }
public static void main(String[] args) { ProcessEntryPoint entryPoint = ProcessEntryPoint.createForArguments(args); entryPoint.launch(new InfiniteTerminationProcess()); } }
public static void main(String[] args) { ProcessEntryPoint entryPoint = ProcessEntryPoint.createForArguments(args); entryPoint.launch(new StandardProcess()); System.exit(0); } }
@Test public void terminate_if_unexpected_shutdown() throws Exception { Props props = createProps(); final ProcessEntryPoint entryPoint = new ProcessEntryPoint(props, exit, commands); final StandardProcess process = new StandardProcess(); Thread runner = new Thread() { @Override public void run() { // starts and waits until terminated entryPoint.launch(process); } }; runner.start(); while (process.getState() != State.STARTED) { Thread.sleep(10L); } // emulate signal to shutdown process entryPoint.getShutdownHook().start(); // hack to prevent JUnit JVM to fail when executing the shutdown hook a second time Runtime.getRuntime().removeShutdownHook(entryPoint.getShutdownHook()); while (process.getState() != State.STOPPED) { Thread.sleep(10L); } // exit before test timeout, ok ! }
public static void main(String[] args) { ProcessEntryPoint entryPoint = ProcessEntryPoint.createForArguments(args); entryPoint.launch(new HttpProcess(entryPoint.getProps().valueAsInt("httpPort"), entryPoint.getCommands())); } }
@Test public void launch_then_request_graceful_stop() throws Exception { Props props = createProps(); final ProcessEntryPoint entryPoint = new ProcessEntryPoint(props, exit, commands); final StandardProcess process = new StandardProcess(); Thread runner = new Thread() { @Override public void run() { // starts and waits until terminated entryPoint.launch(process); } }; runner.start(); while (process.getState() != State.STARTED) { Thread.sleep(10L); } // requests for graceful stop -> waits until down // Should terminate before the timeout of 30s entryPoint.stop(); assertThat(process.getState()).isEqualTo(State.STOPPED); }
@Override public void stop() { try { if (!server.isStopped()) { server.stop(); writeTimeToFile("terminatedAt"); } } catch (Exception e) { throw new IllegalStateException("Fail to stop Jetty", e); } }
@Override public void start() { writeTimeToFile("startingAt"); ContextHandler context = new ContextHandler(); context.setContextPath("/"); context.setClassLoader(Thread.currentThread().getContextClassLoader()); server.setHandler(context); context.setHandler(new AbstractHandler() { @Override public void handle(String target, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { if ("/ping".equals(target)) { request.setHandled(true); httpServletResponse.getWriter().print("ping"); } else if ("/restart".equals(target)) { writeTimeToFile("restartAskedAt"); request.setHandled(true); processCommands.askForRestart(); httpServletResponse.getWriter().print("ok"); } else if ("/kill".equals(target)) { writeTimeToFile("killedAt"); System.exit(0); } } }); try { server.start(); } catch (Exception e) { throw new IllegalStateException("Fail to start Jetty", e); } }