Beispiel #1
0
 private void stopIfAnyProcessDidNotStart() {
   if (!lifecycle.tryToMoveTo(State.STARTED)) {
     // stopping or stopped during startup, for instance :
     // 1. A is started
     // 2. B starts
     // 3. A crashes while B is starting
     // 4. if B was not monitored during Terminator execution, then it's an alive orphan
     stop();
     throw new IllegalStateException("Stopped during startup");
   }
 }
Beispiel #2
0
 private void cleanAfterTermination() {
   trace("go to STOPPED...");
   if (lifecycle.tryToMoveTo(State.STOPPED)) {
     trace("await termination of restartWatcher and hardStopWatcher...");
     // wait for restartWatcher and hardStopWatcher to cleanly stop
     awaitTermination(restartWatcher, hardStopWatcher);
     trace("restartWatcher done");
     // removing shutdown hook to avoid called stop() unnecessarily unless already in shutdownHook
     if (!systemExit.isInShutdownHook()) {
       trace("removing shutdown hook...");
       Runtime.getRuntime().removeShutdownHook(shutdownHook);
     }
     // cleanly close JavaLauncher
     closeJavaLauncher();
   }
 }
Beispiel #3
0
  private void startProcesses() {
    // do no start any child process if not in state INIT or RESTARTING (a stop could be in progress
    // too)
    if (lifecycle.tryToMoveTo(State.STARTING)) {
      resetFileSystem();

      // start watching for stop requested by other process (eg. orchestrator) if enabled and not
      // started yet
      if (watchForHardStop && hardStopWatcher == null) {
        hardStopWatcher = new HardStopWatcherThread();
        hardStopWatcher.start();
      }

      startAndMonitorProcesses();
      stopIfAnyProcessDidNotStart();
    }
  }
Beispiel #4
0
  /**
   * Starts commands and blocks current thread until all processes are in state {@link
   * State#STARTED}.
   *
   * @throws java.lang.IllegalArgumentException if commands list is empty
   * @throws java.lang.IllegalStateException if already started or if at least one process failed to
   *     start. In this case all processes are terminated. No need to execute {@link #stop()}
   */
  public void start(List<JavaCommand> commands) {
    if (commands.isEmpty()) {
      throw new IllegalArgumentException("At least one command is required");
    }

    if (lifecycle.getState() != State.INIT) {
      throw new IllegalStateException("Can not start multiple times");
    }

    // intercepts CTRL-C
    Runtime.getRuntime().addShutdownHook(shutdownHook);

    // start watching for restart requested by child process
    restartWatcher.start();

    javaCommands = commands;
    startProcesses();
  }
Beispiel #5
0
 public State getState() {
   return lifecycle.getState();
 }
Beispiel #6
0
 public void restartAsync() {
   if (lifecycle.tryToMoveTo(State.RESTARTING)) {
     restartor = new RestartorThread();
     restartor.start();
   }
 }
Beispiel #7
0
 private void stopAsync(State stoppingState) {
   assert stoppingState == State.STOPPING || stoppingState == State.HARD_STOPPING;
   if (lifecycle.tryToMoveTo(stoppingState)) {
     terminator.start();
   }
 }