SlideShare a Scribd company logo
Others Talk,
We Listen.
Reactive Java EE -
Let Me Count the
Ways!
Reza Rahman
Senior Architect
rrahman@captechconsulting.com
@reza_rahman
CapTech
Full-service US national IT consulting firm that focuses on client best interests,
trust, servant leadership, culture, professionalism and technical excellence.
#28 in Vault's Consulting Top 50
#3 Best Consulting Internship
#9 Best Overall Internship
#1 in Meeting Client’s Needs
#7 Best Firm to Work For
#1 in Career Development
Ranked for the
7th
Consecutive Year
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
Agenda
• What Exactly is Reactive?
• Touring Reactive in Java EE
• Bearable Reactive with Java SE 8?
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
What’s in a Name?
• “Reactive” fairly old but incredibly vague term
• A big hurdle to broad adoption by average developers
• Sound core principals co-opted by marketing concerns?
• Event/message driven
• Asynchronous
• Non-blocking
• Overloaded concerns to simple core principals attempted to be added on
more recently
• Responsive, resilient/fault-tolerant, elastic/adaptive, scalable, etc
• These are important concerns not that unique to Reactive techniques
• Long met by Java EE at the runtime level
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
What’s the Big Deal?
• Reactive has always been an important software engineering technique
• More responsive user experience
• High throughput, optimal hardware/CPU/IO utilization
• Loose coupling, complex event processing
• Will potentially become more important
• Internet of Things (IoT), device-to-device communication
• Mobile, large global concurrent user bases, more chatty applications
• Not necessarily a panacea
• Asynchronous, event driven code is always harder to write, maintain than
synchronous, blocking code
• Horizontal/hardware scalability can be a cheaper/more maintainable
answer
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
Reactive Java EE
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
JMSJMS EJB 3EJB 3
Message-Driven
Beans
Message-Driven
Beans
Asynchronous
Session Beans
Asynchronous
Session Beans
CDICDI
EventsEvents
ObserversObservers
ServletServlet
AsynchronousAsynchronous
NIONIO
JAX-RSJAX-RS
Async on ServerAsync on Server
Async on ClientAsync on Client
WebSocketWebSocket
Async Remote
Endpoints
Async Remote
Endpoints
Concurrency
Utilities
Concurrency
Utilities
JMS and Message Driven Beans
• JMS one of the oldest APIs in Java EE, strongly aligned with Reactive
techniques
• Message/event driven, asynchronous
• Loosely coupled, reliable, transactional, durable, fault tolerant, error
tolerant, clustered
• Message Driven Beans primary vehicle for JMS message handling
• Just POJOs with annotations
• Transactional, thread-safe, throttled, reliable, load-balanced, fault-
tolerant, error-tolerant
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
JMS Send
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
@Inject JMSContext jmsContext;
@Resource(lookup = "jms/HandlingEventRegistrationAttemptQueue")
Destination handlingEventQueue;
...
public void receivedHandlingEventRegistrationAttempt(
HandlingEventRegistrationAttempt attempt) {
...
jmsContext.createProducer()
.setDeliveryMode(DeliveryMode.PERSISTENT) // The default :-)
.setPriority(LOW_PRIORITY)
.setDisableMessageID(true)
.setDisableMessageTimestamp(true)
.setStringProperty("source", source)
.send(handlingEventQueue, attempt);
}
Message Driven Bean
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType",
propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destinationLookup",
propertyValue = "jms/HandlingEventRegistrationAttemptQueue"),
@ActivationConfigProperty(propertyName = "messageSelector",
propertyValue = "source = 'mobile'")})
public class HandlingEventRegistrationAttemptConsumer
implements MessageListener {
...
public void onMessage(Message message) {
...
HandlingEventRegistrationAttempt attempt
= message.getBody(HandlingEventRegistrationAttempt.class);
...
}
}
Great Possibilities for JMS 2.1
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
@ApplicationScoped
@MaxConcurrency(10)
public class HandlingEventRegistrationAttemptConsumer {
@JmsListener(
destinationLookup="jms/HandlingEventRegistrationAttemptQueue",
selector="source = 'mobile'",
batchSize=10, retry=5, retryDelay=7000,
orderBy=TIMESTAMP)
public void onEventRegistrationAttempt(
HandlingEventRegistrationAttempt... attempts) {
...
}
}
Asynchronous Session Beans
• Dead simple asynchrony at the component level
• Just an annotation on a POJO
• Great when all that is required is greater throughput or responsiveness
• Still transactional, thread-safe, throttled
• Not loosely coupled, persistent, fault tolerant or error tolerant (client must
explicitly handle errors)
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
Asynchronous Session Bean
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
@Asynchronous
public void processPayment(Payment payment) {
// CPU/IO heavy tasks to process a payment
}
@Asynchronous
public Future<Report> generateReport(ReportParameters params) {
try {
Report report = renderReport(params);
return new AsyncResult(report);
} catch(ReportGenerationException e) {
return new AsyncResult(new ErrorReport(e));
}
Asynchronous Session Bean Client
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
@Inject ReportGeneratorService reportGeneratorService;
...
Future<Report> future =
reportGeneratorService.generateReport(parameters);
...
if (future.isDone()) {
Report report = future.get();
...
}
...
future.cancel(true);
@Asynchronous + CompletableFuture
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
@Asynchronous
public CompletableFuture<Confirmation> processPayment(Order order) {
...
Confirmation status = ...;
return
CompletableFuture<Confirmation>.completedFuture(status);
}
paymentService
.processPayment(order)
.thenAccept(
confirmation -> System.out.println(confirmation));
CDI Events/Observers
• Compact, simple, elegant, type-safe events
• Essentially the observer pattern formalized via a DI framework and
annotations
• Offers excellent solution to loose-coupling, type-safe filtering/chaining and
asynchrony (but not much else)
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
CDI Events
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
@Inject @CargoInspected Event<Cargo> cargoInspected;
...
public void inspectCargo(TrackingId trackingId) {
...
cargoInspected.fire(cargo);
}
public void onCargoInspected(
@Observes @CargoInspected Cargo cargo) {
@Qualifier
@Retention(RUNTIME) @Target({FIELD, PARAMETER})
public @interface CargoInspected {}
Asynchronous CDI Events
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
@Inject @CargoInspected Event<Cargo> cargoInspected;
...
public void inspectCargo(TrackingId trackingId) {
...
cargoInspected.fireAsync(cargo);
}
public void onCargoInspected(
@Observes(async=true) @CargoInspected Cargo cargo) {
Asynchronous Servlets and NIO
• Asynchronous Servlets maximize throughput/thread utilization
• Decouple connection from request thread
• Return request thread back to pool
• Handle IO/CPU heavy work on separate backend thread
• Close cached connection when done
• NIO removes possible thread blocks during slow read/write
• Get notified when the IO channel might be ready
• Only read/write when IO channel is ready
• Obvious need when Servlet IO is particularly heavy, otherwise a complex
solution
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
Asynchronous Servlet
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
@WebServlet(urlPatterns={"/report"}, asyncSupported=true)
public class AsyncServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
...
final AsyncContext asyncContext = request.startAsync();
asyncContext.start(() -> {
ReportParameters parameters =
parseReportParameters(asyncContext.getRequest());
Report report = generateReport(parameters);
printReport(report, asyncContext);
asyncContext.complete();
});
}
}
Asynchronous Servlet NIO (Output Stream)
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
private void printReport(Report report, AsyncContext context) {
ServletOutputStream output =
context.getResponse().getOutputStream();
WriteListener writeListener = new ReportWriteListener(
output, report, context);
output.setWriteListener(writeListener);
}
Asynchronous Servlet NIO (Write Listener)
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
class ReportWriteListener implements WriteListener {
private ServletOutputStream output = null;
private InputStream input = null;
private AsyncContext context = null;
ReportWriteListener(ServletOutputStream output, Report report,
AsyncContext context) {
this.output = output;
this.input = report.asPdfStream();
this.context = context;
}
...
Asynchronous Servlet NIO (Write Listener)
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
...
public void onWritePossible() throws IOException {
byte[] chunk = new byte[256];
int read = 0;
while (output.isReady() && (read = input.read(chunk)) != -1)
output.write(chunk, 0, read);
if (read == -1)
context.complete();
}
public void onError(Throwable t) {
context.complete();
t.printStackTrace();
}
}
Asynchronous JAX-RS
• Asynchronous capabilities newly added to JAX-RS 2/Java EE 7
• Both on the server and client side
• Server-side essentially identical to Servlet 3 async
• Nicer declarative syntax
• Client API async capabilities very symmetric to synchronous API
• Both Futures and callbacks supported
• JAX-RS server-side NIO promised for Java EE 8
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
Asynchronous JAX-RS Resource
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
@Stateless
@Path("/reports")
public class ReportsResource {
...
@Path("{id}")
@GET
@Produces({"application/pdf"})
@Asynchronous
public void generateReport(
@PathParam("id") Long id,
@Suspended AsyncResponse response) {
ResponseBuilder builder = Response.ok(renderReport(id));
builder.header("Content-Disposition",
"attachment; filename=report.pdf");
response.resume(builder.build());
}
}
Asynchronous JAX-RS Client
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
WebTarget target = client.target("http://.../balance")...
Future<Double> future = target.request()
.async().get(Double.class));
...
Double balance = future.get();
WebTarget target = client.target("http://.../balance")...
target.request().async().get(
new InvocationCallback<Double>() {
public void complete(Double balance) {
// Process balance
}
public void failed(InvocationException e) {
// Process error
}
});
Asynchrony/NIO in WebSocket
• WebSocket endpoints are inherently asynchronous/event-driven
• No thread-connection association in the first place
• True for server and client side
• Writes/sends can be made asynchronous for better throughput
• Very symmetric API for both sync and async
• Futures or callbacks supported
• Good idea to use asynchronous send in most cases
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
Asynchronous Remote WebSocket Endpoint
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
@Singleton @ServerEndpoint(value = "/chat"...)
public class ChatServer {
...
@OnOpen
public void onOpen(Session peer) {
peers.add(peer);
}
@OnClose
public void onClose(Session peer) {
peers.remove(peer);
}
@OnMessage
public void onMessage(ChatMessage message) {
for (Session peer : peers) {
...peer.getAsyncRemote().sendObject(message)...
}
}
}
Java EE Concurrency Utilities
• Allows for lower-level threading/asynchronous capabilities in Java EE in a
safe, reliable, managed fashion
• Very specialized code, custom workloads
• Fairly small extension of Java SE Concurrency Utilities
• ManagedExecutorService
• ManagedThreadFactory
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
Managed Executor Service
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
@Path("/reports")
public class ReportsResource {
@Resource ManagedExecutorService executor;
...
@Path("{id}")
@GET
@Produces({"application/pdf"})
public void generateReport(
@PathParam("id") Long id,
@Suspended AsyncResponse response) {
executor.execute(() -> {
ResponseBuilder builder = Response.ok(renderReport(id));
builder.header("Content-Disposition",
"attachment; filename=report.pdf");
response.resume(builder.build());
}
}
}
Completable Future
• Futures and callbacks both have serious flaws
• Especially when it comes to significantly Reactive code
• Java SE 8 CompletableFuture significantly better for Reactive programming
• Non-blocking, event-driven, composable and functional (via lambdas)
• Easy to integrate with Java EE 7 managed executors
• Java EE 8 should embrace CompletableFuture uniformly
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
Looks are Deceiving…
Person p = ...
Assets assets = getAssets(p);
Liabilities liabilities = getLiabilities(p);
Credit credit = calculateCreditScore(assets, liabilities);
History history = getHealthHistory(p);
Health health = calculateHeathScore(history);
Coverage coverage = underwrite(credit, health);
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
The Problem with Futures (and Callbacks)
Person p = ...
Future<Assets> f1 = executor.submit(() -> getAssets(p));
Future<Liabilities> f2 = executor.submit(
() -> getLiabilities(p));
Future<Credit> f3 = executor.submit(
() -> calculateCreditScore(f1.get(), f2.get()));
// The unrelated calls below are now blocked for no reason
Future<History> f4 = executor.submit(() -> getHealthHistory(p));
Future<Health> f5 = executor.submit(
() -> calculateHeathScore(f4.get()));
// Unrelated paths join below
Future<Coverage> f6 = executor.submit(
() -> underwrite(f3.get(), f5.get()));
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
Callbacks don’t block, but introduce callback hell…
https://github.com/m-reza-rahman/reactive_javaee/blob/master/CallbackHell.java
CompletableFuture Basics
public CompletableFuture<Confirmation> processPayment(
Order order) {
CompletableFuture<Confirmation> future =
new CompletableFuture<>();
executor.execute(() -> {
Confirmation status = ...
future.complete(status);
});
return future;
}
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
paymentService
.processPayment(order)
.thenAccept(
confirmation -> System.out.println(confirmation));
Functional Reactive to the Rescue?
CompletableFuture<Assets> getAssets =
CompletableFuture.supplyAsync(() -> getAssets(person));
CompletableFuture<Liabilities> getLiabilities =
CompletableFuture.supplyAsync(() -> getLiabilities(person));
CompletableFuture<Credit> calculateCreditScore =
getAssets.thenCombineAsync(getLiabilities,
(assets, liabilities) ->
calculateCreditScore(assets, liabilities));
CompletableFuture<Health> calculateHeathScore =
CompletableFuture.supplyAsync(() -> getHealthHistory(person))
.thenApplyAsync(history -> calculateHeathScore(history));
Coverage coverage =
calculateCreditScore.thenCombineAsync(calculateHeathScore,
(credit, health) -> underwrite(credit, health)).join();
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
More Possibilities
• Reactive JPA
• Last major reactive frontier for Java EE
• Async/NIO support in underlying database driver/JDBC/Java SE
prerequisite
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
CompletableFuture<List<Country>> countries =
em.createQuery("SELECT c FROM Country c", Country.class)
.async().getResultList();
• Reactive MVC
• Similar to basic model in JAX-RS
• Reactive JSF conceptually tough
• Reactive streams
Java EE Guardians
http://javaee-guardians.io
@javaee_guardian
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
Summary
• Reactive programming well established technique, may be more important in
the future
• Java EE has long had rich support for Reactive techniques
• Things should be improved even more with Java EE 8
• Java SE 8 helps quite a bit to make the programming model easier
• Beyond Java EE application servers provide clustering, load-balancing,
replication, failover, bandwidth throttling, resource pooling, thread pooling,
caching, etc
• Be careful – Reactive is not an easy approach to take
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
Resources
• Java EE Tutorials
• http://docs.oracle.com/javaee/7/tutorial/doc/home.htm
• Java SE Tutorials
• http://docs.oracle.com/javase/tutorial/
• Digging Deeper
• http://docs.oracle.com/javaee/7/firstcup/doc/home.htm
• https://glassfish.java.net/hol/
• http://cargotracker.java.net
• Java EE Transparent Expert Groups
• http://javaee-spec.java.net
• Java EE Reference Implementation
• http://glassfish.org
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

More Related Content

What's hot (16)

Java EE 8: On the Horizon
Java EE 8:  On the HorizonJava EE 8:  On the Horizon
Java EE 8: On the Horizon
Josh Juneau
 
Batch Applications for Java Platform 1.0: Java EE 7 and GlassFish
Batch Applications for Java Platform 1.0: Java EE 7 and GlassFishBatch Applications for Java Platform 1.0: Java EE 7 and GlassFish
Batch Applications for Java Platform 1.0: Java EE 7 and GlassFish
Arun Gupta
 
Seven Points for Applying Java EE 7
Seven Points for Applying Java EE 7Seven Points for Applying Java EE 7
Seven Points for Applying Java EE 7
Hirofumi Iwasaki
 
Move from J2EE to Java EE
Move from J2EE to Java EEMove from J2EE to Java EE
Move from J2EE to Java EE
Hirofumi Iwasaki
 
GlassFish BOF
GlassFish BOFGlassFish BOF
GlassFish BOF
glassfish
 
EJB and CDI - Alignment and Strategy
EJB and CDI - Alignment and StrategyEJB and CDI - Alignment and Strategy
EJB and CDI - Alignment and Strategy
David Delabassee
 
JavaOne 2014 BOF4241 What's Next for JSF?
JavaOne 2014 BOF4241 What's Next for JSF?JavaOne 2014 BOF4241 What's Next for JSF?
JavaOne 2014 BOF4241 What's Next for JSF?
Edward Burns
 
Java API for WebSocket 1.0: Java EE 7 and GlassFish
Java API for WebSocket 1.0: Java EE 7 and GlassFishJava API for WebSocket 1.0: Java EE 7 and GlassFish
Java API for WebSocket 1.0: Java EE 7 and GlassFish
Arun Gupta
 
Best Way to Write SQL in Java
Best Way to Write SQL in JavaBest Way to Write SQL in Java
Best Way to Write SQL in Java
Gerger
 
Java EE 6 Adoption in One of the World’s Largest Online Financial Systems
Java EE 6 Adoption in One of the World’s Largest Online Financial SystemsJava EE 6 Adoption in One of the World’s Largest Online Financial Systems
Java EE 6 Adoption in One of the World’s Largest Online Financial Systems
Arshal Ameen
 
Down-to-Earth Microservices with Java EE
Down-to-Earth Microservices with Java EEDown-to-Earth Microservices with Java EE
Down-to-Earth Microservices with Java EE
Reza Rahman
 
Modern web application development with java ee 7
Modern web application development with java ee 7Modern web application development with java ee 7
Modern web application development with java ee 7
Shekhar Gulati
 
WebSocket in Enterprise Applications 2015
WebSocket in Enterprise Applications 2015WebSocket in Enterprise Applications 2015
WebSocket in Enterprise Applications 2015
Pavel Bucek
 
Testing Java EE Applications Using Arquillian
Testing Java EE Applications Using ArquillianTesting Java EE Applications Using Arquillian
Testing Java EE Applications Using Arquillian
Reza Rahman
 
JavaScript Frameworks and Java EE – A Great Match
JavaScript Frameworks and Java EE – A Great MatchJavaScript Frameworks and Java EE – A Great Match
JavaScript Frameworks and Java EE – A Great Match
Reza Rahman
 
Java EE 8 Adopt a JSR : JSON-P 1.1 & MVC 1.0
Java EE 8 Adopt a JSR : JSON-P 1.1 & MVC 1.0Java EE 8 Adopt a JSR : JSON-P 1.1 & MVC 1.0
Java EE 8 Adopt a JSR : JSON-P 1.1 & MVC 1.0
David Delabassee
 
Java EE 8: On the Horizon
Java EE 8:  On the HorizonJava EE 8:  On the Horizon
Java EE 8: On the Horizon
Josh Juneau
 
Batch Applications for Java Platform 1.0: Java EE 7 and GlassFish
Batch Applications for Java Platform 1.0: Java EE 7 and GlassFishBatch Applications for Java Platform 1.0: Java EE 7 and GlassFish
Batch Applications for Java Platform 1.0: Java EE 7 and GlassFish
Arun Gupta
 
Seven Points for Applying Java EE 7
Seven Points for Applying Java EE 7Seven Points for Applying Java EE 7
Seven Points for Applying Java EE 7
Hirofumi Iwasaki
 
GlassFish BOF
GlassFish BOFGlassFish BOF
GlassFish BOF
glassfish
 
EJB and CDI - Alignment and Strategy
EJB and CDI - Alignment and StrategyEJB and CDI - Alignment and Strategy
EJB and CDI - Alignment and Strategy
David Delabassee
 
JavaOne 2014 BOF4241 What's Next for JSF?
JavaOne 2014 BOF4241 What's Next for JSF?JavaOne 2014 BOF4241 What's Next for JSF?
JavaOne 2014 BOF4241 What's Next for JSF?
Edward Burns
 
Java API for WebSocket 1.0: Java EE 7 and GlassFish
Java API for WebSocket 1.0: Java EE 7 and GlassFishJava API for WebSocket 1.0: Java EE 7 and GlassFish
Java API for WebSocket 1.0: Java EE 7 and GlassFish
Arun Gupta
 
Best Way to Write SQL in Java
Best Way to Write SQL in JavaBest Way to Write SQL in Java
Best Way to Write SQL in Java
Gerger
 
Java EE 6 Adoption in One of the World’s Largest Online Financial Systems
Java EE 6 Adoption in One of the World’s Largest Online Financial SystemsJava EE 6 Adoption in One of the World’s Largest Online Financial Systems
Java EE 6 Adoption in One of the World’s Largest Online Financial Systems
Arshal Ameen
 
Down-to-Earth Microservices with Java EE
Down-to-Earth Microservices with Java EEDown-to-Earth Microservices with Java EE
Down-to-Earth Microservices with Java EE
Reza Rahman
 
Modern web application development with java ee 7
Modern web application development with java ee 7Modern web application development with java ee 7
Modern web application development with java ee 7
Shekhar Gulati
 
WebSocket in Enterprise Applications 2015
WebSocket in Enterprise Applications 2015WebSocket in Enterprise Applications 2015
WebSocket in Enterprise Applications 2015
Pavel Bucek
 
Testing Java EE Applications Using Arquillian
Testing Java EE Applications Using ArquillianTesting Java EE Applications Using Arquillian
Testing Java EE Applications Using Arquillian
Reza Rahman
 
JavaScript Frameworks and Java EE – A Great Match
JavaScript Frameworks and Java EE – A Great MatchJavaScript Frameworks and Java EE – A Great Match
JavaScript Frameworks and Java EE – A Great Match
Reza Rahman
 
Java EE 8 Adopt a JSR : JSON-P 1.1 & MVC 1.0
Java EE 8 Adopt a JSR : JSON-P 1.1 & MVC 1.0Java EE 8 Adopt a JSR : JSON-P 1.1 & MVC 1.0
Java EE 8 Adopt a JSR : JSON-P 1.1 & MVC 1.0
David Delabassee
 

Similar to Reactive Java EE - Let Me Count the Ways! (20)

Don't Wait! Develop responsive applications with Java EE7 instead
Don't Wait! Develop responsive applications with Java EE7 insteadDon't Wait! Develop responsive applications with Java EE7 instead
Don't Wait! Develop responsive applications with Java EE7 instead
Erin Schnabel
 
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
 
Don't Wait! Develop Responsive Applications with Java EE7 Instead
Don't Wait! Develop Responsive Applications with Java EE7 InsteadDon't Wait! Develop Responsive Applications with Java EE7 Instead
Don't Wait! Develop Responsive Applications with Java EE7 Instead
WASdev Community
 
CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE
CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EECON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE
CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE
Masoud Kalali
 
AAI-1713 Introduction to Java EE 7
AAI-1713 Introduction to Java EE 7AAI-1713 Introduction to Java EE 7
AAI-1713 Introduction to Java EE 7
WASdev Community
 
AAI 1713-Introduction to Java EE 7
AAI 1713-Introduction to Java EE 7AAI 1713-Introduction to Java EE 7
AAI 1713-Introduction to Java EE 7
Kevin Sutter
 
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
 
Speedy perception trumps speedy reception–smart asynchronous interactions (N...
Speedy perception trumps speedy reception–smart asynchronous interactions  (N...Speedy perception trumps speedy reception–smart asynchronous interactions  (N...
Speedy perception trumps speedy reception–smart asynchronous interactions (N...
Lucas Jellema
 
Speedy perception trumps speedy reception–smart asynchronous interactions - L...
Speedy perception trumps speedy reception–smart asynchronous interactions - L...Speedy perception trumps speedy reception–smart asynchronous interactions - L...
Speedy perception trumps speedy reception–smart asynchronous interactions - L...
NLJUG
 
InterConnect 2016 Java EE 7 Overview (PEJ-5296)
InterConnect 2016 Java EE 7 Overview (PEJ-5296)InterConnect 2016 Java EE 7 Overview (PEJ-5296)
InterConnect 2016 Java EE 7 Overview (PEJ-5296)
Kevin Sutter
 
Java ee7 1hour
Java ee7 1hourJava ee7 1hour
Java ee7 1hour
Frank Rodriguez
 
Java EE8 - by Kito Mann
Java EE8 - by Kito Mann Java EE8 - by Kito Mann
Java EE8 - by Kito Mann
Kile Niklawski
 
EJB 3.2 part 1
EJB 3.2 part 1EJB 3.2 part 1
EJB 3.2 part 1
megrhi haikel
 
OTN Tour 2013: What's new in java EE 7
OTN Tour 2013: What's new in java EE 7OTN Tour 2013: What's new in java EE 7
OTN Tour 2013: What's new in java EE 7
Bruno Borges
 
What’s new in Java SE, EE, ME, Embedded world & new Strategy
What’s new in Java SE, EE, ME, Embedded world & new StrategyWhat’s new in Java SE, EE, ME, Embedded world & new Strategy
What’s new in Java SE, EE, ME, Embedded world & new Strategy
Mohamed Taman
 
Unite5-EJB-2019.ppt
Unite5-EJB-2019.pptUnite5-EJB-2019.ppt
Unite5-EJB-2019.ppt
Krishna900061
 
Java EE 7 in practise - OTN Hyderabad 2014
Java EE 7 in practise - OTN Hyderabad 2014Java EE 7 in practise - OTN Hyderabad 2014
Java EE 7 in practise - OTN Hyderabad 2014
Jagadish Prasath
 
Futures and Rx Observables: powerful abstractions for consuming web services ...
Futures and Rx Observables: powerful abstractions for consuming web services ...Futures and Rx Observables: powerful abstractions for consuming web services ...
Futures and Rx Observables: powerful abstractions for consuming web services ...
Chris Richardson
 
Java EE 8
Java EE 8Java EE 8
Java EE 8
Ryan Cuprak
 
Think async
Think asyncThink async
Think async
Bhakti Mehta
 
Don't Wait! Develop responsive applications with Java EE7 instead
Don't Wait! Develop responsive applications with Java EE7 insteadDon't Wait! Develop responsive applications with Java EE7 instead
Don't Wait! Develop responsive applications with Java EE7 instead
Erin Schnabel
 
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
 
Don't Wait! Develop Responsive Applications with Java EE7 Instead
Don't Wait! Develop Responsive Applications with Java EE7 InsteadDon't Wait! Develop Responsive Applications with Java EE7 Instead
Don't Wait! Develop Responsive Applications with Java EE7 Instead
WASdev Community
 
CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE
CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EECON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE
CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE
Masoud Kalali
 
AAI-1713 Introduction to Java EE 7
AAI-1713 Introduction to Java EE 7AAI-1713 Introduction to Java EE 7
AAI-1713 Introduction to Java EE 7
WASdev Community
 
AAI 1713-Introduction to Java EE 7
AAI 1713-Introduction to Java EE 7AAI 1713-Introduction to Java EE 7
AAI 1713-Introduction to Java EE 7
Kevin Sutter
 
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
 
Speedy perception trumps speedy reception–smart asynchronous interactions (N...
Speedy perception trumps speedy reception–smart asynchronous interactions  (N...Speedy perception trumps speedy reception–smart asynchronous interactions  (N...
Speedy perception trumps speedy reception–smart asynchronous interactions (N...
Lucas Jellema
 
Speedy perception trumps speedy reception–smart asynchronous interactions - L...
Speedy perception trumps speedy reception–smart asynchronous interactions - L...Speedy perception trumps speedy reception–smart asynchronous interactions - L...
Speedy perception trumps speedy reception–smart asynchronous interactions - L...
NLJUG
 
InterConnect 2016 Java EE 7 Overview (PEJ-5296)
InterConnect 2016 Java EE 7 Overview (PEJ-5296)InterConnect 2016 Java EE 7 Overview (PEJ-5296)
InterConnect 2016 Java EE 7 Overview (PEJ-5296)
Kevin Sutter
 
Java EE8 - by Kito Mann
Java EE8 - by Kito Mann Java EE8 - by Kito Mann
Java EE8 - by Kito Mann
Kile Niklawski
 
OTN Tour 2013: What's new in java EE 7
OTN Tour 2013: What's new in java EE 7OTN Tour 2013: What's new in java EE 7
OTN Tour 2013: What's new in java EE 7
Bruno Borges
 
What’s new in Java SE, EE, ME, Embedded world & new Strategy
What’s new in Java SE, EE, ME, Embedded world & new StrategyWhat’s new in Java SE, EE, ME, Embedded world & new Strategy
What’s new in Java SE, EE, ME, Embedded world & new Strategy
Mohamed Taman
 
Java EE 7 in practise - OTN Hyderabad 2014
Java EE 7 in practise - OTN Hyderabad 2014Java EE 7 in practise - OTN Hyderabad 2014
Java EE 7 in practise - OTN Hyderabad 2014
Jagadish Prasath
 
Futures and Rx Observables: powerful abstractions for consuming web services ...
Futures and Rx Observables: powerful abstractions for consuming web services ...Futures and Rx Observables: powerful abstractions for consuming web services ...
Futures and Rx Observables: powerful abstractions for consuming web services ...
Chris Richardson
 
Ad

Recently uploaded (20)

Create Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent BuilderCreate Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent Builder
DianaGray10
 
Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...
pranavbodhak
 
Agentic AI - The New Era of Intelligence
Agentic AI - The New Era of IntelligenceAgentic AI - The New Era of Intelligence
Agentic AI - The New Era of Intelligence
Muzammil Shah
 
Supercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMsSupercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMs
Francesco Corti
 
Grannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI ExperiencesGrannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI Experiences
Lauren Parr
 
AI Emotional Actors: “When Machines Learn to Feel and Perform"
AI Emotional Actors:  “When Machines Learn to Feel and Perform"AI Emotional Actors:  “When Machines Learn to Feel and Perform"
AI Emotional Actors: “When Machines Learn to Feel and Perform"
AkashKumar809858
 
New Ways to Reduce Database Costs with ScyllaDB
New Ways to Reduce Database Costs with ScyllaDBNew Ways to Reduce Database Costs with ScyllaDB
New Ways to Reduce Database Costs with ScyllaDB
ScyllaDB
 
Microsoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentationMicrosoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentation
Digitalmara
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
James Anderson
 
Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)
Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)
Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)
Peter Bittner
 
Fortinet Certified Associate in Cybersecurity
Fortinet Certified Associate in CybersecurityFortinet Certified Associate in Cybersecurity
Fortinet Certified Associate in Cybersecurity
VICTOR MAESTRE RAMIREZ
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
Co-Constructing Explanations for AI Systems using Provenance
Co-Constructing Explanations for AI Systems using ProvenanceCo-Constructing Explanations for AI Systems using Provenance
Co-Constructing Explanations for AI Systems using Provenance
Paul Groth
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
Droidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing HealthcareDroidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing Healthcare
Droidal LLC
 
Cybersecurity Fundamentals: Apprentice - Palo Alto Certificate
Cybersecurity Fundamentals: Apprentice - Palo Alto CertificateCybersecurity Fundamentals: Apprentice - Palo Alto Certificate
Cybersecurity Fundamentals: Apprentice - Palo Alto Certificate
VICTOR MAESTRE RAMIREZ
 
LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection FunctionLSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
Dev Dives: System-to-system integration with UiPath API Workflows
Dev Dives: System-to-system integration with UiPath API WorkflowsDev Dives: System-to-system integration with UiPath API Workflows
Dev Dives: System-to-system integration with UiPath API Workflows
UiPathCommunity
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
Create Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent BuilderCreate Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent Builder
DianaGray10
 
Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...
pranavbodhak
 
Agentic AI - The New Era of Intelligence
Agentic AI - The New Era of IntelligenceAgentic AI - The New Era of Intelligence
Agentic AI - The New Era of Intelligence
Muzammil Shah
 
Supercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMsSupercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMs
Francesco Corti
 
Grannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI ExperiencesGrannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI Experiences
Lauren Parr
 
AI Emotional Actors: “When Machines Learn to Feel and Perform"
AI Emotional Actors:  “When Machines Learn to Feel and Perform"AI Emotional Actors:  “When Machines Learn to Feel and Perform"
AI Emotional Actors: “When Machines Learn to Feel and Perform"
AkashKumar809858
 
New Ways to Reduce Database Costs with ScyllaDB
New Ways to Reduce Database Costs with ScyllaDBNew Ways to Reduce Database Costs with ScyllaDB
New Ways to Reduce Database Costs with ScyllaDB
ScyllaDB
 
Microsoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentationMicrosoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentation
Digitalmara
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
James Anderson
 
Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)
Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)
Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)
Peter Bittner
 
Fortinet Certified Associate in Cybersecurity
Fortinet Certified Associate in CybersecurityFortinet Certified Associate in Cybersecurity
Fortinet Certified Associate in Cybersecurity
VICTOR MAESTRE RAMIREZ
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
Co-Constructing Explanations for AI Systems using Provenance
Co-Constructing Explanations for AI Systems using ProvenanceCo-Constructing Explanations for AI Systems using Provenance
Co-Constructing Explanations for AI Systems using Provenance
Paul Groth
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
Droidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing HealthcareDroidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing Healthcare
Droidal LLC
 
Cybersecurity Fundamentals: Apprentice - Palo Alto Certificate
Cybersecurity Fundamentals: Apprentice - Palo Alto CertificateCybersecurity Fundamentals: Apprentice - Palo Alto Certificate
Cybersecurity Fundamentals: Apprentice - Palo Alto Certificate
VICTOR MAESTRE RAMIREZ
 
LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection FunctionLSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
Dev Dives: System-to-system integration with UiPath API Workflows
Dev Dives: System-to-system integration with UiPath API WorkflowsDev Dives: System-to-system integration with UiPath API Workflows
Dev Dives: System-to-system integration with UiPath API Workflows
UiPathCommunity
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
Ad

Reactive Java EE - Let Me Count the Ways!

  • 1. Others Talk, We Listen. Reactive Java EE - Let Me Count the Ways! Reza Rahman Senior Architect [email protected] @reza_rahman
  • 2. CapTech Full-service US national IT consulting firm that focuses on client best interests, trust, servant leadership, culture, professionalism and technical excellence. #28 in Vault's Consulting Top 50 #3 Best Consulting Internship #9 Best Overall Internship #1 in Meeting Client’s Needs #7 Best Firm to Work For #1 in Career Development Ranked for the 7th Consecutive Year Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  • 3. Agenda • What Exactly is Reactive? • Touring Reactive in Java EE • Bearable Reactive with Java SE 8? Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  • 4. What’s in a Name? • “Reactive” fairly old but incredibly vague term • A big hurdle to broad adoption by average developers • Sound core principals co-opted by marketing concerns? • Event/message driven • Asynchronous • Non-blocking • Overloaded concerns to simple core principals attempted to be added on more recently • Responsive, resilient/fault-tolerant, elastic/adaptive, scalable, etc • These are important concerns not that unique to Reactive techniques • Long met by Java EE at the runtime level Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  • 5. What’s the Big Deal? • Reactive has always been an important software engineering technique • More responsive user experience • High throughput, optimal hardware/CPU/IO utilization • Loose coupling, complex event processing • Will potentially become more important • Internet of Things (IoT), device-to-device communication • Mobile, large global concurrent user bases, more chatty applications • Not necessarily a panacea • Asynchronous, event driven code is always harder to write, maintain than synchronous, blocking code • Horizontal/hardware scalability can be a cheaper/more maintainable answer Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  • 6. Reactive Java EE Copyright © 2015 CapTech Ventures, Inc. All rights reserved. JMSJMS EJB 3EJB 3 Message-Driven Beans Message-Driven Beans Asynchronous Session Beans Asynchronous Session Beans CDICDI EventsEvents ObserversObservers ServletServlet AsynchronousAsynchronous NIONIO JAX-RSJAX-RS Async on ServerAsync on Server Async on ClientAsync on Client WebSocketWebSocket Async Remote Endpoints Async Remote Endpoints Concurrency Utilities Concurrency Utilities
  • 7. JMS and Message Driven Beans • JMS one of the oldest APIs in Java EE, strongly aligned with Reactive techniques • Message/event driven, asynchronous • Loosely coupled, reliable, transactional, durable, fault tolerant, error tolerant, clustered • Message Driven Beans primary vehicle for JMS message handling • Just POJOs with annotations • Transactional, thread-safe, throttled, reliable, load-balanced, fault- tolerant, error-tolerant Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  • 8. JMS Send Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @Inject JMSContext jmsContext; @Resource(lookup = "jms/HandlingEventRegistrationAttemptQueue") Destination handlingEventQueue; ... public void receivedHandlingEventRegistrationAttempt( HandlingEventRegistrationAttempt attempt) { ... jmsContext.createProducer() .setDeliveryMode(DeliveryMode.PERSISTENT) // The default :-) .setPriority(LOW_PRIORITY) .setDisableMessageID(true) .setDisableMessageTimestamp(true) .setStringProperty("source", source) .send(handlingEventQueue, attempt); }
  • 9. Message Driven Bean Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "jms/HandlingEventRegistrationAttemptQueue"), @ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "source = 'mobile'")}) public class HandlingEventRegistrationAttemptConsumer implements MessageListener { ... public void onMessage(Message message) { ... HandlingEventRegistrationAttempt attempt = message.getBody(HandlingEventRegistrationAttempt.class); ... } }
  • 10. Great Possibilities for JMS 2.1 Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @ApplicationScoped @MaxConcurrency(10) public class HandlingEventRegistrationAttemptConsumer { @JmsListener( destinationLookup="jms/HandlingEventRegistrationAttemptQueue", selector="source = 'mobile'", batchSize=10, retry=5, retryDelay=7000, orderBy=TIMESTAMP) public void onEventRegistrationAttempt( HandlingEventRegistrationAttempt... attempts) { ... } }
  • 11. Asynchronous Session Beans • Dead simple asynchrony at the component level • Just an annotation on a POJO • Great when all that is required is greater throughput or responsiveness • Still transactional, thread-safe, throttled • Not loosely coupled, persistent, fault tolerant or error tolerant (client must explicitly handle errors) Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  • 12. Asynchronous Session Bean Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @Asynchronous public void processPayment(Payment payment) { // CPU/IO heavy tasks to process a payment } @Asynchronous public Future<Report> generateReport(ReportParameters params) { try { Report report = renderReport(params); return new AsyncResult(report); } catch(ReportGenerationException e) { return new AsyncResult(new ErrorReport(e)); }
  • 13. Asynchronous Session Bean Client Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @Inject ReportGeneratorService reportGeneratorService; ... Future<Report> future = reportGeneratorService.generateReport(parameters); ... if (future.isDone()) { Report report = future.get(); ... } ... future.cancel(true);
  • 14. @Asynchronous + CompletableFuture Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @Asynchronous public CompletableFuture<Confirmation> processPayment(Order order) { ... Confirmation status = ...; return CompletableFuture<Confirmation>.completedFuture(status); } paymentService .processPayment(order) .thenAccept( confirmation -> System.out.println(confirmation));
  • 15. CDI Events/Observers • Compact, simple, elegant, type-safe events • Essentially the observer pattern formalized via a DI framework and annotations • Offers excellent solution to loose-coupling, type-safe filtering/chaining and asynchrony (but not much else) Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  • 16. CDI Events Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @Inject @CargoInspected Event<Cargo> cargoInspected; ... public void inspectCargo(TrackingId trackingId) { ... cargoInspected.fire(cargo); } public void onCargoInspected( @Observes @CargoInspected Cargo cargo) { @Qualifier @Retention(RUNTIME) @Target({FIELD, PARAMETER}) public @interface CargoInspected {}
  • 17. Asynchronous CDI Events Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @Inject @CargoInspected Event<Cargo> cargoInspected; ... public void inspectCargo(TrackingId trackingId) { ... cargoInspected.fireAsync(cargo); } public void onCargoInspected( @Observes(async=true) @CargoInspected Cargo cargo) {
  • 18. Asynchronous Servlets and NIO • Asynchronous Servlets maximize throughput/thread utilization • Decouple connection from request thread • Return request thread back to pool • Handle IO/CPU heavy work on separate backend thread • Close cached connection when done • NIO removes possible thread blocks during slow read/write • Get notified when the IO channel might be ready • Only read/write when IO channel is ready • Obvious need when Servlet IO is particularly heavy, otherwise a complex solution Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  • 19. Asynchronous Servlet Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @WebServlet(urlPatterns={"/report"}, asyncSupported=true) public class AsyncServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { ... final AsyncContext asyncContext = request.startAsync(); asyncContext.start(() -> { ReportParameters parameters = parseReportParameters(asyncContext.getRequest()); Report report = generateReport(parameters); printReport(report, asyncContext); asyncContext.complete(); }); } }
  • 20. Asynchronous Servlet NIO (Output Stream) Copyright © 2015 CapTech Ventures, Inc. All rights reserved. private void printReport(Report report, AsyncContext context) { ServletOutputStream output = context.getResponse().getOutputStream(); WriteListener writeListener = new ReportWriteListener( output, report, context); output.setWriteListener(writeListener); }
  • 21. Asynchronous Servlet NIO (Write Listener) Copyright © 2015 CapTech Ventures, Inc. All rights reserved. class ReportWriteListener implements WriteListener { private ServletOutputStream output = null; private InputStream input = null; private AsyncContext context = null; ReportWriteListener(ServletOutputStream output, Report report, AsyncContext context) { this.output = output; this.input = report.asPdfStream(); this.context = context; } ...
  • 22. Asynchronous Servlet NIO (Write Listener) Copyright © 2015 CapTech Ventures, Inc. All rights reserved. ... public void onWritePossible() throws IOException { byte[] chunk = new byte[256]; int read = 0; while (output.isReady() && (read = input.read(chunk)) != -1) output.write(chunk, 0, read); if (read == -1) context.complete(); } public void onError(Throwable t) { context.complete(); t.printStackTrace(); } }
  • 23. Asynchronous JAX-RS • Asynchronous capabilities newly added to JAX-RS 2/Java EE 7 • Both on the server and client side • Server-side essentially identical to Servlet 3 async • Nicer declarative syntax • Client API async capabilities very symmetric to synchronous API • Both Futures and callbacks supported • JAX-RS server-side NIO promised for Java EE 8 Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  • 24. Asynchronous JAX-RS Resource Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @Stateless @Path("/reports") public class ReportsResource { ... @Path("{id}") @GET @Produces({"application/pdf"}) @Asynchronous public void generateReport( @PathParam("id") Long id, @Suspended AsyncResponse response) { ResponseBuilder builder = Response.ok(renderReport(id)); builder.header("Content-Disposition", "attachment; filename=report.pdf"); response.resume(builder.build()); } }
  • 25. Asynchronous JAX-RS Client Copyright © 2015 CapTech Ventures, Inc. All rights reserved. WebTarget target = client.target("http://.../balance")... Future<Double> future = target.request() .async().get(Double.class)); ... Double balance = future.get(); WebTarget target = client.target("http://.../balance")... target.request().async().get( new InvocationCallback<Double>() { public void complete(Double balance) { // Process balance } public void failed(InvocationException e) { // Process error } });
  • 26. Asynchrony/NIO in WebSocket • WebSocket endpoints are inherently asynchronous/event-driven • No thread-connection association in the first place • True for server and client side • Writes/sends can be made asynchronous for better throughput • Very symmetric API for both sync and async • Futures or callbacks supported • Good idea to use asynchronous send in most cases Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  • 27. Asynchronous Remote WebSocket Endpoint Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @Singleton @ServerEndpoint(value = "/chat"...) public class ChatServer { ... @OnOpen public void onOpen(Session peer) { peers.add(peer); } @OnClose public void onClose(Session peer) { peers.remove(peer); } @OnMessage public void onMessage(ChatMessage message) { for (Session peer : peers) { ...peer.getAsyncRemote().sendObject(message)... } } }
  • 28. Java EE Concurrency Utilities • Allows for lower-level threading/asynchronous capabilities in Java EE in a safe, reliable, managed fashion • Very specialized code, custom workloads • Fairly small extension of Java SE Concurrency Utilities • ManagedExecutorService • ManagedThreadFactory Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  • 29. Managed Executor Service Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @Path("/reports") public class ReportsResource { @Resource ManagedExecutorService executor; ... @Path("{id}") @GET @Produces({"application/pdf"}) public void generateReport( @PathParam("id") Long id, @Suspended AsyncResponse response) { executor.execute(() -> { ResponseBuilder builder = Response.ok(renderReport(id)); builder.header("Content-Disposition", "attachment; filename=report.pdf"); response.resume(builder.build()); } } }
  • 30. Completable Future • Futures and callbacks both have serious flaws • Especially when it comes to significantly Reactive code • Java SE 8 CompletableFuture significantly better for Reactive programming • Non-blocking, event-driven, composable and functional (via lambdas) • Easy to integrate with Java EE 7 managed executors • Java EE 8 should embrace CompletableFuture uniformly Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  • 31. Looks are Deceiving… Person p = ... Assets assets = getAssets(p); Liabilities liabilities = getLiabilities(p); Credit credit = calculateCreditScore(assets, liabilities); History history = getHealthHistory(p); Health health = calculateHeathScore(history); Coverage coverage = underwrite(credit, health); Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  • 32. The Problem with Futures (and Callbacks) Person p = ... Future<Assets> f1 = executor.submit(() -> getAssets(p)); Future<Liabilities> f2 = executor.submit( () -> getLiabilities(p)); Future<Credit> f3 = executor.submit( () -> calculateCreditScore(f1.get(), f2.get())); // The unrelated calls below are now blocked for no reason Future<History> f4 = executor.submit(() -> getHealthHistory(p)); Future<Health> f5 = executor.submit( () -> calculateHeathScore(f4.get())); // Unrelated paths join below Future<Coverage> f6 = executor.submit( () -> underwrite(f3.get(), f5.get())); Copyright © 2015 CapTech Ventures, Inc. All rights reserved. Callbacks don’t block, but introduce callback hell… https://github.com/m-reza-rahman/reactive_javaee/blob/master/CallbackHell.java
  • 33. CompletableFuture Basics public CompletableFuture<Confirmation> processPayment( Order order) { CompletableFuture<Confirmation> future = new CompletableFuture<>(); executor.execute(() -> { Confirmation status = ... future.complete(status); }); return future; } Copyright © 2015 CapTech Ventures, Inc. All rights reserved. paymentService .processPayment(order) .thenAccept( confirmation -> System.out.println(confirmation));
  • 34. Functional Reactive to the Rescue? CompletableFuture<Assets> getAssets = CompletableFuture.supplyAsync(() -> getAssets(person)); CompletableFuture<Liabilities> getLiabilities = CompletableFuture.supplyAsync(() -> getLiabilities(person)); CompletableFuture<Credit> calculateCreditScore = getAssets.thenCombineAsync(getLiabilities, (assets, liabilities) -> calculateCreditScore(assets, liabilities)); CompletableFuture<Health> calculateHeathScore = CompletableFuture.supplyAsync(() -> getHealthHistory(person)) .thenApplyAsync(history -> calculateHeathScore(history)); Coverage coverage = calculateCreditScore.thenCombineAsync(calculateHeathScore, (credit, health) -> underwrite(credit, health)).join(); Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  • 35. More Possibilities • Reactive JPA • Last major reactive frontier for Java EE • Async/NIO support in underlying database driver/JDBC/Java SE prerequisite Copyright © 2015 CapTech Ventures, Inc. All rights reserved. CompletableFuture<List<Country>> countries = em.createQuery("SELECT c FROM Country c", Country.class) .async().getResultList(); • Reactive MVC • Similar to basic model in JAX-RS • Reactive JSF conceptually tough • Reactive streams
  • 37. Summary • Reactive programming well established technique, may be more important in the future • Java EE has long had rich support for Reactive techniques • Things should be improved even more with Java EE 8 • Java SE 8 helps quite a bit to make the programming model easier • Beyond Java EE application servers provide clustering, load-balancing, replication, failover, bandwidth throttling, resource pooling, thread pooling, caching, etc • Be careful – Reactive is not an easy approach to take Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  • 38. Resources • Java EE Tutorials • http://docs.oracle.com/javaee/7/tutorial/doc/home.htm • Java SE Tutorials • http://docs.oracle.com/javase/tutorial/ • Digging Deeper • http://docs.oracle.com/javaee/7/firstcup/doc/home.htm • https://glassfish.java.net/hol/ • http://cargotracker.java.net • Java EE Transparent Expert Groups • http://javaee-spec.java.net • Java EE Reference Implementation • http://glassfish.org Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  • 39. Copyright © 2015 CapTech Ventures, Inc. All rights reserved.