July 2018
Intermediate to advanced
116 pages
2h 41m
English
Finally, we can use CompletableFuture as an alternative syntactic sugar way of using those asynchronous REST APIs. Again, we use a ManagedExecutorService instance here. The next thing we want to do is use CompletableFuture to run the Fibonacci calculation asynchronously and then apply the asyncResponse::resume method. The code will look as follows. Using CompletableFuture, we call the runAsync method, run our Fibonacci calculation using the supplied executorService, and then apply the asyncResponse::resume method:
@GET @Path("/{i}") public void completable(@Suspended final AsyncResponse asyncResponse, @PathParam("i") final int i) { CompletableFuture .runAsync(() -> fibonacci(i), executorService) .thenApply(asyncResponse::resume); ...