JavaCodingNerd

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

JavaEE

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.

@ApplicationPath("resources")
public class JAXRSConfiguration extends Application {

}

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

@Path("hello")
public class FirstResource {

  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String getFirstService() {
         
    return "HELLO";
    
  }

}

@Path("world")
public class SecondResource{

  @GET
  @Produces(MediaType.TEXT_PLAIN)
  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.

@Path("completable")
public class CompletableResource {
  
  @Resource
  private ManagedExecutorService managedExecutorService;
  
  private static final String TARGET = "localhost:/port/projectname/resources";
  

  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String getRestAsync() throws InterruptedException, ExecutionException {
            
    CompletableFuture firstResourceFuture = getCompletableFutureForRestRessource("hello");
		
    CompletableFuture secondResourceFuture =getCompletableFutureForRestRessource("world");
  
    StringBuilder builder = new StringBuilder();
    builder.append("firstResourceFuture.get());
    builder.append("\nsecondResourceFuture.get());
    
    return builder.toString();
  }
  
  
  private CompletableFuture getCompletableFutureForRestRessource(String path){
    
    Client client = ClientBuilder.newClient();
    WebTarget webTarget = client.target(TARGET).path(path);
    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

2 Comments

  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,
    Francesco

Leave a Reply

Theme by Anders Norén