19
REACTIVE WITH PLAY AND AKKA Markus Jura

Introduction to Reactive with Play and Akka - Markus Jura

  • Upload
    nljug

  • View
    227

  • Download
    3

Embed Size (px)

Citation preview

REACTIVE WITH PLAY AND AKKAMarkus Jura

REACTIVE APPS

reactivemanifesto.org

ProductivityScalableModern web & mobile

ROUTESVERB PATH CONTROLLER_METHOD

GET / controllers.Application.index()GET /foo controllers.Application.foo()

Declarative, type-safe URL Mapping

CONTROLLERSpublic static Result index() { return ok("Hello World!");}

ASYNCHRONOUS REQUESTimport play.libs.F.*;

private static Integer calc() { return (5134 * 5789) / 349;}

public static Promise<Result> basicPromise() { Promise<Integer> promise = Promise.promise(() -> calc()); return promise.map(i -> ok("Result is: " + i));}

HTTP REQUESTpublic static Promise<Result> getPage() { final Promise<WSResponse> promise = WS.url("http://google.com").get(); return promise.map(response -> ok(response.getBody()));}

CONCURRENT HTTP REQUESTSpublic static Promise<Result> composition() { final Promise<WSResponse> googlePromise = WS.url("http://google.com").get(); final Promise<WSResponse> twitterPromise = WS.url("http://twitter.com").get();

return googlePromise.flatMap(google -> twitterPromise.map(twitter -> ok(google.getBody() + twitter.getBody())));}

WEBSOCKETS OVERVIEW

WEBSOCKETS CLIENT CODEws = new WebSocket("ws://url/echo")ws.onmessage = (event) -> alert(event.data)

ws.onopen = () -> ws.send("Echo")

CoffeeScript

WEBSOCKETS SERVER CODEpublic static WebSocket<JsonNode> echo() { return WebSocket.whenReady((in, out) -> { in.onMessage(out::write); in.onClose(() -> Logger.debug("Connection closed")); });}

Play Controller

Single unified programming model forSimpler concurrencySimpler distributionSimpler fault tolerance

SIMPLER CONCURRENCY

Actors let us write code in a single-threaded illusionNo locks, synchronized or other primitives needed

SIMPLER DISTRIBUTION

Everything in Akka is distributed by defaultAkka goes from remote to local by optimization

SIMPLER FAULT TOLERANCE

Akka decouples communication from failure handling:Supervisors handle failureCallers need not care (they can't anyway)

REACTIVE STOCKS ARCHITECTURE

Demo