Reactive Programming
Jeff Wang
Agenda
-
Reactive Manifesto
-
REACTIVE STREAMS
-
Reactive Extensions (RX)
-
Marble Diagrams
Reactive Manifesto
- The Reactive Manifesto is a document that defines the core principles of reactive programming
- First released in 2013, Updated released in 2014
- Bible for the programmers of the reactive programming religion
The 4 principles of the Reactive Manifesto
- Responsive
- Resilient
- Elastic
- Message Driven
Responsive
The system responds in a timely manner if at all possible.
Responsiveness is the cornerstone of usability
Resilient
The system stays responsive in the face of failure.
Elastic
The system stays responsive under varying workload.
It can react to changes in the input rate by increasing or decreasing the resources allocated to service these inputs.
Message Driven
The system relies on asynchronous message passing to establish a boundary between components that ensures loose coupling, isolation, and location transparency.
The four tenets of the Reactive Manifesto
Reactive programming
- Reactive Programming offers productivity for Developers—through performance and resource efficiency—at the component level for internal logic and dataflow management.
- Reactive programming is programming with asynchronous data streams by Event-driven.
Reactive systems
- Reactive Systems offers productivity for Architects and DevOps through resilience and elasticity at the system level, for building “Cloud Native”1 or other large-scale distributed systems.
- The foundation for a Reactive System is Message-Driven, which creates a temporal boundary between components that allows them to be decoupled in time.
Reactive Streams
- A stream is a sequence of ongoing events ordered in time.
- It is an initiative to provide a standard for asynchronous stream processing with non-blocking backpressure.
- These interfaces are now part of Java 9, nested within the new java.util.concurrent.Flow class
Flow API
-
Flow.Publisher<T>
- Flow.Subscriber<T>
- Flow.Subscription
- Flow.Processor<T, R>
Publisher & Subscriber
- The Publisher is a Java functional interface that allows a Subscriber to register itself as a listener of the events issued by the Publisher.
- The Subscriber interface has four callback methods that are invoked by the Publisher when it produces the corresponding events.
@FunctionalInterface
public interface Publisher<T> {
void subscribe(Subscriber<? super T> s);
}
Use the subscribe method to register itself to Publisher
public interface Subscriber<T> {
void onSubscribe(Subscription s);
void onNext(T t);
void onError(Throwable t);
void onComplete();
}
onSubscribe onNext* (onError | onComplete)?
- onSubscribe:
Invoked as the first event -
onNext:
Invoked when receiving the event. - onComplete:
Callback to signify that no more elements will be produced -
onError:
Invoked if the Publisher experiences a failure.
Subscription
- The Subscription interface declares two methods.
- The Subscriber can use the first method to notify the Publisher that it’s ready to process a given number of events.
- The second method allows it to cancel the Subscription, thereby telling the Publisher that it’s no longer interested in receiving its events.
public interface Subscription {
void request(long n);
void cancel();
}
Processor
- The interface represents a transformation stage of the events processed through the reactive stream.
- When receiving an error, the Processor can choose to recover from it or immediately propagate the onError signal to its Subscriber(s).
public interface Processor<T, R> extends Subscriber<T>, Publisher<R> { }
The life cycle of the Flow API
REACTIVE EXTENSIONS (RX)
ReactiveX
-
Observable
-
Observer
-
Operators
-
Streams
-
Schedulers
-
Subjects
ReactiveX is a library for composing asynchronous and event-based programs by using observable sequences.
Observable
- Observable is a single abstraction represents the idea of an invokable collection of future values or events
- A sequence of the element which includes time concept
- Combine of Observer Pattern and Iterator Pattern
Observer
A collection of callbacks that knows how to listen to values delivered by the Observable.
public interface Observer<T> {
void onSubscribe(Disposable d);
void onNext(T t);
void onError(Throwable t);
void onComplete();
}
Operators
- Pure functions that enable a functional programming style of dealing with collections with operations like map, filter, reduce, etc.
- Most operators operate on an Observable and return an Observable. This allows you to apply these operators one after the other, in a chain.
schedulers
- Are centralized dispatchers to control concurrency, allowing us to coordinate when computation happens.
- ObserveOn: The operator which affects the thread that the Observable will use below where that operator appears
subjects
- A Subject is a sort of bridge or proxy that acts both as an Subscriber and as an Observable.
- It can subscribe to one or more Observables, and because it is an Observable, it can pass through the items it observes by reemitting them, and it can also emit new items.
MARBLE DIAGRAMS
Reference
Thanks
Reactive Programming
By Chien Chieh Wang
Reactive Programming
- 53