0

I want to create a helper method that can wrap/convert just any sync method call into an async Mono.

The following is close, but shows an error:

Required type: Mono <T>
Provided: Mono<? extends Callable<? extends T>>

This is my code:

public <T> Mono<T> wrapAsync(Callable<? extends T> supplier) {
    return Mono.fromCallable(() -> supplier)
            .subscribeOn(Schedulers.boundedElastic());
}

public void run() {
    Mono<Boolean> mono = wrapAsync(() -> syncMethod());
}

private Boolean mySyncMethod() {
    return true; //for testing only
}
membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • 2
    If `Mono.fromCallable` takes a `Callable`, then `(() -> supplier)` is the wrong argument (it's more like a `Callable>`). – ernest_k Oct 20 '20 at 10:16

1 Answers1

3

First you call Mono.fromCallable with a Callable<Callable<? extend T>>. You need to change the call like this: Mono.fromCallable(supplier).

Then you will have a problem because Mono.fromCallable will be inferred as Callable<? extend ? extend T> so your Mono will be Mono<? extend T> instead of Mono<T>. To avoid this, two solutions:

  1. Change the signature of wrapAsync:
public <T> Mono<T> wrapAsync(Callable<T> supplier) {
    return Mono.fromCallable(supplier)
            .subscribeOn(Schedulers.boundedElastic());
}
  1. Or if you want to keep the signature you need to provide type:
public <T> Mono<T> wrapAsync(Callable<? extends T> supplier) {
    return Mono.<T>fromCallable(supplier)
            .subscribeOn(Schedulers.boundedElastic());
}
JEY
  • 6,973
  • 1
  • 36
  • 51
  • Okay, yes that makes sense. My problem was I though I'd have to provide the supplier itself also as a lambda call. – membersound Oct 20 '20 at 11:35