code: private void addShutdownHook() {
      ServiceManager manager = this.serviceManager;
      Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
          // Give the services 5 seconds to stop to ensure that we are responsive to shutdown
          // requests.
          try {
            manager.stopAsync().awaitStopped(5, TimeUnit.SECONDS);
          } catch (TimeoutException timeout) {
            // stopping timed out
          }
        }
      });
    }
docs: Register a shutdown hook for this thread.