3

I am very new to Java and Mutiny.

As indicated below, my test function asks Redis for the value of key "foo" which is "bar". That is working and the Future onCompleted() gets "bar".

So far so good.

I have two issues with the Uni.createFrom().future() bit.

  1. The compiler says: The method future(Future<? extends T>) in the type UniCreate is not applicable for the arguments (Future<Response>). I have tried the suggested fixes but ended up in a deeper hole. My Java skills are insufficient to fully grasp the meaning of the error.

  2. How do I get "bar" into the Uni<String> returned from test()? I have tried all sorts of subscribing and CompletableFutures and cannot make anything work. I figure I need to return a function to generate the Uni but am at a loss about how to do that.

// The abbreviated setup
import io.vertx.redis.client.Redis;
private final Redis redisClient;
this.redisClient = Redis.createClient(vertx);


public Uni<String> test () {
  // Ask Redis for the value of key "foo" => "bar"
  Future<Response> futureResponse = this.redisClient.send(Request.cmd(Command.create("JSON.GET")).arg("foo"))
      .compose(response -> {
        // response == 'bar'
        return Future.succeededFuture(response);
      }).onComplete(res -> {
        // res == 'bar'
      });

  // How to make the return of the Uni<String> wait for the completed futureResponse 
  // so it makes a Uni<String> from "bar" and returns it from the method?
  Uni<String> respUni = Uni.createFrom().future(futureResponse);

  return respUni;
}

Thanks. Any suggestions gratefully accepted! (And yes, I have spent many hours trying to work it out for myself) ;-)

Murrah
  • 1,508
  • 1
  • 13
  • 26
  • I would recommend using the Mutiny Redis client API directly. So, you won't have to deal with the futures and the completion stages. – Clement Sep 07 '22 at 12:05
  • Thanks @Clement. Yes, that was on my mind too. I wanted to get the Vertx version working first a way to learn more. I did try the Mutiny version and had some issues getting it to work - again probably due to my lack of experience. I was thinking of asking you how to convert the working sample below to a Mutiny version. Thanks for your comment, Murray. – Murrah Sep 07 '22 at 20:42
  • PS: My use-case is the Redis JSON and SEARCH commands, which is why I am not using the Quarkus version. – Murrah Sep 07 '22 at 21:07
  • I've a good news for you. First, check https://quarkus.io/blog/redis-api-intro/ and you will see how the new API is structured. The low-level client (providing a Mutiny API) already supports JSON and SEARCH commands. After some internal debate, we will provide high-level methods for these modules, too (as part of the Redis data source interface). I can't give you an ETA unfortunately (help welcome :-D) – Clement Sep 09 '22 at 06:15
  • That looks terrific, Clement! I will dig in and learn. Much appreciated. Murray – Murrah Sep 09 '22 at 07:42

1 Answers1

3

Updated the post, because of errors.

UniCreate.future() takes a java.util.concurrent.Future of some type and returns Uni of the same type. That is, you'll have to pass a java.util.concurrent.Future<String> to get a Uni<String>.

The send method of the Redis client returns a io.vertx.core.Future<Response> which is not assignment compatible to java.util.concurrent.Future.

Fortunately, the API provides io.vertx.core.Future#toCompletionStage to convert a vertx Future to a JDK CompletionStage while Mutiny provides UniCreate.completionStage() to get the job done.

public Uni<String> test () {
  Future<String> futureResponse = this.redisClient.send(Request.cmd(Command.create("JSON.GET")).arg("foo"))
      .compose(response -> {
        return Future.succeededFuture(response.toString());
      });

  Uni<String> respUni = Uni.createFrom().completionStage(futureResponse.toCompletionStage());

  return respUni;
}
Mihe
  • 2,270
  • 2
  • 4
  • 14
  • thanks! I have made the changes as per your suggestion and still get the error `The method future(Future extends T>) in the type UniCreate is not applicable for the arguments (Future)` on `.future(`. If I accept the suggested fix it changes it to `Uni.createFrom().future((java.util.concurrent.Future extends T>) futureResponse);` then throws another error: `cannot convert from Uni to Uni`. Am I supposed to add some types in there? I am using `io.vertx.core.Future` for Future. – Murrah Sep 07 '22 at 09:18
  • Sorry, I suspect that is a basic question but a week in to Java is not enough learning yet! – Murrah Sep 07 '22 at 09:21
  • Sorry, I was misleaded by the type names and updated my answer. – Mihe Sep 07 '22 at 10:44
  • Brilliant! Thank you so much. In all my reading I didn't come across the `toCompletionStage()` function. Cool! Much appreciated. :-) – Murrah Sep 07 '22 at 11:01
  • Honestly, it took me a while to figure that out because I don't use neither vertx nor Mutiny :-) – Mihe Sep 07 '22 at 11:04
  • 1
    Well, I really appreciate your efforts, thanks again. If it took you a while I probably wouldn't have cracked it with just 1 week of Java under my belt! – Murrah Sep 07 '22 at 11:07
  • @murrah - 20 years under this belt, and I had the same questions. You're doing good. – Nino Walker Feb 09 '23 at 16:46
  • @NinoWalker: thanks! Getting there. Loving Quarkus and Mutiny. – Murrah Feb 09 '23 at 20:54