Reactive applications are flexible and robust. Reactive programming is already an established concept and there are many modern frameworks and tools which support it. You need to understand the concept in order to build truly reactive applications. But you do not need to switch from what you already know to build them. With standard Java EE you have wide range of options to add reactive behavior to new and even existing applications.
I will show that Java EE is a modern and evolving framework, which supports reactive concepts. The latest versions of JDK and Java EE add even more asynchronous API to write fully reactive applications. On top of asynchronous processing in servlets, REST services and EJBs, there is asynchronous IO, managed multithreading and CompletableFuture with its promise-like API to tackle callback hell. Several Java EE implementations, like Payara Micro, go even further and provide extensions to scale your applications in natural way.
8. @OMihalyi
But…
•… if waiting is too long
Thread resources are
wasted
Processing may halt if all
threads waiting
9. @OMihalyi
Spawn a separate thread
• Idea:
- Blocking call in a new thread
- Do something while waiting
- Retrieve results
- Fail after timeout vs. block
infinitely
- java.util.concurrent.Future
11. @OMihalyi
Finish in a new thread!
•Idea:
− Blocking call in another thread
− Transfer context to another thread
− No need to wait for the
blocking call
− But new thread may still be
blocked…
12. @OMihalyi
…and asynchronous calls
• Idea:
− Provide a callback
− Call finishes immediately
− Callback called in a new thread
− Finish request in the callback
− No need to wait at all
− But, is it possible?
13. @OMihalyi
Asynchronous API in Java EE
• Async Servlet request completion
• Async JAX-RS request completion
• Async JAX-RS client
• Async IO in Servlet
• Async EJB calls
• Managed Executors
• WebSockets
14. @OMihalyi
Async API Example
• JAX-RS AsyncResponse @Suspended
@GET
void get(@Suspended
AsyncResponse response)
…
response.resume("OK");
15. @OMihalyi
Async API Example
• JAX-RS async client
resourceTarget
.request(MediaType.TEXT_PLAIN)
.async()
.get(
new InvocationCallback() …
16. @OMihalyi
Java EE + Java 8
•Future → CompletableFuture ?
− No, not compatible
•Callbacks → CompletableFuture
− callback triggers cf.complete()
•Pass CF as additional parameter
to complete it later
17. @OMihalyi
Pass CF as additional parameter
cf = new CompletableFuture<...>();
webtarget.async()
.get(new InvocationCallback() {
… cf.complete(result); …
});
cf.thenAccept(result -> {
/* Do something after */ });
20. @OMihalyi
Live Demo
• Revisit famous cargo-tracker in a reactive way
Available at:
https://github.com/OndrejM-demonstrations/Reactive-CargoTracker
21. @OMihalyi
●
Other parts of being Reactive
•We’ve shown responsive API
•The other 3 reactive concepts:
−Resilience
−Messaging
−Elasticity
22. @OMihalyi
Payara Micro
• Application server as executable JAR
• Runs WAR apps from command line
• automatic and elastic clustering
→ spawn many micro services dynamically
→ replication using distributed cache
→ shared 60MB runtime, 40MB in heap
www.payara.fish
23. @OMihalyi
•Simple messaging and caching
@Inject @Outbound
Event<MyMsg> ev;
// handle in different JVM
void handle(@Observes
@Inbound MyMsg ev) {
… }
Payara Micro Event Bus
- events triggered on all nodes
- Asynchronous micro services
- No need for service registry
- On top of CDI events
JCache API
• Standard Java API for caching
• Distributed cache
Simple messaging and caching
24. @OMihalyi
Dynamic scaling
• Just run repeatedly
• binds to a free port to avoid port collisions
• All instances autoconnect to a cluster
− Even across network (multicast)
java -jar payara-micro.jar
--deploy app.war
--autoBindHttp
26. @OMihalyi
• Fully reactive comes at a greater cost
− Dealing with threads, hard to track
origin, communication overhead
• Don’t over-engineer, but leave doors
open
• Java EE enables gradual
improvement
General advice