This document provides an overview of reactive programming concepts like state, time, sync vs async operations, futures and promises. It discusses different approaches to reactive programming in Java like using CompletableFuture, JDeferred and RxJava. It also covers functional programming concepts, data streams, reactive Spring and the future of reactive programming in Java 9 and beyond.
Introduction to Reactive Programming concepts by Andres Almiray, outlining his credentials and the presentation agenda.
Comparison of Imperative and Declarative programming approaches, explaining their states and time management.
Explanation of Futures and Promises in programming, their differences, and examples of usage in Java.Insights into CompletableFuture added in Java 8, and comparison to JDeferred for improved function composition.Demonstration of producer and consumer patterns using Promises in both Java and JDeferred.
Introduction to Reactive Programming using RxJava, showcasing producer and consumer implementations.
Methods for handling paginated data in Reactive programming, emphasizing Observable sequences.
Overview of reactive programming resources, including data streams, observables, and observer concepts.
Functional programming concepts including immutability, side effects, and libraries like Vert.x and gRPC.
Exploration of Java 9+ Flow API, Reactive Spring data, and useful resources for further learning.
Final thoughts on the paradigm shifts brought by Reactive Programming and gratitude for the audience's participation.
@aalmiray SYNC VS ASYNC Synchronousoperations block the flow until they yield a result (success or error). Asynchronous operations DO NOT block the flow, rather they allow the program to continue. Results (or errors) will be handled at a later point in time, typically using functions (callbacks).
8.
@aalmiray FUTURES AND PROMISES Theydescribe an object that acts as mediator for a value that may be unknown at init time. The terms Future and Promise are normally used interchangeably but there’s a difference: • A Future is a read-only reference of the expected value. • A Promise is a write-once container that sets the value of the Future. https://en.wikipedia.org/wiki/Futures_and_promises
9.
@aalmiray FUTURES AND PROMISES Futuresin Java are synchronous, this can be demonstrated by the following method signatures found in java.util.concurrent.Future V get() V get(long timeout, TimeUnit unit)
10.
@aalmiray FUTURES AND PROMISES Java8 added a new type, CompletableFuture, that implements CompletableStage, which in turns defines the contract of a Promise, such as thenAccept(Consumer<? Super T> action) whenComplete(BiConsumer<? super T,? super Throwable> action) exceptionally(Function<Throwable,? extends T> fn) … and many others
11.
@aalmiray FUTURES AND PROMISES JDeferredoffers a different API that allows better function composition Promise<D, F, P> then(DoneCallback<D> doneCallback) Promise<D, F, P> done(DoneCallback<D> callback) Promise<D, F, P> fail(FailCallback<F> callback) Promise<D, F, P> always(AlwaysCallback<D, F> callback)
@aalmiray HTTP://REACTIVEX.IO/ Reactive Programming API,provides observable data flows. Multiple implementations in more than 20 programming languages Java adn Javascript implement the http://www.reactive- streams.org/ specification
@aalmiray DATA STREAMS A sequenceof values calculated over time. Values are emitted when ready; they are computed without blocking consumers. Consumers listen to changes in the data stream and react to said changes (hence the name Reactive Programming). Push vs pull model.
34.
@aalmiray OBSERVABLE/OBSERVER Data streams areof type Observable while consumer are of type Observer. The Observable type exposes multiple operations that allow value composition, combinations, filtering, and other value transformations. NOTE: many operations from RxJava/Reactor generate a new Observable (decorator pattern)
@aalmiray IMMUTABILITY Immutable data structuresand/or data containers (POJOs) complement Functional Programming in a natural way. They allow data sharing with multiple consumers and/or functions without fear of changes affecting the original data. They help reduce synchronization points.
@aalmiray JAVA 9+ FLOW Java9 implements the Reactive Streams in its own way with java.util.concurrent.Flow This API allows combining streams from different providers (RxJava, Reactor, etc). Additions to the CompletableStage/CompletableFuture API.
48.
@aalmiray REACTIVE SPRING (WEB) @GetMapping("/accounts/{id}/alerts") publicFlux<Alert> getAccountAlerts(@PathVariable Long id) { return this.repository.getAccount(id) .flatMap(account -> this.webClient .perform(get("/alerts/{key}", account.getKey())) .extract(bodyStream(Alert.class))); } https://spring.io/blog/2016/07/28/reactive-programming-with-spring-5-0-m1
49.
@aalmiray REACTIVE SPRING (DATA+WEB) @RestController classPersonController { private final PersonRepository people; public PersonController(PersonRepository people) { this.people = people; } @GetMapping("/people") Flux<String> namesByLastname(@RequestParam Mono<String> lastname) { Flux<Person> result = repository.findByLastname(lastname); return result.map(it -> it.getFullName()); } } https://spring.io/blog/2016/11/28/going-reactive-with-spring-data