'

Intro to Reactive Programming

Понравилась презентация – покажи это...





Слайд 0

SPRINGONE2GX WASHINGTON DC Intro To Reactive Programming Stephane Maldini Rossen Stoyanchev Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/


Слайд 1

About the Speakers • Stephane Maldini • Reactor Project Lead • Reactive Streams Spec contributor • Rossen Stoyanchev • Spring Framework committer • Spring MVC • WebSocket messaging (STOMP, SockJS) Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 2


Слайд 2

Motivation Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 3


Слайд 3

Long Running Shift to Concurrency For 30 years computer performance driven by Moore's Law; from now on, it will be driven by Amdahl's Law. Moore's Law is delivering more cores but not faster cores. March 2006 Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 4


Слайд 4

Amdahl’s Law Theoretical max speedup using multiple processors? Limited by the time needed for sequential processing. Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 5


Слайд 5

Scale Up • Remember SOA ? EJB 1 ? • distributed components, then services • functional boundaries between system components • location transparent resources pooled • … but it didn’t do well • complex / expensive solutions • full-stack approach • overhead not factored in: latency failure tolerance • Ended up in hype failure mockeries and hacker news rants Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 6


Слайд 6

Internet Scale • Social media became mainstream • massive scale • Background Processing and pooled resources regain interest • everything “write-heavy” -- candidate for async handling • e.g. a tweet • Push-based servers start to proliferate • async response handling (e.g. facebook messages) Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 7


Слайд 7

Cloud Scale • “Cloud-ready” systems began to spread around 2012 • AWS, open-source PaaS democratization • Tech giants expose internal distributed stack • Twitter, Google, Netflix... • Tools to address core concerns of distributed systems • concurrency, service discovery, monitoring Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 8


Слайд 8

Wait! Another traffic spike: IoT Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 9


Слайд 9

Scale Out • There are limits to scale the monolith way • cost efficiency aside • Memory overuse • large thread pools, application footprint • CPU underuse • blocking I/O (DB, remote service) Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 10


Слайд 10

Tales of Time and Space • Massive scale requires higher efficiency • respect physical laws • speed of light dampens inter-service communication • Pooling resources simply not enough • need to coordinate, compose, orchestrate data flows • Non-blocking must be embraced fundamentally Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 11


Слайд 11

Non-Blocking Architecture • Non-blocking requires profound change • can’t write imperative code • can’t assume single thread of control (e.g. exception handling) • Must deal with async results • listener/callbacks become unwieldy with nested composition • Everything becomes an event stream • e.g. from Input/OutputStream to stream of events Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 12


Слайд 12

Reactive Programming Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 13


Слайд 13

Generally speaking… • “Reactive” is used broadly to define event-driven systems • UI events, network protocols • Now also entering the domain of application/business logic • Non-blocking event-driven architecture Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 14


Слайд 14

Reactive Manifesto • Reflects the emerging field of scalable, non-blocking applications • A hark back to other successful manifestos • Well articulated but very broad strokes • Defines qualities of reactive systems • For reactive programming we need concrete tools Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 15


Слайд 15

Reactive Programming in Context • To build reactive apps we need two essential building blocks • Contract for interop between non-blocking components • Reactive Streams spec • API for composing asynchronous programs • e.g. Reactive Extensions Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 16


Слайд 16

Myths about “Reactive” • Async / concurrent == reactive ? • easy to end up with too many threads • fill up hand-off queue • Must be async to be reactive ? • nope but must be agnostic to source of concurrency • Reactive is the domain of specific programming languages ? • language features can help • e.g. JDK 8 lambda Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 17


Слайд 17

Reactive Streams Specification for the JVM Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 18


Слайд 18

Specification Goals • Govern the exchange of data across async boundaries • Use back-pressure for flow control • Fast sources should not overwhelm slower consumers • Provide API types, TCK Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 19


Слайд 19

Back-Pressure • When publisher maintains higher rate for extended time • queues grow without bounds • Back-pressure allows a subscriber to control queue bounds • subscriber requests # of elements • publisher produces up to # of requested • If source can’t be controlled (e.g. mouse movement clock tick) • publisher may buffer or drop • must obey # of requested elements Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 20


Слайд 20

API Types public interface Publisher<T> { void subscribe(Subscriber<? super T> s); } public void void void void } interface Subscriber<T> { onSubscribe(Subscription s); onNext(T t); onError(Throwable t); onComplete(); public interface Subscription { void request(long n); void cancel(); } All are void methods… one-way, message style public interface Processor<T R> extends Subscriber<T> Publisher<R> { } Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 21


Слайд 21

Reactive Streams API onSubscribe Publisher Subscription request(n) cancel() Subscriber onNext* (onError|onComplete)? Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 22


Слайд 22

Reactive Streams: Message Passing Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 23


Слайд 23

Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/


Слайд 24

Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/


Слайд 25

Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 26


Слайд 26

Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 27


Слайд 27

Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 28


Слайд 28

Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 29


Слайд 29

Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 30


Слайд 30

Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 31


Слайд 31

Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/


Слайд 32

Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/


Слайд 33

Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/


Слайд 34

Spec Rules • No concurrent calls from Publisher to Subscriber (1.3) or vice versa (2.7) • A Subscriber may perform work synchronously or asynchronously • either way must be non-blocking (2.2) • Upper bound on open recursion required (3.3) • onNext → request → onNext → … → StackOverflow • No exceptions can be raised except NPE for null input (1.9 2.13) • Publisher calls onError(Throwable) • Subscriber cancels subscription Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 35


Слайд 35

What does request(n) mean? • N represents an element count • relative (byte chunks) more so than absolute (# of bytes) • Boundaries • Must be > 0 • Long.MAX_VALUE means unbounded read • So does overflow of N Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 36


Слайд 36

Request Strategies • Request some receive all request more ... (“stop-and-wait”) • request signal equivalent to an ack • Pre-fill buffer of certain capacity (“pre-fetch”) • request again when buffer falls below some threshold • more in-flight data → more risk • Request using timer/scheduler facility (“poll”) • Adapt to network or processing latency (“dynamic”) • find and pick the best request size Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 37


Слайд 37

Demo: Publisher & Subscriber, Back-pressure, TCK verification Demo source, TCK test Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 38


Слайд 38

Async vs Sync • Sync is explicitly allowed -- 2.2 3.2 3.10 3.11 • If Publisher/Subscriber are both sync → open recursion • not always an issue e.g. single item Publisher • The picture is different in a processing chain (vs. single Publisher-Subscriber) • async hand-off is likely needed at some stage • but not at every stage • Async is not the goal non-blocking is the overall objective Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 39


Слайд 39

Async vs Sync (continued) • Implementations are free to choose how to do this • As long as they comply with all the rules • Generally speaking the burden is on the Publisher • make async or sync calls as necessary • prevent open recursion • sequential signals • Subscribers can largely ignore such concerns Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 40


Слайд 40

Reactive Streams → JDK 9 Flow.java • No single best fluent async/parallel API in Java [1] • CompletableFuture/CompletionStage • continuation-style programming on futures • java.util.stream • multi-stage/parallel, “pull”-style operations on collections • No "push"-style API for items as they become available from active source Java “concurrency-interest” list: [1] initial announcement [2] why in the JDK? [3] recent update Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 41


Слайд 41

JDK 9 java.util.concurrent • java.util.concurrent.Flow • • Flow.Publisher Flow.Subscriber Flow.Subscription ... Flow.consume(...) and Flow.stream(...) • Tie-ins to CompletableFuture and java.util.Stream • SubmissionPublisher • turn any kind of source to Publisher • Executor-based delivery to subscribers w/ bounded (ring) buffer • submit and offer strategies (block or drop) to publish Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 42


Слайд 42

API For Async Composition Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 43


Слайд 43

Application Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 44


Слайд 44

Application Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Streams HTTP Data Broker 45


Слайд 45

Application ? Reactive Streams HTTP Data Broker API to compose asynchronous programs? Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 46


Слайд 46

Working with Streams • Non-blocking services return Publisher<T> instead of <T> • How do you attach further processing? • Reactive Streams is a callback-based API • becomes very nested quickly • Need something more declarative • it’s beyond the scope of Reactive Streams Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 47


Слайд 47

Stream Operations • Publisher represents a stream of data • It’s natural to apply operations functional-style • like the Java 8 Stream • Need API for composing async logic • rise above callbacks Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 48


Слайд 48

Reactor Stream • Project Reactor provides a Stream API • Reactive Streams Publisher + composable operations Streams.just('a' 'b' 'c') .take(2) .map(Character::toUpperCase) .consume(System.out::println); Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 49


Слайд 49

Demo: Reactor Stream, Back-Pressure Demo source Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 50


Слайд 50

Reactive Extensions (Rx) • Stream composition API based on Observer pattern • Originated at Microsoft, work by Erik Meijer • Implemented for different languages -- RxJava, RxJS, Rx.NET, … Observable.just('a', 'b', 'c') .take(2) .map(Character::toUpperCase) .subscribe(System.out::println); Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 51


Слайд 51

RxJava and Reactive Streams • RxJava 1.x predates Reactive Streams and doesn’t implement it directly • Very similar concepts, different names • Observable-Observer vs Publisher-Subscriber • RxJava supports “reactive pull” back-pressure • RxJava 1.x - Reactive Streams bridge Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 52


Слайд 52

Rx vs Java 8 Streams • Java 8 Streams best suited for collections • Observer and Iterator actually very closely related • same except push vs pull • Observable however can represent any source including collections • RxJava 2 is planned to support Java 8 Stream • Observable.from(Iterable) + implement java.util.Stream Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 53


Слайд 53

Demo: RxJava, Back-Pressure, Reactive Streams Bridge Demo class, TCK test Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 54


Слайд 54

RxJava vs Reactor • RxJava is a great choice for composition in applications • many operators, polyglot (client/server), well-documented • Reactive Streams is ideal for use in library APIs • remain agnostic to composition • Reactor is positioned as foundation for libraries • essential Reactive Streams infrastructure + core operators • also used in high volume applications Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 55


Слайд 55

Head Start with Stream Composition Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 56


Слайд 56

Operators: reactivex.io Alphabetical List Aggregate, All, Amb, and_, And, Any, apply, as_blocking, AsObservable, AssertEqual, asyncAction, asyncFunc, Average, averageDouble, averageFloat, averageInteger, averageLong, blocking, Buffer, bufferWithCount, bufferWithTime, bufferWithTimeOrCount, byLine, cache, case, Cast, Catch, catchException, collect, collect (RxScala version of Filter), CombineLatest, combineLatestWith, Concat, concat_all, concatMap, concatMapObserver, concatAll, concatWith, Connect, connect_forever, cons, Contains, controlled, Count, countLong, Create, cycle, Debounce, decode, DefaultIfEmpty, Defer, deferFuture, Delay, delaySubscription, delayWithSelector, Dematerialize, Distinct, DistinctUntilChanged, Do, doAction, doOnCompleted, doOnEach, doOnError, doOnRequest, doOnSubscribe, doOnTerminate, doOnUnsubscribe, doseq, doWhile, drop, dropRight, dropUntil, dropWhile, ElementAt, ElementAtOrDefault, Empty, empty?, encode, ensures, error, every, exclusive, exists, expand, failWith, Filter, filterNot, Finally, finallyAction, finallyDo, find, findIndex, First, FirstOrDefault, firstOrElse, FlatMap, flatMapFirst, flatMapIterable, flatMapIterableWith, flatMapLatest, flatMapObserver, flatMapWith, flatMapWithMaxConcurrent, flat_map_with_index, flatten, flattenDelayError, foldl, foldLeft, for, forall, ForEach, forEachFuture, forIn, forkJoin, From, fromAction, fromArray, FromAsyncPattern, fromCallable, fromCallback, FromEvent, FromEventPattern, fromFunc0, from_future, from_iterable, from_list, fromNodeCallback, fromPromise, fromRunnable, Generate, generateWithAbsoluteTime, generateWithRelativeTime, generator, GetEnumerator, getIterator, GroupBy, GroupByUntil, GroupJoin, head, headOption, headOrElse, if, ifThen, IgnoreElements, indexOf, interleave, interpose, Interval, into, isEmpty, items, Join, join (string), jortSort, jortSortUntil, Just, keep, keep-indexed, Last, lastOption, LastOrDefault, lastOrElse, Latest, latest (Rx.rb version of Switch), length, let, letBind, limit, LongCount, ManySelect, Map, map (RxClojure version of Zip), MapCat, mapCat (RxClojure version of Zip), map-indexed, map_with_index, Materialize, Max, MaxBy, Merge, mergeAll, merge_concurrent, mergeDelayError, mergeObservable, mergeWith, Min, MinBy, MostRecent, Multicast, nest, Never, Next, Next (BlockingObservable version), none, nonEmpty, nth, ObserveOn, ObserveOnDispatcher, observeSingleOn, of, of_array, ofArrayChanges, of_enumerable, of_enumerator, ofObjectChanges, OfType, ofWithScheduler, onBackpressureBlock, onBackpressureBuffer, onBackpressureDrop, OnErrorResumeNext, onErrorReturn, onExceptionResumeNext, orElse, pairs, pairwise, partition, partition-all, pausable, pausableBuffered, pluck, product, Publish, PublishLast, publish_synchronized, publishValue, raise_error, Range, Reduce, reductions, RefCount, Repeat, repeat_infinitely, repeatWhen, Replay, rescue_error, rest, Retry, retry_infinitely, retryWhen, Return, returnElement, returnValue, runAsync, Sample, Scan, scope, Select (alternate name of Map), select (alternate name of Filter), selectConcat, selectConcatObserver, SelectMany, selectManyObserver, select_switch, selectSwitch, selectSwitchFirst, selectWithMaxConcurrent, select_with_index, seq, SequenceEqual, sequence_eql?, SequenceEqualWith, Serialize, share, shareReplay, shareValue, Single, SingleOrDefault, singleOption, singleOrElse, size, Skip, SkipLast, skipLastWithTime, SkipUntil, skipUntilWithTime, SkipWhile, skip_while_with_index, skip_with_time, slice, sliding, slidingBuffer, some, sort, sort-by, sorted-list-by, split, split-with, Start, startAsync, startFuture, StartWith, stringConcat, stopAndWait, subscribe, SubscribeOn, SubscribeOnDispatcher, subscribeOnCompleted, subscribeOnError, subscribeOnNext, Sum, sumDouble, sumFloat, sumInteger, sumLong, Switch, switchCase, switchIfEmpty, switchLatest, switchMap, switchOnNext, Synchronize, Take, take_with_time, takeFirst, TakeLast, takeLastBuffer, takeLastBufferWithTime, takeLastWithTime, takeRight (see also: TakeLast), TakeUntil, takeUntilWithTime, TakeWhile, take_while_with_index, tail, tap, tapOnCompleted, tapOnError, tapOnNext, Then, thenDo, Throttle, throttleFirst, throttleLast, throttleWithSelector, throttleWithTimeout, Throw, throwError, throwException, TimeInterval, Timeout, timeoutWithSelector, Timer, Timestamp, To, to_a, ToArray, ToAsync, toBlocking, toBuffer, to_dict, ToDictionary, ToEnumerable, ToEvent, ToEventPattern, ToFuture, to_h, toIndexedSeq, toIterable, toIterator, ToList, ToLookup, toMap, toMultiMap, ToObservable, toSet, toSortedList, toStream, ToTask, toTraversable, toVector, tumbling, tumblingBuffer, unsubscribeOn, Using, When, Where, while, whileDo, Window, windowWithCount, windowWithTime, windowWithTimeOrCount, windowed, withFilter, withLatestFrom, Zip, zipArray, zipWith, zipWithIndex Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 57


Слайд 57

Operators: rx.Observable all, amb, ambWith, asObservable, buffer, cache, cast, collect, combineLatest, compose, concat, concatMap, concatWith, contains, count, countLong, create, debounce, defaultIfEmpty, defer, delay, delaySubscription, dematerialize, distinct, distinctUntilChanged, doOnCompleted, doOnEach, doOnError, doOnNext, doOnRequest, doOnSubscribe, doOnTerminate, doOnUnsubscribe, elementAt, elementAtOrDefault, empty, error, exists, filter, finallyDo, first, firstOrDefault, flatMap, flatMapIterable, from, groupBy, groupJoin, ignoreElements, interval, isEmpty, join, just, last, lastOrDefault, lift, limit, map, materialize, merge, mergeDelayError, mergeWith, nest, never, observeOn, ofType, onBackpressureBlock, onBackpressureBuffer, onBackpressureDrop, onBackpressureLatest, onErrorResumeNext, onErrorReturn, onExceptionResumeNext, publish, range, reduce, repeat, repeatWhen, replay, retry, retryWhen, sample, scan, sequenceEqual, serialize, share, single, singleOrDefault, skip, skipLast, skipUntil, skipWhile, startWith, subscribeOn, switchIfEmpty, switchMap, switchOnNext, take, takeFirst, takeLast, takeLastBuffer, takeUntil, takeWhile, throttleFirst, throttleLast, throttleWithTimeout, timeInterval, timeout, timer, timestamp, toList, toMap, toMultimap, toSortedList, unsubscribeOn, using, window, withLatestFrom, zip, zipWith Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 58


Слайд 58

Operators: what can you do with sequences? • Originate -- fixed values, arrays, ranges, from scratch • Reduce -- filter, accumulate, aggregate, partition • Transform -- create new type of sequence • Combine -- concatenate, merge, pair • Control -- overflow, errors Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 59


Слайд 59

Compose Non-blocking Service Layer Blocking: List<String> findUsers(String skill); LinkedInProfile getConnections(String id); TwitterProfile getTwitterProfile(String id); FacebookProfile getFacebookProfile(String id); Non-Blocking: Observable<String> findUsers(String skill); Observable<LinkedInProfile> getConnections(String id); Observable<TwitterProfile> getTwitterProfile(String id); Observable<FacebookProfile> getFacebookProfile(String id); Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 60


Слайд 60

Demo: Get List of RxJava Operators Demo source Compose Non-Blocking Service Layer Demo source Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 61


Слайд 61

flatMap?! Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 62


Слайд 62

From map to flatMap • map is used to apply a function to each element • Function<T, R> • The function could return a new sequence • Function<T, Observable<R>> • In which case we go from one to many sequences • flatMap merges (flattens) those back into one • i.e. map + merge Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 63


Слайд 63

Learn Your Operators • flatMap is essential composing non-blocking services • scatter-gather, microservices • Along with concatMap, merge, zip , and others • Learn and experiment with operators • Use decision tree for choosing operators on reactivex.io to learn Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 64


Слайд 64

Stream Types • “Cold” source -- passive, subscriber can dictate rate • e.g. file, database cursor • usually “replayed from the top” per subscriber • “Hot” source -- active, produce irrespective of subscribers • e.g. mouse events, timer events • cannot repeat • Not always a clear distinction • operators further change stream behavior • e.g. merge hot + cold Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 65


Слайд 65

Stream Types & Back-Pressure • Cold sources are well suited for reactive back-pressure • i.e. support request(n) • Hot sources cannot pause, need alternative flow control • buffer, sample or drop • Several operators exist • onBackPressureBuffer, onBackPressureDrop • buffer, window • etc. Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 66


Слайд 66

More on Stream Composition Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 67


Слайд 67

How Operators Work • Each operator is a deferred declaration of work to be done observable.map(...).filter(...).take(5)... • No work is done until there is a subscriber observable.map(...).filter(...).take(5). subscribe(...) • Underlying this is the lift operator Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 68


Слайд 68

How the lift Operator Works • Accepts function for decorating Subscriber • lift(Function<Subscriber<DOWN>, Subscriber<UP>>) • Returns new Observable that when subscribed to will use the function to decorate the Subscriber • Effectively each operator decorates the target Subscriber • at the time of subscribing Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 69


Слайд 69

The Subscribe • For every subscriber the lift chain is used (subscriber decorated) • Data starts flowing to the subscriber • Every subscriber is decorated independently • Results in separate data pipelines (N subscribers → N pipelines) Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 70


Слайд 70

Reasons For Multiple Subscribers • Different rates of consumption, slow/fast consumers • Different resource needs, i.e. IO vs CPU bound • Different tolerance to errors • Different views on dealing with overflow • Different code bases Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 71


Слайд 71

Shared Data Pipeline • It’s possible to have shared pipeline across subscribers • Insert shared publisher in the chain • vs lift which decorates every subscriber • Both RxJava and Reactor support this • observable.share() and stream.process(...) respectively • Reactor has processors for fan-out or point-to-point • pass all elements to all subscribers • distribute elements (“worker queue” pattern) Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 72


Слайд 72

Demo: Multiple Streams, Shared Streams (fan-out, point-to-point) Demo Source Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 73


Слайд 73

Concurrency in Rx • By default all work performed on subscriber’s thread • Operators generally not concurrent • some do accept rx.Scheduler (timer-related) • Two operators for explicit concurrency control • subscribeOn(Scheduler), observeOn(Scheduler) • Schedulers class with factory methods for Scheduler instances • io(), computation(), trampoline(), … Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 74


Слайд 74

observeOn Operator • Invoke rest of the chain of operators on specified Scheduler • Inserts async boundary for downstream processing • Uses a queue to buffer elements • Temporarily mitigate different upstream/downstream rates Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 75


Слайд 75

subscribeOn operator • Put source Observable on specified Scheduler • both initial subscribe + subsequent requests • Order in chain of operators not significant • Use for Observable that performs blocking I/O • e.g. observable.subscribeOn(Schedulers.io()) Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 76


Слайд 76

Concurrency in Reactor • Reactive Streams Processor for explicit concurrency control • comparable to Rx observeOn Streams.period(1) .process(RingBufferProcessor.create()) .consume(...); • Processor can also insert async boundary for upstream signals • comparable to Rx subscribeOn • Internally some operators may use Timer thread • the main reason for Rx operators accepting Scheduler Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 77


Слайд 77

Error Handling • try-catch-finally is of limited value in non-blocking app • Exceptions may occur on different thread(s) • Notifications are the only path to consistent error handling • Reactive Streams forbids propagating exceptions through the call-stack • Subscriber receives onError Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 78


Слайд 78

Try but won’t catch try { Streams.just(1, 2, 3, 4, 5) .map(i -> { throw new NullPointerException(); }) .consume( element -> { … }, error -> logger.error("Oooh error!", error) ); } catch (Throwable ex) { logger.error("Crickets..."); } Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 79


Слайд 79

Recovering from Errors • There are operators to handle error notifications • Swallow error + emit backup sequence (or single value) • onErrorResumeNext, onErrorReturn • Re-subscribe, it might work next time • retry, retryWhen • Lifecycle related • doOnTerminate/finallyDo… before/after error-complete Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 80


Слайд 80

Learn More. Stay Connected. @springcentral Spring.io/video Follow-up talk on Thursday (10:30am) “Reactive Web Applications” Unless otherwise indicated these slides are © 2013-2014 Pivotal Software Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 81


Слайд 81


×

HTML:





Ссылка: