Anzeige
Anzeige

Más contenido relacionado

Anzeige

Reactive features of MicroProfile you need to learn

  1. Reactive Features of MicroProfile You Need To Learn Ondro Mihályi http://www.payara.fish @ONDROMIH
  2. @ONDROMIH Agenda
  3. @ONDROMIH Agenda Flowable.fromPublisher(p) .map(mapper) .subscribe(); Stream.generate(s) .map(mapper) .forEach();
  4. @ONDROMIH Agenda org.reactivestreams (Java 8+) java.util.concurrent.Flow (Java 9+)
  5. @ONDROMIH Agenda <dependency> <groupId> org.eclipse.microprofile </groupId> <artifactId> microprofile </artifactId> <scope>provided</scope> </dependency> Reactive Streams
  6. @ONDROMIH Goals to Solve ● Better user experience ● Save resources/costs ● Keep up with massive loads ● Handle failures (recover)
  7. @ONDROMIH Traditional Thread Usage ● Dedicated thread per request ● Threads acquired from a pool
  8. @ONDROMIH
  9. @ONDROMIH Reactive in a nutshell
  10. @ONDROMIH Reactive in a nutshell
  11. @ONDROMIH Asynchronous nature ● To avoid blocking threads, responses must be processes asynchronously (later) ● Callbacks, Promises, CompletionStage, Observers
  12. @ONDROMIH Code comparison Simple blocking code data = readData(query); /* runs after data retrieved */ response = buildResponse(data); return response; Asynchronous code readData(query) // after data retrieved .thenApply( buildResponse) .thenAccept( sendResponse); // runs immediately return;
  13. @ONDROMIH Existing asynchronous APIs ● Java SE: CompletionStage ● RxJava library: Flowable ● Spring Reactor library: Flux
  14. @ONDROMIH Reactive >= asynchronous ● Process data as soon as possible (streams) ● Recover from failures ● Control data flow (protect the consumer)
  15. @ONDROMIH Reactive pipeline ● Data stream – Subscribe → Process → Publish – Handle: ● Data, Error, Completion
  16. @ONDROMIH Existing Stream/Pipeline APIs ● Java SE: Streams – no subscribe, publish ● Reactive Streams spec – no processing ● Java SE 9+: Flow – as Reactive Streams ● Libraries: RxJava, Project Reactor
  17. @ONDROMIH Java Stream API stream .filter(isValid) .map(toJson) .peek(logData) .forEach(sendData);
  18. @ONDROMIH Java Stream API ● Stream processing API, but… ● No way to subscribe! ● Terminal operations blocking ● An exception terminates processing
  19. @ONDROMIH RxJava pipeline Flowable.fromPublisher(p) .filter(isValid) .map(toJson) .doOnEach(logData) .doOnError(handleError) .doFinally(releaseResources) .subscribe(sendData);
  20. @ONDROMIH RxJava pipeline ● Complete solution but… ● Complicated API ● Incompatible with other reactive libraries ● Reactive Streams and Java SE Flow – Standard interface among reactive APIs
  21. @ONDROMIH MicroProfile Reactive ● MicroProfile – brings common APIs – collaboration of vendors and library creators ● Reactive APIs – Stream processing – Message passing – Fault Tolerance
  22. @ONDROMIH MicroProfile pipeline ReactiveStreams.fromPublisher(p) .filter(isValid) .map(toJson) .peek(logData) .onError(handleError) .onComplete(releaseResources) .forEach(sendData) .run();
  23. @ONDROMIH Java Streams vs. MicroProfile ReactiveStreams .fromPublisher(p) .filter(isValid) .map(toJson) .peek(logData) .onError(handleErr) .onComplete(release) .forEach(sendData) .run(); stream .filter(isValid) .map(toJson) .peek(logData) .forEach(sendData);
  24. @ONDROMIH RxJava vs. MicroProfile ReactiveStreams .fromPublisher(p) .filter(isValid) .map(toJson) .peek(logData) .onError(handleErr) .onComplete(release) .forEach(sendData) .run(); Flowable .fromPublisher(p) .filter(isValid) .map(toJson) .doOnEach(logData) .doOnError(handle) .doFinally(release) .subscribe(send);
  25. @ONDROMIH MicroProfile operators ● Consuming (findFirst, forEach, collect) ● Filtering (filter, limit, skip) ● Transforming (map, flatMap) ● Peeking, Connecting, ErrorHandlig ● Javadoc with diagrams: https://download.eclipse.org/microprofile/microprofile-react ive-streams-operators-1.0/apidocs/
  26. @ONDROMIH MicroProfile plus RxJava org.reactivestreams.Publisher pub = ReactiveStreams.fromPublisher(p) .map(toJson) .onError(handleError) .onComplete(releaseResources) .buildRs(); Flowable .fromPublisher(pub) .window(3) .subscribe(sendBatch);
  27. @ONDROMIH Extended Reactive Pipeline ● Data stream – Receive Message → Subscribe → Process → Publish → Send Message
  28. @ONDROMIH Messaging pipeline
  29. @ONDROMIH MicroProfile message API @Outgoing("source") String json() {…} @Incoming("source") @Outgoing("target") Entity convertJson(String json) {…} @Incoming("target") void method(Entity entity) {…}
  30. @ONDROMIH MicroProfile message API @Incoming("source") @Outgoing("target") Publisher<Message<Item>> splitList(Message<ItemList> list) {…}
  31. @ONDROMIH Message acknowledgement ● Automatic: Pre- and Post-processing ● Manual: Message.ack()
  32. @ONDROMIH Connectors ● Internal channels (by default) ● Pluggable connectors to remote brokers – CDI beans, implement IncomingConnectorFactory and/or OutgoingConnectorFactory – Examples: Kafka, MQTT, Camel, AMQP
  33. @ONDROMIH Fault Tolerance ● Annotations on methods of CDI beans ● Retry, Circuit breaker, Fallback, … ● Support CompletionStage return type
  34. @ONDROMIH Fault Tolerance API @Asynchronous @Retry @Fallback(fallbackMethod="handleError") public CompletionStage<List> load() { return ReactiveStreams .fromPublisher(retrieveData()) .filter(isValid) .toList() .run(); }
  35. @ONDROMIH MicroProfile implementations ● Payara, Helidon, OpenLiberty, Quarkus, SmallRye, … ● Around 10 runtimes for full MicroProfile ● Various libraries support a subset of APIs
  36. @ONDROMIH Thank you! ● https://www.payara.fish ● https://microprofile.io ● https://www.reactivemanifesto.org ● http://www.reactive-streams.org ● https://github.com/eclipse/microprofile-reactive-streams-operators/releases ● https://github.com/eclipse/microprofile-reactive-messaging/releases
  37. Not using the Payara Platform yet? Download the open source software: Payara Server or Payara Micro​ https://payara.fish/downloads​ ​ Need support for the Payara Platform?​ https://payara.fish/support​
Anzeige