Skip to Content
Building RESTful Web Services with Java EE 8
book

Building RESTful Web Services with Java EE 8

by Mario-Leander Reimer
July 2018
Intermediate to advanced
116 pages
2h 41m
English
Packt Publishing
Content preview from Building RESTful Web Services with Java EE 8

CompletableFuture

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); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

RESTful Java Web Services - Third Edition

RESTful Java Web Services - Third Edition

Balachandar Bogunuva Mohanram

Publisher Resources

ISBN: 9781789532883Supplemental Content