Calling a vert.x async method from a sync method
Made popular by NodeJS and ES6 asynchronous programming promises (pun intended) better throughput and resource usage by entertaining an Event Loop. In Java land vert.x implements exactly this approach and has proven its mettle, being the foundation of Quarkus
Your legacy app doesn't magically convert
When you start a new vert.x project using the App Generator, everything is asynchronous from the beginning. Snippets of synchronous code (a.k.a blocking code), you might need to maintain, can be wrapped into executeBlocking and handled in their own thread.
However when you are about to convert a synchronous application, e.g. a servlet to asynchronous and you can't finish in a sprint/session, things become interesting. The doGet
method is synchronous by nature. There are a few steps that need to be accomplished:
- Have vert.x running in its own thread. You can't start it on the main thread with its blocking operations
- Have a method that returns a vert.x Future
- Convert that into a Java CompletableFuture
Let's have a look at the moving parts:
Read more
Posted by Stephan H Wissel on 03 August 2022 | Comments (0) | categories: Java vert.x