@Override protected void doStart() { Exceptions.checkNotClosed(this.closed.get(), this); notifyStarted(); log.info("{}: Started.", this.traceObjectId); this.runTask = doRun(); this.runTask.whenComplete((r, ex) -> { // Handle any exception that may have been thrown. if (ex != null && !(Exceptions.unwrap(ex) instanceof CancellationException && state() != State.RUNNING)) { // We ignore CancellationExceptions while shutting down - those are expected. errorHandler(ex); } // Make sure the service is stopped when the runTask is done (whether successfully or not). if (state() == State.RUNNING) { stopAsync(); } }); }