@Override public void run() { exit.setInShutdownHook(); stop(); } });
/** * Launch process and waits until it's down */ public void launch(Monitored mp) { if (!lifecycle.tryToMoveTo(Lifecycle.State.STARTING)) { throw new IllegalStateException("Already started"); } monitored = mp; Logger logger = LoggerFactory.getLogger(getClass()); try { launch(logger); } catch (Exception e) { logger.warn("Fail to start {}", getKey(), e); } finally { stop(); } }
private void launch(Logger logger) throws InterruptedException { logger.info("Starting {}", getKey()); Runtime.getRuntime().addShutdownHook(shutdownHook); stopWatcher.start(); monitored.start(); Monitored.Status status = waitForNotDownStatus(); if (status == Monitored.Status.UP || status == Monitored.Status.OPERATIONAL) { // notify monitor that process is ready commands.setUp(); if (lifecycle.tryToMoveTo(Lifecycle.State.STARTED)) { Monitored.Status newStatus = waitForOperational(status); if (newStatus == Monitored.Status.OPERATIONAL && lifecycle.tryToMoveTo(Lifecycle.State.OPERATIONAL)) { commands.setOperational(); } monitored.awaitStop(); } } else { stop(); } }
@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 run() { exit.setInShutdownHook(); stop(); } });
/** * Launch process and waits until it's down */ public void launch(Monitored mp) { if (!lifecycle.tryToMoveTo(Lifecycle.State.STARTING)) { throw new IllegalStateException("Already started"); } monitored = mp; try { LoggerFactory.getLogger(getClass()).info("Starting " + getKey()); Runtime.getRuntime().addShutdownHook(shutdownHook); stopWatcher.start(); monitored.start(); boolean ready = false; while (!ready) { ready = monitored.isReady(); Thread.sleep(200L); } // notify monitor that process is ready commands.setReady(); if (lifecycle.tryToMoveTo(Lifecycle.State.STARTED)) { monitored.awaitStop(); } } catch (Exception e) { LoggerFactory.getLogger(getClass()).warn("Fail to start " + getKey(), e); } finally { stop(); } }