jabrena / ReactiveLab

A Lab about reactive technologies with Java 8 to be applied on Backend.
https://jabrena.github.io/ReactiveLab/www/
MIT License
1 stars 0 forks source link

Add contents for Game 5 #9

Open jabrena opened 7 years ago

jabrena commented 7 years ago

ReactiveX: An API for asynchronous programming with observable streams

http://reactivex.io/ https://github.com/ReactiveX/RxJava https://github.com/Reactive-Extensions/ http://rxmarbles.com/ https://en.wikipedia.org/wiki/Observer_pattern

The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.

                single items                    multiple items
synchronous  T getData()                    Iterable<T> getData()
asynchronous     Future<T> getData()    Observable<T> getData()

Observables Are Composable Techniques like Java Futures are straightforward to use for a single level of asynchronous execution but they start to add non-trivial complexity when they’re nested.

It is difficult to use Futures to optimally compose conditional asynchronous execution flows (or impossible, since latencies of each request vary at runtime). This can be done, of course, but it quickly becomes complicated (and thus error-prone) or it prematurely blocks on Future.get(), which eliminates the benefit of asynchronous execution.

ReactiveX Observables, on the other hand, are intended for composing flows and sequences of asynchronous data.

https://gist.github.com/benjchristensen/4670979 https://gist.github.com/benjchristensen/4671081 https://gist.github.com/benjchristensen/4671081#file-futuresb-java-L163 https://gist.github.com/benjchristensen/4677544

jabrena commented 7 years ago

Observable:

http://reactivex.io/documentation/observable.html https://en.wikipedia.org/wiki/Reactor_pattern

In ReactiveX an observer subscribes to an Observable. Then that observer reacts to whatever item or sequence of items the Observable emits.

“Hot” and “Cold” Observables

Observable (onNext, onError, and onCompleted) Single (onSuccess, onError)

jabrena commented 7 years ago

Operators: http://reactivex.io/documentation/operators.html

Composition via Observable Operators

This documentation groups information about the various operators and examples of their usage into the following pages:

Creating Observables
Create, Defer, Empty/Never/Throw, From, Interval, Just, Range, Repeat, Start, and Timer

Transforming Observable Items
Buffer, FlatMap, GroupBy, Map, Scan, and Window

Filtering Observables
Debounce, Distinct, ElementAt, Filter, First, IgnoreElements, Last, Sample, Skip, SkipLast, Take, and TakeLast

Combining Observables
And/Then/When, CombineLatest, Join, Merge, StartWith, Switch, and Zip

Error Handling Operators
Catch and Retry

Utility Operators
Delay, Do, Materialize/Dematerialize, ObserveOn, Serialize, Subscribe, SubscribeOn, TimeInterval, Timeout, Timestamp, and Using

Conditional and Boolean Operators
All, Amb, Contains, DefaultIfEmpty, SequenceEqual, SkipUntil, SkipWhile, TakeUntil, and TakeWhile

Mathematical and Aggregate Operators
Average, Concat, Count, Max, Min, Reduce, and Sum
Converting Observables
To

Connectable Observable Operators
Connect, Publish, RefCount, and Replay

Backpressure Operators
a variety of operators that enforce particular flow-control policies
jabrena commented 7 years ago

Single:

http://reactivex.io/documentation/single.html

Single vs Observarble

jabrena commented 7 years ago

Subject:

http://reactivex.io/documentation/subject.html

A Subject is a sort of bridge or proxy that is available in some implementations of ReactiveX that acts both as an observer and as an Observable. Because it is an observer, 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.

jabrena commented 7 years ago

Scheduler:

If you want to introduce multithreading into your cascade of Observable operators, you can do so by instructing those operators (or particular Observables) to operate on particular Schedulers.

jabrena commented 7 years ago

RxJava 1.x vs RxJava 2.x:

https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0

RxJava 2.0 has been completely rewritten from scratch on top of the Reactive-Streams specification. The specification itself has evolved out of RxJava 1.x and provides a common baseline for reactive systems and libraries.

When to use Observable

You have a flow of no more than 1000 elements at its longest: i.e., you have so few elements over time that there is practically no chance for OOME in your application. You deal with GUI events such as mouse moves or touch events: these can rarely be backpressured reasonably and aren't that frequent. You may be able to handle a element frequency of 1000 Hz or less with Observable but consider using sampling/debouncing anyway. Your flow is essentially synchronous but your platform doesn't support Java Streams or you miss features from it. Using Observable has lower overhead in general than Flowable. (You could also consider IxJava which is optimized for Iterable flows supporting Java 6+).

When to use Flowable

Dealing with 10k+ of elements that are generated in some fashion somewhere and thus the chain can tell the source to limit the amount it generates. Reading (parsing) files from disk is inherently blocking and pull-based which works well with backpressure as you control, for example, how many lines you read from this for a specified request amount). Reading from database through JDBC is also blocking and pull-based and is controlled by you by calling ResultSet.next() for likely each downstream request. Network (Streaming) IO where either the network helps or the protocol used supports requesting some logical amount. Many blocking and/or pull based data sources which may eventually get a non-blocking reactive API/driver in the future.

jabrena commented 7 years ago

http://www.reactive-streams.org/

Reactive Streams is an initiative to provide a standard for asynchronous stream processing with non-blocking back pressure. This encompasses efforts aimed at runtime environments (JVM and JavaScript) as well as network protocols.

jabrena commented 7 years ago

http://stackoverflow.com/questions/35329845/difference-between-completablefuture-future-and-observable-rxjava http://stackoverflow.com/questions/30216979/difference-between-java-8-streams-and-rxjava-observables http://stackoverflow.com/questions/21163108/custom-thread-pool-in-java-8-parallel-stream http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinTask.html#fork-- https://www.javacodegeeks.com/2015/08/using-java-8-completablefuture-and-rx-java-observable.html http://es.slideshare.net/jpaumard/java-8-stream-api-and-rxjava-comparison http://fahdshariff.blogspot.co.uk/2016/06/java-8-completablefuture-vs-parallel.html

jabrena commented 7 years ago

http://openjdk.java.net/projects/code-tools/jmh/

jabrena commented 7 years ago

http://openjdk.java.net/jeps/266