This document summarizes a presentation on baking reactive behavior into Java EE applications. It discusses reactive support in Java EE 7, using Java 8 features like CompletableFuture with asynchronous APIs. It demonstrates passing CompletableFuture as a parameter to complete later. The presentation also covers concurrency utilities, a live demo of a reactive cargo tracker app, Payara Micro for application servers as JARs, and dynamic scaling. General advice is given to not over-engineer for full reactivity but leave doors open to gradual improvement.
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