javarx-javavert.x

What should I use instead of the deprecated `Vertex#rxExecuteBlocking`, starting with Vert.x 4.5?


Starting with Vert.x 4.5.0, Vertx#rxExecuteBlocking (and the signatures of executeBlocking that return a Maybe) are deprecated.

How should I go from a Vert.x Future (with Vertx#executeBlocking) to an RxJava Maybe (or an Observable)?


Solution

  • This isn't the answer I'm looking for, but is an answer nonetheless

    I imagine that there must somewhere be a function that turns a Future to a Maybe or even an Observable straight away. That's what I'm looking for!


    The option I figured out

    Here's an option, although I'm not sure it is the most appropriate one:

    Where you would get a Vert.x Future from some computation-intensive activity (such as Thread#sleep 😉) from a method defined as follows:

    private static final Vertx VERTX = Vertx.vertx();
    
    private static Future<String> compute() {
      return VERTX.executeBlocking(() -> {
        Thread.sleep(1000);
        return "Done.";
      });
    }
    

    You could get a Maybe like so:

    final Maybe<String> maybe = MaybeHelper
      .toMaybe(handler -> compute().onComplete(handler));
    

    As an additional note, I'd like to point out that the Maybe-to-Observable conversion seems to be trivially resolved using Maybe#toObservable, thus allowing the following way to obtain an Observable out of Vertx#executeBlocking:

    final Observable<String> observable = MaybeHelper
      .toMaybe(handler -> compute().onComplete(handler))
      .toObservable();
    

    Here's a small helper class that may assist you in converting a Vert.x Future to an RxJava Observable (which I think should exist in the vertx-rx-java3 package but I can't locate):

    import io.reactivex.rxjava3.core.Observable;
    import io.vertx.core.Future;
    import io.vertx.rxjava3.MaybeHelper;
    
    public class Observables {
    
      public static <T> Observable<T> from(final Future<T> future) {
        return MaybeHelper.<T>toMaybe(future::onComplete).toObservable();
      }
    
    }