Java Coding Nerd. This Blog will feature content-rich articles, tutorials and own opinions on variuos programming related topcis.


Asynchronus REST-Calls with CompletableFuture

Asynchronus REST-Calls LocalDateTimeConverter

Asynchronus REST-Calls LocalDateTimeConverter

In this Blog i will demonstrate how to use CompletableFuture to make Asynchronus REST-Calls

Implementing the REST Resources

I´m asuming that you know how to setup a JavaEE Project and are keen on deploying it to your application of choice. If you got everything set up make an entry point for the rest-api.

public class JAXRSConfiguration extends Application {


We also need to REST Resources that we can call asynchronus later on. Let´s keep it simple here

public class FirstResource {

  public String getFirstService() {
    return "HELLO";


public class SecondResource{

  public String getFirstService() {
    return "WORLD";


Calling the Resources asynchronus

Now we want to call the two GET Methods asynchronus. To do that we will define a client that consumes the resources using CompletableFuture´s supplyasync() Method.

public class CompletableResource {
  private ManagedExecutorService managedExecutorService;
  private static final String TARGET = "localhost:/port/projectname/resources";

  public String getRestAsync() throws InterruptedException, ExecutionException {
    CompletableFuture firstResourceFuture = getCompletableFutureForRestRessource("hello");
    CompletableFuture secondResourceFuture =getCompletableFutureForRestRessource("world");
    StringBuilder builder = new StringBuilder();
    return builder.toString();
  private CompletableFuture getCompletableFutureForRestRessource(String path){
    Client client = ClientBuilder.newClient();
    WebTarget webTarget =;
    Invocation invocation = webTarget.request("text/plain").buildGet();
    return CompletableFuture.supplyAsync(() -> invocation.invoke(String.class),                  managedExecutorService);

We made use of the functional interface Supplier and of CompletableFuture. Another interesting thing is the ManagedExecutorService which allows the Asynchronus REST-Calls to be run on a JavaEE Server.
You can now deploy the Project and call the resource: localhost:/port/projectname/resources/completable


If you have any question feel free to ask.


Spread the love


  1. Francesco

    Hi, your example is very useful. Can you please tell me the dependencies in order to try your example ?
    Does Client comes from Jersey?
    Thanks in advance,

Leave a Reply

Theme by Anders Norén