Open Liberty Docs (2024)

Implementing asynchronous REST clients can be a powerful strategy for microservices-based applications. Asynchronous clients don’t need to wait for a response to continue working. Therefore, microservices in an app can continue to process and send data, even when one of their partner services runs into trouble. This capability provides more reliable service to the user and can be especially valuable in cases where service availability is low or overloaded with demand.

For example, asynchronous clients might be implemented within a travel service app that makes REST calls to an airline service, hotel service, and car rental service. If the hotel service experiences a lag, the airline and car rental service are able to continue working. For an in-depth look at how to implement asynchronous clients in this travel app example, check out Andy McCright’s blog post on Asynchronous REST with JAX-RS and MicroProfile.

Asynchronous REST clients with MicroProfile Rest Client

Callbacks are functions that run after another function completes. With MicroProfile Rest Client, asynchronous clients rely on callbacks to manage the transfer of data between microservices. The return type of the interface method determines whether a RESTful service is invoked synchronously or asynchronously. If the return type is a CompletionStage, the service is invoked asynchronously. A CompletionStage return type acts as a promise to the service that a particular piece of code must be executed at some point in the future. A wide range of callbacks can be attached to a CompletionStage return type, which can enable different functions after the code executes. Therefore, non-blocking systems can be implemented among microservices in an application.

MicroProfile Rest Client is enabled to set up both synchronous and asynchronous REST clients. You can use MicroProfile Rest Client with Open Liberty by enabling the mpRestClient feature in your server.xml file.

The following example shows a MicroProfile Rest Client interface that has methods to invoke the same remote service both synchronously and asynchronously:

@Path("/somePath")public interface MyClient { @GET Widget getSync(); @GET CompletionStage<Widget> getAsync();}

The following example shows the code to invoke that remote service:

MyClient client = RestClientBuilder.newBuilder().baseUri(someUri).build(MyClient.class);CompletionStage<Widget> cs = client.getAsync();// while that request is running, let's invoke it synchronously too:Widget widgetSync = client.getSync(); // now we have to wait until the request completes// done. So now we can see the results of the async request// that was processing while we did the sync request:Widget widgetAsync = toCompletableFuture().get();assertEquals(widgetAsync, widgetSync);

Asynchronous REST clients with JAX-RS

With JAX-RS clients, you need to explicitly build an async client with an API call, as shown in the following example:

Client client = ClientBuilder.newClient();WebTarget target = client.target(url);Invoker.Builder builder = target.request();AsyncInvoker asyncInvoker = builder.async();


Then, you can choose whether to provide a callback or get a Future return type, as shown in the following example.

Future<MyResponseObject> future = asyncInvoker.get(MyResponseObject.class);// orFuture<MyResponseObject> future = asyncInvoker.get( new InvocationCallback<MyResponseObject>() { @Override public void completed(MyResponseObject o) { // do something with o } @Override public void failed(Throwable t) { // handle the failed request/response }});
Open Liberty Docs (2024)
Top Articles
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 5819

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.