code: public static void shutdownObject(Object obj, Logger log) {
      if (obj instanceof Service) {
        ((Service) obj).stopAsync();
      } else if (obj instanceof Closeable) {
        try {
          ((Closeable) obj).close();
        } catch (IOException ioe) {
          log.error("Failed to close {}.", obj);
        }
      }
    }
docs: Close {@link Closeable}s and shutdown {@link Service}s.