SlideShare a Scribd company logo
How To Bake Reactive Behavior
Into Your Java EE Applications 
Ondrej Mihályi
@omihalyi ondrej.mihalyi@payara.fish
Prague, 20-21 October 2016
@OMihalyi
Agenda
➢
Reactive Support in JavaEE7
➢
Java 8 goodness on Reactive
Implementations
➢
Payara Micro Additions
➢
Live demo
➢
Q&A
@OMihalyi
Reactive Manifesto
ResponsiveResponsive
ElasticElastic ResilientResilient
Message DrivenMessage Driven
@OMihalyi
Traditional approach
Request started
→ external resource required (DB, WS, files)
→ request external resource
→ external resource is slow
WHAT DO WE DO?
@OMihalyi
Traditional approach
WE SIMPLY WAIT…
@OMihalyi
Traditional approach
WE SIMPLY WAIT…
• Simple
• Thread-safe
• Often Sufficient (when waiting
time is negligible)
@OMihalyi
But…
@OMihalyi
But…
• … if waiting is too long
 Thread resources are wasted
 Processing may halt if all
threads waiting
@OMihalyi
Spawn a separate thread
• Idea:
- Blocking call in a new thread
- Do something while waiting
- Retrieve results
- Fail after timeout vs. block infinitely
@OMihalyi
Drawbacks…
• Complexity
 keep asking “Are you ready?”
• Requires one more thread
 blocked while waiting
@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…
@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?
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
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
@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
@OMihalyi
Async API Example
• JAX-RS AsyncResponse @Suspended
@GET
void get(@Suspended
AsyncResponse response)
…
response.resume("OK");
@OMihalyi
Async API Example
• JAX-RS async client
resourceTarget
.request(MediaType.TEXT_PLAIN)
.async()
.get(
new InvocationCallback() …
@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
@OMihalyi
Pass CF as additional parameter
cf = new CompletableFuture<...>();
webtarget.async()
.get(new InvocationCallback() {
… cf.complete(result); …
});
cf.thenAccept(result -> …)
@OMihalyi
CompletableFuture
− Chain callbacks (like promises)
●
thenRun(), thenApply(), …
●
thenCompose()
− Complete execution in any thread
●
compFuture.complete()
@OMihalyi
Concurrency utilities
ctxSvc.createContextualProxy(
() -> {…wrapped code…},Runnable.class})
• Requests span multiple threads
• Context must be retained
• Managed threads (ExecutorService)
• ContextService for non-managed
@OMihalyi
Live Demo
• Revisit famous cargo-tracker in a reactive way
Available at:
https://github.com/OndrejM-demonstrations/Reactive-CargoTracker
@OMihalyi
Traditional Design
@OMihalyi
More Reactive Design
@OMihalyi
●
Other parts of being Reactive
• We’ve shown responsive API
• The other 3 reactive concepts:
−Resilience
−Messaging
−Elasticity
@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
@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
@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
@OMihalyi
Even More Reactive
@OMihalyi
Live Demo
• Enhancements to the Cargo Tracker app using Payara
Micro features
@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
@OMihalyi
Questions?
Thank you

More Related Content

What's hot

Introduction to Asynchronous scala
Introduction to Asynchronous scalaIntroduction to Asynchronous scala
Introduction to Asynchronous scalaStratio
 
WebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! FrameworkWebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! Framework
Fabio Tiriticco
 
V sphere automation_vlaxa_2017
V sphere automation_vlaxa_2017V sphere automation_vlaxa_2017
V sphere automation_vlaxa_2017
Vladan Laxa
 
Java Enterprise Edition Concurrency Misconceptions
Java Enterprise Edition Concurrency Misconceptions Java Enterprise Edition Concurrency Misconceptions
Java Enterprise Edition Concurrency Misconceptions
Haim Yadid
 
Rabbits, indians and... Symfony meets queueing brokers
Rabbits, indians and...  Symfony meets queueing brokersRabbits, indians and...  Symfony meets queueing brokers
Rabbits, indians and... Symfony meets queueing brokers
Gaetano Giunta
 
Node.js Patterns and Opinions
Node.js Patterns and OpinionsNode.js Patterns and Opinions
Node.js Patterns and Opinions
IsaacSchlueter
 
Using Websockets in Play !
Using Websockets in Play !Using Websockets in Play !
Using Websockets in Play !
Knoldus Inc.
 
Enterprise Integration Patterns with Apache Camel
Enterprise Integration Patterns with Apache CamelEnterprise Integration Patterns with Apache Camel
Enterprise Integration Patterns with Apache Camel
Ioan Eugen Stan
 
Vladimir Ulogov - Beyond the Loadable Module
Vladimir Ulogov - Beyond the Loadable ModuleVladimir Ulogov - Beyond the Loadable Module
Vladimir Ulogov - Beyond the Loadable Module
Zabbix
 
Why and how Pricing Assistant migrated from Celery to RQ - Paris.py #2
Why and how Pricing Assistant migrated from Celery to RQ - Paris.py #2Why and how Pricing Assistant migrated from Celery to RQ - Paris.py #2
Why and how Pricing Assistant migrated from Celery to RQ - Paris.py #2
Sylvain Zimmer
 
Introduction to Laravel
Introduction to LaravelIntroduction to Laravel
Introduction to Laravel
Vin Lim
 
Using Websockets with Play!
Using Websockets with Play!Using Websockets with Play!
Using Websockets with Play!
Andrew Conner
 
SSDs are Awesome
SSDs are AwesomeSSDs are Awesome
SSDs are Awesome
Barry Abrahamson
 
Shootout! Template engines for the JVM
Shootout! Template engines for the JVMShootout! Template engines for the JVM
Shootout! Template engines for the JVM
Jeroen Reijn
 
Power Cli Workshop London Vmug
Power Cli Workshop London VmugPower Cli Workshop London Vmug
Power Cli Workshop London Vmugjonathanmedd
 
Custom coded projects
Custom coded projectsCustom coded projects
Custom coded projects
Marko Heijnen
 
The Many Ways to Test Your React App
The Many Ways to Test Your React AppThe Many Ways to Test Your React App
The Many Ways to Test Your React App
All Things Open
 
ASP.NET vNext ANUG 20140817
ASP.NET vNext ANUG 20140817ASP.NET vNext ANUG 20140817
ASP.NET vNext ANUG 20140817
Christian Horsdal
 
Campus days 2014 owin
Campus days 2014 owinCampus days 2014 owin
Campus days 2014 owin
Christian Horsdal
 

What's hot (20)

Introduction to Asynchronous scala
Introduction to Asynchronous scalaIntroduction to Asynchronous scala
Introduction to Asynchronous scala
 
How to-node-core
How to-node-coreHow to-node-core
How to-node-core
 
WebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! FrameworkWebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! Framework
 
V sphere automation_vlaxa_2017
V sphere automation_vlaxa_2017V sphere automation_vlaxa_2017
V sphere automation_vlaxa_2017
 
Java Enterprise Edition Concurrency Misconceptions
Java Enterprise Edition Concurrency Misconceptions Java Enterprise Edition Concurrency Misconceptions
Java Enterprise Edition Concurrency Misconceptions
 
Rabbits, indians and... Symfony meets queueing brokers
Rabbits, indians and...  Symfony meets queueing brokersRabbits, indians and...  Symfony meets queueing brokers
Rabbits, indians and... Symfony meets queueing brokers
 
Node.js Patterns and Opinions
Node.js Patterns and OpinionsNode.js Patterns and Opinions
Node.js Patterns and Opinions
 
Using Websockets in Play !
Using Websockets in Play !Using Websockets in Play !
Using Websockets in Play !
 
Enterprise Integration Patterns with Apache Camel
Enterprise Integration Patterns with Apache CamelEnterprise Integration Patterns with Apache Camel
Enterprise Integration Patterns with Apache Camel
 
Vladimir Ulogov - Beyond the Loadable Module
Vladimir Ulogov - Beyond the Loadable ModuleVladimir Ulogov - Beyond the Loadable Module
Vladimir Ulogov - Beyond the Loadable Module
 
Why and how Pricing Assistant migrated from Celery to RQ - Paris.py #2
Why and how Pricing Assistant migrated from Celery to RQ - Paris.py #2Why and how Pricing Assistant migrated from Celery to RQ - Paris.py #2
Why and how Pricing Assistant migrated from Celery to RQ - Paris.py #2
 
Introduction to Laravel
Introduction to LaravelIntroduction to Laravel
Introduction to Laravel
 
Using Websockets with Play!
Using Websockets with Play!Using Websockets with Play!
Using Websockets with Play!
 
SSDs are Awesome
SSDs are AwesomeSSDs are Awesome
SSDs are Awesome
 
Shootout! Template engines for the JVM
Shootout! Template engines for the JVMShootout! Template engines for the JVM
Shootout! Template engines for the JVM
 
Power Cli Workshop London Vmug
Power Cli Workshop London VmugPower Cli Workshop London Vmug
Power Cli Workshop London Vmug
 
Custom coded projects
Custom coded projectsCustom coded projects
Custom coded projects
 
The Many Ways to Test Your React App
The Many Ways to Test Your React AppThe Many Ways to Test Your React App
The Many Ways to Test Your React App
 
ASP.NET vNext ANUG 20140817
ASP.NET vNext ANUG 20140817ASP.NET vNext ANUG 20140817
ASP.NET vNext ANUG 20140817
 
Campus days 2014 owin
Campus days 2014 owinCampus days 2014 owin
Campus days 2014 owin
 

Viewers also liked

Social Media BICS Froum - Cassie Delaney
Social Media BICS Froum - Cassie DelaneySocial Media BICS Froum - Cassie Delaney
Social Media BICS Froum - Cassie DelaneyBICS
 
How to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applicationsHow to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applications
Ondrej Mihályi
 
BICS Forum 2013 - FUND IT
BICS Forum 2013 - FUND ITBICS Forum 2013 - FUND IT
BICS Forum 2013 - FUND ITBICS
 
1 informe audiencia publica aguadita 2011
1 informe audiencia publica aguadita 20111 informe audiencia publica aguadita 2011
1 informe audiencia publica aguadita 2011sedeprincipalfiladelfia
 
194 943-1-pb (1)
194 943-1-pb (1)194 943-1-pb (1)
194 943-1-pb (1)ahmadwaiz
 
I donate - BICS Forum 2012
I donate - BICS Forum 2012I donate - BICS Forum 2012
I donate - BICS Forum 2012BICS
 
BICS Forum 2013 Design and Logo
BICS Forum 2013 Design and  LogoBICS Forum 2013 Design and  Logo
BICS Forum 2013 Design and LogoBICS
 
BICS Forum BICS awards presentation
BICS Forum BICS awards presentationBICS Forum BICS awards presentation
BICS Forum BICS awards presentationBICS
 
Como remover os quatro espíritos que tentam destruir a igreja
Como remover os quatro espíritos que tentam destruir a igrejaComo remover os quatro espíritos que tentam destruir a igreja
Como remover os quatro espíritos que tentam destruir a igreja
Renize Lima
 
BICS Forum – Brand workshop Jim kelly
BICS Forum – Brand workshop Jim kellyBICS Forum – Brand workshop Jim kelly
BICS Forum – Brand workshop Jim kellyBICS
 

Viewers also liked (12)

Social Media BICS Froum - Cassie Delaney
Social Media BICS Froum - Cassie DelaneySocial Media BICS Froum - Cassie Delaney
Social Media BICS Froum - Cassie Delaney
 
How to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applicationsHow to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applications
 
BICS Forum 2013 - FUND IT
BICS Forum 2013 - FUND ITBICS Forum 2013 - FUND IT
BICS Forum 2013 - FUND IT
 
1 informe audiencia publica aguadita 2011
1 informe audiencia publica aguadita 20111 informe audiencia publica aguadita 2011
1 informe audiencia publica aguadita 2011
 
194 943-1-pb (1)
194 943-1-pb (1)194 943-1-pb (1)
194 943-1-pb (1)
 
Tangram
TangramTangram
Tangram
 
I donate - BICS Forum 2012
I donate - BICS Forum 2012I donate - BICS Forum 2012
I donate - BICS Forum 2012
 
BICS Forum 2013 Design and Logo
BICS Forum 2013 Design and  LogoBICS Forum 2013 Design and  Logo
BICS Forum 2013 Design and Logo
 
1 gestion administrativa
1 gestion administrativa1 gestion administrativa
1 gestion administrativa
 
BICS Forum BICS awards presentation
BICS Forum BICS awards presentationBICS Forum BICS awards presentation
BICS Forum BICS awards presentation
 
Como remover os quatro espíritos que tentam destruir a igreja
Como remover os quatro espíritos que tentam destruir a igrejaComo remover os quatro espíritos que tentam destruir a igreja
Como remover os quatro espíritos que tentam destruir a igreja
 
BICS Forum – Brand workshop Jim kelly
BICS Forum – Brand workshop Jim kellyBICS Forum – Brand workshop Jim kelly
BICS Forum – Brand workshop Jim kelly
 

Similar to How to bake reactive behavior into your Java EE applications

JDD 2016 - Ondrej Mihalyi - How to bake reactive behavior into your Java EE ...
JDD 2016 - Ondrej Mihalyi -  How to bake reactive behavior into your Java EE ...JDD 2016 - Ondrej Mihalyi -  How to bake reactive behavior into your Java EE ...
JDD 2016 - Ondrej Mihalyi - How to bake reactive behavior into your Java EE ...
PROIDEA
 
How to bake_reactive_behavior_into_your_java_ee_applications
How to bake_reactive_behavior_into_your_java_ee_applicationsHow to bake_reactive_behavior_into_your_java_ee_applications
How to bake_reactive_behavior_into_your_java_ee_applications
Ondrej Mihályi
 
Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java
Baruch Sadogursky
 
Using JCache to speed up your apps
Using JCache to speed up your appsUsing JCache to speed up your apps
Using JCache to speed up your apps
Vassilis Bekiaris
 
IMC Summit 2016 Breakout - Greg Luck - How to Speed Up Your Application Using...
IMC Summit 2016 Breakout - Greg Luck - How to Speed Up Your Application Using...IMC Summit 2016 Breakout - Greg Luck - How to Speed Up Your Application Using...
IMC Summit 2016 Breakout - Greg Luck - How to Speed Up Your Application Using...
In-Memory Computing Summit
 
Introduction to Django-Celery and Supervisor
Introduction to Django-Celery and SupervisorIntroduction to Django-Celery and Supervisor
Introduction to Django-Celery and Supervisor
Suresh Kumar
 
Александр Пашинский "Reinventing Design Patterns with Java 8"
Александр Пашинский "Reinventing Design Patterns with Java 8"Александр Пашинский "Reinventing Design Patterns with Java 8"
Александр Пашинский "Reinventing Design Patterns with Java 8"
Anna Shymchenko
 
Workflow Engines for Hadoop
Workflow Engines for HadoopWorkflow Engines for Hadoop
Workflow Engines for Hadoop
Joe Crobak
 
What to expect from Java 9
What to expect from Java 9What to expect from Java 9
What to expect from Java 9
Ivan Krylov
 
Back to the future with Java 7 (Geekout June/2011)
Back to the future with Java 7 (Geekout June/2011)Back to the future with Java 7 (Geekout June/2011)
Back to the future with Java 7 (Geekout June/2011)
Martijn Verburg
 
Devoxx France: Fault tolerant microservices on the JVM with Cassandra
Devoxx France: Fault tolerant microservices on the JVM with CassandraDevoxx France: Fault tolerant microservices on the JVM with Cassandra
Devoxx France: Fault tolerant microservices on the JVM with Cassandra
Christopher Batey
 
JCache Using JCache
JCache Using JCacheJCache Using JCache
Play framework productivity formula
Play framework   productivity formula Play framework   productivity formula
Play framework productivity formula
Sorin Chiprian
 
Advanced java
Advanced javaAdvanced java
Advanced java
Giacomo Veneri
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and python
Chetan Giridhar
 
Reactive Programming and REST/JSON for SQL Databases - Microsoft SQL Server M...
Reactive Programming and REST/JSON for SQL Databases - Microsoft SQL Server M...Reactive Programming and REST/JSON for SQL Databases - Microsoft SQL Server M...
Reactive Programming and REST/JSON for SQL Databases - Microsoft SQL Server M...
Espresso Logic
 
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::SynchronyFast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::SynchronyKyle Drake
 
Projects Valhalla, Loom and GraalVM at JCon 2020
Projects Valhalla, Loom and GraalVM at JCon 2020Projects Valhalla, Loom and GraalVM at JCon 2020
Projects Valhalla, Loom and GraalVM at JCon 2020
Vadym Kazulkin
 
Red Dirt Ruby Conference
Red Dirt Ruby ConferenceRed Dirt Ruby Conference
Red Dirt Ruby Conference
John Woodell
 
OpenNTF Webinar 2022-08 - XPages Jakarta EE Support in Practice
OpenNTF Webinar 2022-08 - XPages Jakarta EE Support in PracticeOpenNTF Webinar 2022-08 - XPages Jakarta EE Support in Practice
OpenNTF Webinar 2022-08 - XPages Jakarta EE Support in Practice
Jesse Gallagher
 

Similar to How to bake reactive behavior into your Java EE applications (20)

JDD 2016 - Ondrej Mihalyi - How to bake reactive behavior into your Java EE ...
JDD 2016 - Ondrej Mihalyi -  How to bake reactive behavior into your Java EE ...JDD 2016 - Ondrej Mihalyi -  How to bake reactive behavior into your Java EE ...
JDD 2016 - Ondrej Mihalyi - How to bake reactive behavior into your Java EE ...
 
How to bake_reactive_behavior_into_your_java_ee_applications
How to bake_reactive_behavior_into_your_java_ee_applicationsHow to bake_reactive_behavior_into_your_java_ee_applications
How to bake_reactive_behavior_into_your_java_ee_applications
 
Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java
 
Using JCache to speed up your apps
Using JCache to speed up your appsUsing JCache to speed up your apps
Using JCache to speed up your apps
 
IMC Summit 2016 Breakout - Greg Luck - How to Speed Up Your Application Using...
IMC Summit 2016 Breakout - Greg Luck - How to Speed Up Your Application Using...IMC Summit 2016 Breakout - Greg Luck - How to Speed Up Your Application Using...
IMC Summit 2016 Breakout - Greg Luck - How to Speed Up Your Application Using...
 
Introduction to Django-Celery and Supervisor
Introduction to Django-Celery and SupervisorIntroduction to Django-Celery and Supervisor
Introduction to Django-Celery and Supervisor
 
Александр Пашинский "Reinventing Design Patterns with Java 8"
Александр Пашинский "Reinventing Design Patterns with Java 8"Александр Пашинский "Reinventing Design Patterns with Java 8"
Александр Пашинский "Reinventing Design Patterns with Java 8"
 
Workflow Engines for Hadoop
Workflow Engines for HadoopWorkflow Engines for Hadoop
Workflow Engines for Hadoop
 
What to expect from Java 9
What to expect from Java 9What to expect from Java 9
What to expect from Java 9
 
Back to the future with Java 7 (Geekout June/2011)
Back to the future with Java 7 (Geekout June/2011)Back to the future with Java 7 (Geekout June/2011)
Back to the future with Java 7 (Geekout June/2011)
 
Devoxx France: Fault tolerant microservices on the JVM with Cassandra
Devoxx France: Fault tolerant microservices on the JVM with CassandraDevoxx France: Fault tolerant microservices on the JVM with Cassandra
Devoxx France: Fault tolerant microservices on the JVM with Cassandra
 
JCache Using JCache
JCache Using JCacheJCache Using JCache
JCache Using JCache
 
Play framework productivity formula
Play framework   productivity formula Play framework   productivity formula
Play framework productivity formula
 
Advanced java
Advanced javaAdvanced java
Advanced java
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and python
 
Reactive Programming and REST/JSON for SQL Databases - Microsoft SQL Server M...
Reactive Programming and REST/JSON for SQL Databases - Microsoft SQL Server M...Reactive Programming and REST/JSON for SQL Databases - Microsoft SQL Server M...
Reactive Programming and REST/JSON for SQL Databases - Microsoft SQL Server M...
 
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::SynchronyFast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
 
Projects Valhalla, Loom and GraalVM at JCon 2020
Projects Valhalla, Loom and GraalVM at JCon 2020Projects Valhalla, Loom and GraalVM at JCon 2020
Projects Valhalla, Loom and GraalVM at JCon 2020
 
Red Dirt Ruby Conference
Red Dirt Ruby ConferenceRed Dirt Ruby Conference
Red Dirt Ruby Conference
 
OpenNTF Webinar 2022-08 - XPages Jakarta EE Support in Practice
OpenNTF Webinar 2022-08 - XPages Jakarta EE Support in PracticeOpenNTF Webinar 2022-08 - XPages Jakarta EE Support in Practice
OpenNTF Webinar 2022-08 - XPages Jakarta EE Support in Practice
 

More from Ondrej Mihályi

Easily scale enterprise applications using distributed data grids
Easily scale enterprise applications using distributed data gridsEasily scale enterprise applications using distributed data grids
Easily scale enterprise applications using distributed data grids
Ondrej Mihályi
 
Elastic and Cloud-ready Applications with Payara Micro
Elastic and Cloud-ready Applications with Payara MicroElastic and Cloud-ready Applications with Payara Micro
Elastic and Cloud-ready Applications with Payara Micro
Ondrej Mihályi
 
Bed con - MicroProfile: A Quest for a lightweight and reactive Enterprise Ja...
Bed con - MicroProfile:  A Quest for a lightweight and reactive Enterprise Ja...Bed con - MicroProfile:  A Quest for a lightweight and reactive Enterprise Ja...
Bed con - MicroProfile: A Quest for a lightweight and reactive Enterprise Ja...
Ondrej Mihályi
 
Easily scale enterprise applications using distributed data grids
Easily scale enterprise applications using distributed data gridsEasily scale enterprise applications using distributed data grids
Easily scale enterprise applications using distributed data grids
Ondrej Mihályi
 
Business layer and transactions
Business layer and transactionsBusiness layer and transactions
Business layer and transactions
Ondrej Mihályi
 
Working with jpa
Working with jpaWorking with jpa
Working with jpa
Ondrej Mihályi
 
Maven in Java EE project
Maven in Java EE projectMaven in Java EE project
Maven in Java EE project
Ondrej Mihályi
 
Java EE web project introduction
Java EE web project introductionJava EE web project introduction
Java EE web project introduction
Ondrej Mihályi
 

More from Ondrej Mihályi (8)

Easily scale enterprise applications using distributed data grids
Easily scale enterprise applications using distributed data gridsEasily scale enterprise applications using distributed data grids
Easily scale enterprise applications using distributed data grids
 
Elastic and Cloud-ready Applications with Payara Micro
Elastic and Cloud-ready Applications with Payara MicroElastic and Cloud-ready Applications with Payara Micro
Elastic and Cloud-ready Applications with Payara Micro
 
Bed con - MicroProfile: A Quest for a lightweight and reactive Enterprise Ja...
Bed con - MicroProfile:  A Quest for a lightweight and reactive Enterprise Ja...Bed con - MicroProfile:  A Quest for a lightweight and reactive Enterprise Ja...
Bed con - MicroProfile: A Quest for a lightweight and reactive Enterprise Ja...
 
Easily scale enterprise applications using distributed data grids
Easily scale enterprise applications using distributed data gridsEasily scale enterprise applications using distributed data grids
Easily scale enterprise applications using distributed data grids
 
Business layer and transactions
Business layer and transactionsBusiness layer and transactions
Business layer and transactions
 
Working with jpa
Working with jpaWorking with jpa
Working with jpa
 
Maven in Java EE project
Maven in Java EE projectMaven in Java EE project
Maven in Java EE project
 
Java EE web project introduction
Java EE web project introductionJava EE web project introduction
Java EE web project introduction
 

Recently uploaded

Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
QuickwayInfoSystems3
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
Roshan Dwivedi
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
abdulrafaychaudhry
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 

Recently uploaded (20)

Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 

How to bake reactive behavior into your Java EE applications

Editor's Notes

  1. -ondrej -mert
  2. Since application requirements changed in recent years, we went through from having tens of servers and gigabytes of data to thousands of multi processors and petabytes of data. We now want applications to be more flexible, loosely coupled from each other and scale at any factor. The idea behind the reactive manifesto is to suffice those needs, by having response, elastic, resilient, message driven applications. Reactive means system will response in a timely manner, which will increase the usability of the application. With the help of asynchronous implementation models, the user could get partial responses and the executed code parts would not block each other for the sake of the responsiveness. It also states that the application will be resilient, meaning that application will stay responsive if any problem occurs. In order to be responsiveness, meaning that to get back to the user faster at every request, application should be elastic, it should increase or decrease its resources according to the input. That would also be the cost-effective way for implementing reactive approach. For having loosely coupled components, reactive systems favor message driven approach, which will allow non-blocking communication between the components. Of course application do not inherently like this, since there is a good deal of traditional blocking API and they are being heavily used inside applications, such as JDBC. And monolithic architectures need to evolve in order to support reactive approach and that means time + money. It’s also hard to do reactive coding, you will be dealing with callbacks and threads spawning everywhere. There are frameworks of course that embodies reactive approach from ground up but they are completely new frameworks like Vert.x, as a Java EE developer you should learn everything from scratch. So in JavaEE wise, we’ll be talking about where you can introduce reactive approach into your existing code base, where it adds the most value. For that reason, we converter the famous cargo-tracker application, into reactive cargo-tracker ;)
  3. When dealing with incoming requests, the traditional approach is – one thread per request, all is sequential, waiting for slow resources like DB or external services adds to the request processing time.
  4. Just a transition slide to next slide – next slide automatically appears after 10 seconds.
  5. But waiting is not bad, if we can afford it. It is simple, as sequential is easier than parallel, and it is thread-safe. And we can afford it in many scenarios. Thus additional complexity to solve blocking calls is not required in many cases.
  6. At this point – a warning finger is raised with But…, and a small pause to watch the video. If there are too many slow blocking calls involved, often many resources (threads, CPU) are being wasted during waiting.
  7. And what if all threads are waiting? You may guess – the system is far from being responsive. The application may halt processing new requests and even crash under high load. Being responsive is one of the key aspects of the Reactive Manifesto, and if our application fails to meet that too often, it becomes unusable and therefore not suitable for production. So let’s look at how the responsiveness can be improved.
  8. How we can improve on this? Let’s introduce more threads, so that we can continue processing something else in the original thread while waiting. `Future`API supports this concept in Java. We gained more control over waiting. Now, we can decide not to wait for a result after timeout and release resources with an error response. But users don’t like error messages, do they?
  9. There are also technically drawbacks to this approach. The API may become complex, requiring checking for results iteratively, or falling back to waiting infinitely once further processing depends on the result. It is also not efficient to resources. It even consumes more threads now, just to give us more control over waiting.
  10. If we let any thread to finish the response, there’s no need to wait for threads to finish. We may simply release threads as the work is finished, and the task that finishes last will complete the request. We only need to pass the context of the request between threads. But since the blocking call is still involved, there is always at least one thread being blocked by it. We need still need to add something more to the recipe to utilize resources effectively.
  11. Ideally, we should not block any thread while waiting for a resource. We only need to handle the response from the resource in another thread. The catch is that the API to access the resource must provide a non-blocking call, which accepts a callback. So the recipe for making the application responsive and resource efficient is: - ability to transfer the request context to another thread - asynchronous API to avoid blocking And the question is – how Java EE supports these 2 concepts?
  12. Is it possible to build reactive applications in enterprise environment? And is it worth it? Although Java EE is also suitable in other areas, it already provides a stable and robust platform to build applications in the enterprise. Building new enterprise applications on top of completely new frameworks introduces a great risk. But the real problem is that majority of enterprise applications are not new, and rebuilding them from scratch using a new platform is not even an option. On the other hand, rebuilding whole application to meet reactive expectations has its drawbacks too. Reactive approach is not natural for many developers and is not straightforward in most languages. Furthermore, the asynchronous aspect of it brings new challenges to making sure that the code is correct and to track and fix bugs. Although Java EE might not be the best platform to build fully reactive applications, it provides enough means to significantly improve reactiveness of existing applications and build new reactive applications with the investment in learning a completely new framework. An important promise of Java EE is that it makes it simple and fast to build traditional enterprise applications, while leaving the door open for future enhancements and refactoring step by step to improve its reactiveness You can thus continue later with making only critical parts of your applications reactive and improve other parts later.
  13. Established and improving platform → small costs of upgrade or redesign → simplifications (EJB3, CDI, JMS 2.0 simplified API) Improvements of the application where needed → turn many synchronous API to async → split the task into multiple threads - managed executor → easily extend with CDI extensions to provide custom reactive features
  14. Java EE supports passing of a request context and finishing the response in another thread: - Servlet - JAX-RS endpoint For example, Servlet IO and JAX-RS client API provide asynchronous calls with callbacks when a response from a resource is ready. But the truth is that this is not possible with every Java EE API. In that case, you need to resort to wrapping a blocking call in a separate thread. But keep in mind that you should get it from a separate thread pool so that it does not affect other processing while being blocked. Async EJB calls provide a simple way to run tasks in separate threads. Managed executors are exactly what you need for a finer-grained control over scheduling tasks in separate threads, even in various thread pools. Finally, WebSockets are convenient to build highly responsive frontends, which are updated as soon as possible and not only after all the data is available.
  15. The JAX-RS 2.0 specification has added asynchronous HTTP support via two classes. The @Suspended annotation, and AsyncResponse interface. Having AsyncResponse instance passed as a parameter to the method states that the HTTP request/response should be detached from the currently executing thread and that the current thread should not try to automatically process the response. AsyncResponse is a callback object, meaning that calling resume method will cause a response to be sent back to the client and will also terminate the HTTP request.
  16. For the client side it’s also possible to introduce async calls, with the help of .async() method invocation and the instance of InvocationCallback that we are providing enables the way to interfere with the server response. Here InvocationCallback is a callback implementation as you would guess, which offers methods named: completed() and failed().
  17. Where Java EE provides the means, Java 8 makes it easier to write reactive code, mainly with 2 new additions - lambdas, which make it very easy to build callbacks - CompletableFuture, which makes it easy to handle asynchronous calls and chain callbacks But how to combine them effectively, as Java EE is built on top of Java 7 only? Ideally, CF or its interface counterpart CompletionStage could be used instead of Future. But that would require support in Java EE, no sooner than in Java EE 8. Therefore we need to adopt another approach. We need to create a CF ourselves and pass it to an asynchronous call, either within the callback object, or as a parameter to an asynchronous EJB method. The asynchronous call will then call either cf.complete() or cf.completeExceptionally() after the call is finished.
  18. Why is CF so much better than Future? Chain of callbacks executable in various threads, instead of just waiting for a thread to complete. It is possible to chain CF themselves using thenCompose, making it easy to chain multiple asynchronous calls. Furthermore provides a standard way to complete the future or throw and exception.
  19. We usually want to access the same resources during request processing. A request spans multiple threads and this causes some problems with the traditional Java EE. References to resources are bound to a thread and have to be transferred to subsequent threads. When new threads are provided using the managed executor service, it automatically passes the context to the new threads. If threads are created using plain Java SE, they context needs to be transferred manually, using the ContextService. In a simple scenario, we can easily wrap a lambda within a contextual proxy using appropriate functional reference. The proxy will then transfer the thread context before each method call. Remember, the contextual proxy has to be created in a managed thread. Its methods can be called in unmanaged threads. It would not work if we created the proxy inside a callback.
  20. -ondrej -mert
  21. -ondrej -mert
  22. -ondrej -mert
  23. -ondrej There’s more to being reactive than just responsiveness. We can continue to extend our analogy with the Kung-fu master to explain the other 3 concepts. - we need to be resilient to problems and any damage received from our enemies and keep fighting -&amp;gt; Resilience - when fighting together with our allies, we shout for help but cannot afford to wait for it and need to continue fighting. The help will come eventually. -&amp;gt; hence Messaging for communication - and of course Elasticity – it would be great if we could clone ourselves to fight more enemies, wouldn’t it? Java EE traditionally offers means to ensure resilience using transactions and clustering; and provide messaging, mainly by the JMS. In a bigger picture, It is usually extend by additional infrastructure to improve resilience and elasticity, e.g. by load balancing and service discovery. But this is not enough. We often need to resort to additional features of a specific application server to complete what is missing. An example of an application server providing more support for building and running reactive applications, is Payara Micro.
  24. So Payara Micro enables you to run war files from the command line without any application server installation. It is small, around 70mb and you can just execute it by using java –jar. You can deploy a web application archive through the command line and with the hazelcast integration each Payara Micro instance that will be spawned will automagically cluster with other Payara Micro processes on the network, giving you an automatic and elastic clustering configuration. If you haven’t tried our Micro before, give it a try we have a good deal of resources on payara.fish, you can even execute Micro on your Raspberry PI…!
  25. So dispatching events across JVM is simple as using CDI’s outbound event mechanism where you wrap your instance type with Event and then fire it in order to have it observed in the handle method given below. One thing to be noted here is: this model executes as the topic approach of JMS. So Event Bus of Payara Micro handles the events on any distributed node, which brings asynchronous model to the implementation. Yay! We have reactive micro services now right? There is no need for an UDDI / Service Registry definition and these all can be implemented with the help of 2 qualifiers on CDI events, @Outbound and @Inbound. Payara Micro uses Hazelcast’s distributed executor in the background to achieve this.
  26. In order to spawn a new server, just run java –jar payara-micro.jar and with – deploy parameter specify your war application and with –autoBindHttp parameter we will enable the application server to auto-bind to an HTTP port to prevent collisions. All spawned micro instances will auto-connect to a cluster, even across networks.
  27. -ondrej -mert
  28. -ondrej -mert
  29. -ondrej -mert
  30. -ondrej -mert