Spring Async and Java's 8 CompletableFuture, a small change to the existing tutorial #spring #async #java8
It is known that I am not the biggest fan of Spring, but at the time being I work for an organization that maintains too many projects utilizing Spring (in different forms and versions). I still remain skeptic towards Spring, of course there are some very nice ideas, there are some nice (too many) abstractions, there are some very handy shortcuts to bootstrap complex projects. I am not going to elaborate on the things I don't like in this post.
One thing I like on Spring's documentation, is their getting started guides. Well written and concrete. I was reading through, a short guide, for Async method execution, through SpringBoot /RestApi [ link .
So this is this the implementation of the example _asynchronous' findUser() method. Full source here .
1@Asyncpublic Future findUser(String user) throws InterruptedException { System.out.println("Looking up " + user); User results = restTemplate.getForObject("https://api.github.com/users/" + user, User.class); // Artificial delay of 1s for demonstration purposes Thread.sleep(1000L); return new AsyncResult(results);}
I was wondering why there is still a Future in the example, while we have been introduced Java8, CompletableFuture. I guess the original authors want to preserve backwards compatibility with previous versions of Java (6 / 7 ) - where this construct is not available.
It seems that someone else had the same question, and wrote a very nice example here. In one of the comments, you can see a hint that from version 4.2 and onward the Spring API, would be compatible with the use of CompletableFuture, on top of Future & AsyncResult which are already provided. I thought, well it's a shame, why not try it or even document it, because if someone lands on this example, he/she might stay with the current implementation_ - why not use something standard?.
So I decided to make a tiny change, remove Future and replace it with CompletableFuture, also comment out the calls to Future.isDone() and replace it with the very handy [CompletableFuture.allof()](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html#allOf-java.util.concurrent.CompletableFuture .-) method.
So I changed the return type on the service method
while, updating the, caller code - to sync on all 3 futures and once allof() them were done, we could print the results.
The modified, example can be found ** here**. I found this and this blog posts from Tomasz Nirkewicz, a very nice and pragmatic walk through of CompletableFuture, rich method list. There is also a quite complete presentation by my favorite Devoxx Speaker , Jose Paumard you can find it here.
Links
- https://spring.io/guides/gs/async-method/
- https://geowarin.github.io/completable-futures-with-spring-async.html
- https://www.nurkiewicz.com/2013/05/java-8-completablefuture-in-action.html
- https://www.nurkiewicz.com/2013/05/java-8-definitive-guide-to.html
- https://github.com/javapapo/projects-from-blog/tree/master/spring-async-complfuture