Skip to content

Instantly share code, notes, and snippets.

@pluttrell
Created March 22, 2020 22:29
Show Gist options
  • Save pluttrell/9f4d3746e5715e2a56238bcc4e1a13c6 to your computer and use it in GitHub Desktop.
Save pluttrell/9f4d3746e5715e2a56238bcc4e1a13c6 to your computer and use it in GitHub Desktop.

Revisions

  1. pluttrell created this gist Mar 22, 2020.
    18 changes: 18 additions & 0 deletions Integrated CompletableFuture
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    public static <T> Mono<T> fromFuture(Supplier<CompletableFuture<T>> completableFutureSupplier) {

    return
    Mono.subscriberContext()
    .flatMap(context ->
    Mono.fromFuture(completableFutureSupplier)
    .subscriberContext(context)
    .doFinally(signalType -> {
    var completableFuture = completableFutureSupplier.get();
    if (SignalType.CANCEL == signalType) {
    if (!completableFuture.isDone() && !completableFuture.isCancelled()) {
    completableFuture.cancel(true);
    }
    }
    })
    );

    }