code: public void prepareForShutdown() {
          Collection<CompletableFuture<Void>> prepareShutdownFutures = shutdownListeners.stream()
                                                                                        .map(shutdownListener -> CompletableFuture.runAsync(() -> shutdownListener.prepareForShutdown()))
                                                                                        .collect(Collectors.toList());
          try {
              CompletableFuture.allOf(prepareShutdownFutures.toArray(new CompletableFuture[prepareShutdownFutures.size()]))
                               .get(prepareForShutdownTimeoutSec, TimeUnit.SECONDS);
          } catch (Exception e) {
              logger.error("Exception occurred while preparing shutdown.", e);
          }
      }
docs: Will call {@link ShutdownListener#prepareForShutdown()} for each {@link #registerForShutdown(ShutdownListener) registered listener}
  asynchronously, and waiting a total of five seconds for all to complete or will then timeout without waiting.