public static <T> void completeFuture(T res, Throwable ex, CompletableFuture<T> future) {
    if (ex != null) {
      future.completeExceptionally(ex);
    }
    else {
      future.complete(res);
    }
  }
public static <T> void completeFuture(T res, Throwable ex, CompletableFuture<T> future)