Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

How to bake reactive behavior into your Java EE applications

459 views

Published on

You will witness that Java EE is a modern and evolving framework and you’ll learn how it supports reactive concepts by many examples and live demonstration. The latest version of Java EE together with Java 8 add even more asynchronous API to write fully reactive applications. Several Java EE implementations, like Payara Micro, go even further and provide extensions to scale your applications in a natural way.

As presented at GeeCON Prague 2016. Almost the same slides used at JDD Krakow 2016 and CZJUG Hradec Kralove.

Published in: Software
  • Be the first to comment

How to bake reactive behavior into your Java EE applications

  1. 1. How To Bake Reactive Behavior Into Your Java EE Applications  Ondrej Mihályi @omihalyi ondrej.mihalyi@payara.fish Prague, 20-21 October 2016
  2. 2. @OMihalyi Agenda ➢ Reactive Support in JavaEE7 ➢ Java 8 goodness on Reactive Implementations ➢ Payara Micro Additions ➢ Live demo ➢ Q&A
  3. 3. @OMihalyi Reactive Manifesto ResponsiveResponsive ElasticElastic ResilientResilient Message DrivenMessage Driven
  4. 4. @OMihalyi Traditional approach Request started → external resource required (DB, WS, files) → request external resource → external resource is slow WHAT DO WE DO?
  5. 5. @OMihalyi Traditional approach WE SIMPLY WAIT…
  6. 6. @OMihalyi Traditional approach WE SIMPLY WAIT… • Simple • Thread-safe • Often Sufficient (when waiting time is negligible)
  7. 7. @OMihalyi But…
  8. 8. @OMihalyi But… • … if waiting is too long  Thread resources are wasted  Processing may halt if all threads waiting
  9. 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
  10. 10. @OMihalyi Drawbacks… • Complexity  keep asking “Are you ready?” • Requires one more thread  blocked while waiting
  11. 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. 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. 13. Possible in Enterprise? • New tools and frameworks ➔ High risks and costs • Fully reactive approach ➔ High cost of development ➔ Harder to avoid and track bugs • Advice: ➔ reactive where it’s worth it ➔ leave the door open for future @OMihalyi
  14. 14. Java EE leaves the door open • Established and wide-spread − Built with resilience in mind (Transactions) − Messaging is first-class citizen (JMS) • Continuous improvements − Asynchronous API, thread-management − Scalability improvements (JCache) − Portable CDI extensions @OMihalyi
  15. 15. @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
  16. 16. @OMihalyi Async API Example • JAX-RS AsyncResponse @Suspended @GET void get(@Suspended AsyncResponse response) … response.resume("OK");
  17. 17. @OMihalyi Async API Example • JAX-RS async client resourceTarget .request(MediaType.TEXT_PLAIN) .async() .get( new InvocationCallback() …
  18. 18. @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
  19. 19. @OMihalyi Pass CF as additional parameter cf = new CompletableFuture<...>(); webtarget.async() .get(new InvocationCallback() { … cf.complete(result); … }); cf.thenAccept(result -> …)
  20. 20. @OMihalyi CompletableFuture − Chain callbacks (like promises) ● thenRun(), thenApply(), … ● thenCompose() − Complete execution in any thread ● compFuture.complete()
  21. 21. @OMihalyi Concurrency utilities ctxSvc.createContextualProxy( () -> {…wrapped code…},Runnable.class}) • Requests span multiple threads • Context must be retained • Managed threads (ExecutorService) • ContextService for non-managed
  22. 22. @OMihalyi Live Demo • Revisit famous cargo-tracker in a reactive way Available at: https://github.com/OndrejM-demonstrations/Reactive-CargoTracker
  23. 23. @OMihalyi Traditional Design
  24. 24. @OMihalyi More Reactive Design
  25. 25. @OMihalyi ● Other parts of being Reactive • We’ve shown responsive API • The other 3 reactive concepts: −Resilience −Messaging −Elasticity
  26. 26. @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
  27. 27. @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
  28. 28. @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
  29. 29. @OMihalyi Even More Reactive
  30. 30. @OMihalyi Live Demo • Enhancements to the Cargo Tracker app using Payara Micro features
  31. 31. @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
  32. 32. @OMihalyi Questions? Thank you

×