Skip to content
Advertisement

How to create a generic wrapper for just any method call?

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:

JavaScript

This is my code:

JavaScript

Advertisement

Answer

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:
JavaScript
  1. Or if you want to keep the signature you need to provide type:
JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement