Slide 1

Slide 1 text

A step-by-step guide from traditional Java EE to reactive microservice design Ondrej Mihályi @OndroMih https://ondro.inginea.eu

Slide 2

Slide 2 text

 Engineer at Payara  Java EE developer, lecturer  MicroProfile member  Czech JUG leader Ondro Mihályi Web: ondro.inginea.eu www.payara.fish Twitter: @OMihalyi

Slide 3

Slide 3 text

 Reactive improvements  Separation to micro-svc  Deploy with Docker https://github.com/OndrejM-demonstrations/ ReactiveWay-cargotracker-ext Agenda

Slide 4

Slide 4 text

@ONDROMIH Majority of the enterprise code Request started → resource required (DB, WS, files) → request the resource → the resource is slow WHAT DO WE DO? (hint: it’s quite natural)

Slide 5

Slide 5 text

@ONDROMIH We simply...

Slide 6

Slide 6 text

@ONDROMIH We simply... ...WAIT No shame, we all do it  Simple  Thread-safe  Enough in most cases

Slide 7

Slide 7 text

@ONDROMIH But what if...

Slide 8

Slide 8 text

@ONDROMIH If waiting is too long...  Thread resources are wasted  Processing may slow down or halt if all threads waiting  Users don’t receive a timely feedback  Failures lead to more failures

Slide 9

Slide 9 text

@ONDROMIH If waiting is too long...  Thread resources are wasted  Processing may slow down or halt if all threads waiting  Users don’t receive a timely feedback  Failures lead to more failures

Slide 10

Slide 10 text

@ONDROMIH Reactive manifesto http://www.reactivemanifesto.org/

Slide 11

Slide 11 text

@ONDROMIH Reactive idea in a nutshell Single-threaded Reactive - not bound to a thread

Slide 12

Slide 12 text

@ONDROMIH Threads reused between calls

Slide 13

Slide 13 text

@ONDROMIH Example traditional application We’re going to improve it reactively Original project: https://github.com/javaee/cargotracker Reactive improvements: https://github.com/OndrejM-demonstrations/Rea ctiveWay-cargotracker-ext

Slide 14

Slide 14 text

@ONDROMIH Traditional design

Slide 15

Slide 15 text

@ONDROMIH More reactive design

Slide 16

Slide 16 text

@ONDROMIH Chaining callbacks CompletableFuture in Java 8  complete, completeExceptionally  thenAccept, thenApply, thenCompose  exceptionally Alternatively use third-party libraries  RxJava observables

Slide 17

Slide 17 text

@ONDROMIH 3. Messages WebSocket, Server-Sent Events CDI events  Light-weight but extensible API  Events don’t cross JVM boundaries  Can we turn them into messages?

Slide 18

Slide 18 text

@ONDROMIH Even more reactive design

Slide 19

Slide 19 text

@ONDROMIH Simple messaging and caching Payara CDI Event Bus  Async events on all nodes  On top of CDI events  Uses Hazelcast JCache API  Scalable distributed memory  Replication and failover @Inject @Outbound Event ev; /* handle in the same or a different JVM: */ void handle(@Observes @Inbound MyMsg ev) { … }

Slide 20

Slide 20 text

@ONDROMIH Multiple threads, need to track origin and arrival of responses, errors may be unnoticed, new design patterns Don’t over-engineer, but leave space for future improvements Java EE and MicroProfile allow for gradual improvements Reactive programming isn’t easy

Slide 21

Slide 21 text

@ONDROMIH Thank you!