This document discusses how to build reactive applications with Java 8 using frameworks like Play, Akka, and Scala. It covers reactive requests, composition, and push capabilities in Play and building actor-based, asynchronous, distributed systems with Akka. It also demonstrates interoperability between Java and Scala and provides links to Activator templates for getting started with reactive applications.
9. Asynchronous Request
Underthe covers Play implements allrequests in this way
publicstaticF.Promise<Result>asyncFoo(){
returnF.Promise.promise(()->ok("asyncfoo"));
}
10. Reactive Request (Async + Non-
Blocking)
Needs to have something to notblock on
publicstaticIntegerlongRunningCalculation(){
return(5134*5789)/349;
}
publicstaticF.Promise<Result>basicPromise(){
F.Promise<Integer>promise=F.Promise.promise(()->longRunningCalculation());
returnpromise.map((Integeri)->ok("Thecalculationresultis:"+i));
}
11. Reactive Requests
Two chained requests (clientto server& serverto typesafe.com)
both async& non-blocking
publicstaticF.Promise<Result>reactiveRequest(){
F.Promise<WS.Response>typesafePromise=WS.url("http://www.typesafe.com").get();
returntypesafePromise.map(response->ok(response.getBody()));
}
12. Reactive Composition
Three requests, two in parrallel(depending on thread availability)
allasync& non-blocking
publicstaticF.Promise<Result>reactiveComposition(){
finalF.Promise<WS.Response>twitterPromise=WS.url("http://www.twitter.com").get();
finalF.Promise<WS.Response>typesafePromise=WS.url("http://www.typesafe.com").get();
returntwitterPromise.flatMap((twitter)->
typesafePromise.map((typesafe)->
ok(twitter.getBody()+typesafe.getBody())));
}
20. Supervision with Lambdas
Take differentactions depending on the failure
privatestaticSupervisorStrategystrategy=
newOneForOneStrategy(10,Duration.create("1minute"),DeciderBuilder.
match(ArithmeticException.class,e->resume()).
match(NullPointerException.class,e->restart()).
match(IllegalArgumentException.class,e->stop()).
matchAny(o->escalate()).build());
@Override
publicSupervisorStrategysupervisorStrategy(){
returnstrategy;
}
28. Demo: Reactive Stocks (Java 8)
Getthe ActivatorTemplate:
http://typesafe.com/activator/template/reactive-stocks-java8
AkkaActors forthread-safe state and non-requestbased events
Play Framework forReactive Composition, Reactive Push, and a
JavaScriptUI
Reactive Stocks (Java8)
29. Get Started with Activator
http://typesafe.com/activator/template/hello-akka-java8
http://typesafe.com/activator/template/reactive-java8-play
http://typesafe.com/activator/template/akka-supervision-java-lambda
http://typesafe.com/activator/template/akka-sample-fsm-java-lambda
http://typesafe.com/activator/template/akka-sample-persistence-java-lambda
http://typesafe.com/activator/template/reactive-stocks-java8
Hello Akka!(Java8)
Go Reactive with Java8 & Play Framework
AkkaSupervision in Javawith Lambdas
AkkaFSMin Javawith Lambdas
AkkaPersistence Samples in Javawith Lambdas
Reactive Stocks (Java8)