private SQProcess(Builder builder) { this.processId = requireNonNull(builder.processId, "processId can't be null"); this.lifecycle = new Lifecycle(this.processId, builder.lifecycleListeners); this.eventListeners = builder.eventListeners; this.watcherDelayMs = builder.watcherDelayMs; this.stopWatcher = new StopWatcher(); this.eventWatcher = new EventWatcher(); }
public boolean start(Supplier<ProcessMonitor> commandLauncher) { if (!lifecycle.tryToMoveTo(Lifecycle.State.STARTING)) { // has already been started return false; } try { this.process = commandLauncher.get(); } catch (RuntimeException e) { LOG.error("Fail to launch process [{}]", processId.getKey(), e); lifecycle.tryToMoveTo(Lifecycle.State.STOPPED); throw e; } this.stdOutGobbler = new StreamGobbler(process.getInputStream(), processId.getKey()); this.stdOutGobbler.start(); this.stdErrGobbler = new StreamGobbler(process.getErrorStream(), processId.getKey()); this.stdErrGobbler.start(); this.stopWatcher.start(); this.eventWatcher.start(); // Could be improved by checking the status "up" in shared memory. // Not a problem so far as this state is not used by listeners. lifecycle.tryToMoveTo(Lifecycle.State.STARTED); return true; }
public boolean start(Supplier<ProcessMonitor> commandLauncher) { if (!lifecycle.tryToMoveTo(Lifecycle.State.STARTING)) { // has already been started return false; } try { this.process = commandLauncher.get(); } catch (RuntimeException e) { LOG.error("Fail to launch process [{}]", processId.getKey(), e); lifecycle.tryToMoveTo(Lifecycle.State.STOPPED); throw e; } this.stdOutGobbler = new StreamGobbler(process.getInputStream(), processId.getKey()); this.stdOutGobbler.start(); this.stdErrGobbler = new StreamGobbler(process.getErrorStream(), processId.getKey()); this.stdErrGobbler.start(); this.stopWatcher.start(); this.eventWatcher.start(); // Could be improved by checking the status "up" in shared memory. // Not a problem so far as this state is not used by listeners. lifecycle.tryToMoveTo(Lifecycle.State.STARTED); return true; }
public void stopForcibly() { eventWatcher.interrupt(); stopWatcher.interrupt(); if (process != null) { process.destroyForcibly(); waitForDown(); process.closeStreams(); } if (stdOutGobbler != null) { StreamGobbler.waitUntilFinish(stdOutGobbler); stdOutGobbler.interrupt(); } if (stdErrGobbler != null) { StreamGobbler.waitUntilFinish(stdErrGobbler); stdErrGobbler.interrupt(); } lifecycle.tryToMoveTo(Lifecycle.State.STOPPED); }
private SQProcess(Builder builder) { this.processId = requireNonNull(builder.processId, "processId can't be null"); this.lifecycle = new Lifecycle(this.processId, builder.lifecycleListeners); this.eventListeners = builder.eventListeners; this.watcherDelayMs = builder.watcherDelayMs; this.stopWatcher = new StopWatcher(); this.eventWatcher = new EventWatcher(); }
public void stopForcibly() { eventWatcher.interrupt(); stopWatcher.interrupt(); if (process != null) { process.destroyForcibly(); waitForDown(); process.closeStreams(); } if (stdOutGobbler != null) { StreamGobbler.waitUntilFinish(stdOutGobbler); stdOutGobbler.interrupt(); } if (stdErrGobbler != null) { StreamGobbler.waitUntilFinish(stdErrGobbler); stdErrGobbler.interrupt(); } lifecycle.tryToMoveTo(Lifecycle.State.STOPPED); }