Invoking REST APIs Asynchronously With Quarkus
Join the DZone community and get the full member experience.Join For Free
Recently I blogged about how to develop reactive REST APIs with Quarkus. Developing the actual endpoints asynchronously is the first step. To leverage reactive capabilities maximally though, the complete code path should be asynchronous, especially long lasting operations like database accesses and REST API invocations. This article describes two options how to invoke REST APIs asynchronously with Quarkus.
Two Options to Invoke REST APIs Asynchronously
Here are the two options:
The question is when to use which option. Rather than trying to answer this myself, let me refer to Clement Escoffier, the reactive expert in the Quarkus team. Clement answered this question on StackOverflow:
- The MicroProfile Rest Client is not non-blocking. The Vert.x web client is.
- If the rest of your code uses RX Java, the Vert.x client has a neat RX Java API.
- The MicroProfile Rest Client is using an annotation-driven approach, the Vert.x client is API-driven.
You can use both options in the same application which is what I’ve done in the sample application that is included in the cloud-native-starter project. The sample application uses several microservices. The web-api service invokes the articles service which I’ll describe below.
Eclipse Vert.x Axle Web Client
The methods ‘get’ and ‘send’ invoke the HTTP request. I’ve explained in my previous article why ‘toCompletableFuture’ and ‘orTimeout’ or used.
Once the request has been completed, the HTTP status code has to be checked and converted into a Java exception. If the request has been successful, the response will be a io.vertx.core.json.JsonArray or io.vertx.core.json.JsonObject. These objects need to be converted manually into Java objects, in this example in a list of articles.
Eclipse MicroProfile REST Client
The second option is to use the MicroProfile REST Client. Check out my previous blog Invoking REST APIs from Java Microservices which explains how to invoke REST APIs synchronously.
The MicroProfile REST Client has been extended to also support asynchronous invocations. There is an OpenLiberty guide Consuming RESTful services asynchronously that describes this functionality.
Again, let’s take a look at the code. First you need to define an interface to the service you want to invoke:
In this interface an exception mapper class is defined. This class maps HTTP errors to Java exceptions (see code):
In order to invoke the REST API, a normal Java method can be called. The implementation of this method handles MicroProfile magically (see code):
Note that the deserialization of the JSON response into a list of articles is done automatically.
My two cents: If you are not an experienced Rx Java developer, I’d go with MicroProfile for asynchronous REST invocations. The MicroProfile model is nicely designed, you don’t have to convert objects manually and it’s not blocking.
All samples of this article are included in the open source project cloud-native-starter. Check it out to see the code in action.
This article is part of a series. Read the other articles of this series to learn about reactive programming:
Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.