際際滷

際際滷Share a Scribd company logo
Reactive Streams
仍亠从亠亶 仂仄舒仆从
2
弍仂 仄仆亠
10k msg/s
4
亅于仂仍ム亳 backend仂于
DBApp
5
亅于仂仍ム亳 backend仂于
DB
API
Storage
Cache
App
App
仂从仂仗仂亳亰于仂亟亳亠仍仆亶
backend
60仄
 仗舒亳仆亞 亰舒仗仂舒 - 1仄
 仗仂于亠从舒 于 从亠亠 - 2仄
 亰舒仗仂 于  - 30仄
 亰舒仗仂 于  - 25仄
 仂仄亳仂于舒仆亳亠 仂于亠舒 - 2仄
8
60仄
9
 亟亠仍仆亶 仗仂仂从
 95% 仂亢亳亟舒亠
 舒仆亳仄舒亠 亠
10
弌亳仆仂仆仆舒 仄仂亟亠仍
11
亢亳亟舒仆亳亠
 弍舒弍仂从舒 于 舒亰仆 仗仂仂从舒
 仂仂从亳 仆亠 仗仂舒亳于舒ム
 亅亠从亳于仆仂亠 亳仗仂仍亰仂于舒仆亳亠 亠仂于
12
亳仆仂仆仆舒 仄仂亟亠仍
 
 仆亠仆亳亠 API
 弌亠
 亰舒亳仄仂亟亠亶于亳亠  亟亞亳仄亳 仗仂仂从舒仄
13
亳仆仂仆仆亠 亞舒仆亳 于亠亰亟亠
 仂仄仗仂亰亳亳
 亠于仍亠仆亳亠
 弍舒弍仂从舒 仂亳弍仂从
 Backpressure
14
亳仆仂仆仆仂 仂 仍仂亢仆仂
15
亳仆仂仆仆仂 仂 仍仂亢仆仂
16
亳仆仂仆仆仂 仂 仍仂亢仆仂
17
亳仆仂仆仆仂 仂 仍仂亢仆仂
 Mutex, semaphore, etc
 Green threads
 Future-Promise
 仂仂从亳 亟舒仆仆
18
仂亟亠仍亳
仂仂从亳
20
 仂亟仆 亠从 仆亠仍亰 于仂亶亳
亟于舒亢亟
 仆仂亢亠于仂 仂仂弍亠仆亳亶 仂亟仆仂亞仂 亳仗舒
 舒于亳亳 仂 于亠仄亠仆亳 仆舒舒仍舒 仆舒弍仍ミ莞黍出狐
 仂亢亠 仆亠 亳仄亠 仆亳 仆舒舒仍舒 仆亳 从仂仆舒
22
仂仂从亳 亟舒仆仆
 curl twitter.com | grep CodeFest | wc -n
 亠亠于亠 仂亠亟亳仆亠仆亳
 亰于从 亳 于亳亟亠仂
 亰舒仗仂 亳 仂于亠 亠于亠舒
23
仂仂从亳 于仂从亞 仆舒
24
Backend
舒仗仂 于亠
亊舒仗仂舒仗仂 于亠于亠
25
26
Backend
舒亳仆亞  API 于亠
舒亳仆亞  API 于亠
27
Backend
舒亳仆亞  API 于亠
舒亳仆亞  API 于亠
28
仂仂从亳 亟舒仆仆
舒亳仆亞  API 于亠
 仂亠 舒亳仆仂仆仆亠 亞舒仆亳
 舒亳仆舒 于 亠仍仂仄
 舒舒仍仍亠仍仆仂亠 仗仂亞舒仄仄亳仂于舒仆亳亠
 亳弍从亳 亳 亰舒于亠亠仆亳亠
29
亠亳仄亠于舒
Backpressure
31
弌仂弍亳亶 仍亳从仂仄 仄仆仂亞仂
仗舒于亳亠仍 仂仍舒亠仍
32
弌仂弍亳亶 仍亳从仂仄 仄仆仂亞仂
仗舒于亳亠仍 仂仍舒亠仍
33
弌仂弍亳亶 仍亳从仂仄 仄仆仂亞仂
仗舒于亳亠仍 仂仍舒亠仍
34
弌仂弍亳亶 仍亳从仂仄 仄仆仂亞仂
仗舒于亳亠仍 仂仍舒亠仍
35
弌仂弍亳亶 仍亳从仂仄 仄仆仂亞仂
仗舒于亳亠仍 仂仍舒亠仍
36
弌仂弍亳亶 仍亳从仂仄 仄仆仂亞仂
仗舒于亳亠仍 仂仍舒亠仍
37
弌仂弍亳亶 仍亳从仂仄 仄仆仂亞仂
仗舒于亳亠仍 仂仍舒亠仍
38
弌仂弍亳亶 仍亳从仂仄 仄仆仂亞仂
仗舒于亳亠仍 仂仍舒亠仍
39
仍仂从亳ム亳亶 于亰仂于
仗舒于亳亠仍 仂仍舒亠仍
40
Pull
仗舒于亳亠仍 仂仍舒亠仍
41
Negative Acknowledge
仗舒于亳亠仍 仂仍舒亠仍
42
Negative Acknowledge
仗舒于亳亠仍 仂仍舒亠仍
43
Dynamic pull-push
仗舒于亳亠仍 仂仍舒亠仍
4
44
Dynamic pull-push
4
仗舒于亳亠仍 仂仍舒亠仍
45
Dynamic pull-push
3
仗舒于亳亠仍 仂仍舒亠仍
46
Dynamic pull-push
3
仗舒于亳亠仍 仂仍舒亠仍
47
Dynamic pull-push
0
仗舒于亳亠仍 仂仍舒亠仍
48
Dynamic pull-push
0
仗舒于亳亠仍 仂仍舒亠仍
49
Dynamic pull-push
0
仗舒于亳亠仍 仂仍舒亠仍
2
50
Dynamic pull-push
2
仗舒于亳亠仍 仂仍舒亠仍
51
Dynamic pull-push
0
仗舒于亳亠仍 仂仍舒亠仍
于亠舒亳仆亞
52
亠 于 弍仂亠
Reactive Streams
 弌仗亠亳亳从舒亳
 亳仆仂仆仆仂亞仂 于亰舒亳仄仂亟亠亶于亳
 丕仗仂磲仂亠仆仆亠 仂仂弍亠仆亳
 弍舒仆舒 于磶
54
Reactive Streams
 RxJava
 Akka Stream
 Reactor
 Ratpack
55
亠舒仍亳亰舒亳亳
Talk is cheap,
show me the code
 Reactive Stream
 Dynamic pull-push model
 Scala, Java
 弌舒亳亠从舒 亳仗亳亰舒亳
 仂亟亠仍 舒从仂仂于
57
Akka Stream
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>you specify which {@link Scheduler} this operator will use</dd>
* </dl>
*
* @param notificationHandler
* receives an Observable of notifications with which a user can complete or error, aborting the repeat.
* @param scheduler
* the {@link Scheduler} to emit the items on
* @return the source Observable modified with repeat logic
* @see <a href="http://reactivex.io/documentation/operators/repeat.html">ReactiveX operators documentation: Repeat</a>
*/
public final Observable<T> repeatWhen(final Func1<? super Observable<? extends Void>, ? extends Observable<?>> notificationHandler, Scheduler scheduler) {
Func1<? super Observable<? extends Notification<?>>, ? extends Observable<?>> dematerializedNotificationHandler = new Func1<Observable<? extends Notification<?>>, Observable<?>>()
@Override
public Observable<?> call(Observable<? extends Notification<?>> notifications) {
return notificationHandler.call(notifications.map(new Func1<Notification<?>, Void>() {
@Override
public Void call(Notification<?> notification) {
return null;
}
}));
}
};
return OnSubscribeRedo.repeat(this, dematerializedNotificationHandler, scheduler);
}

/**
* Returns an Observable that emits the same values as the source Observable with the exception of an
* {@code onCompleted}. An {@code onCompleted} notification from the source will result in the emission of
* a {@code void} item to the Observable provided as an argument to the {@code notificationHandler}
* function. If that Observable calls {@code onComplete} or {@code onError} then {@code repeatWhen} will
* call {@code onCompleted} or {@code onError} on the child subscription. Otherwise, this Observable will
* resubscribe to the source observable.
* <p>
* <img width="640" height="430" src=/slideshow/codefest2015-reactive-streams/46430538/"https:/raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/repeatWhen.f.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code repeatWhen} operates by default on the {@code trampoline} {@link Scheduler}.</dd>
* </dl>
*
* @param notificationHandler
* receives an Observable of notifications with which a user can complete or error, aborting the repeat.
* @return the source Observable modified with repeat logic
* @see <a href="http://reactivex.io/documentation/operators/repeat.html">ReactiveX operators documentation: Repeat</a>
*/
public final Observable<T> repeatWhen(final Func1<? super Observable<? extends Void>, ? extends Observable<?>> notificationHandler) {
Func1<? super Observable<? extends Notification<?>>, ? extends Observable<?>> dematerializedNotificationHandler = new Func1<Observable<? extends Notification<?>>, Observable<?>>()
@Override
public Observable<?> call(Observable<? extends Notification<?>> notifications) {
return notificationHandler.call(notifications.map(new Func1<Notification<?>, Void>() {
@Override
public Void call(Notification<?> notification) {
return null;
}
}));
}
};
return OnSubscribeRedo.repeat(this, dematerializedNotificationHandler);
}
/**
* An Observable that never sends any information to an {@link Observer}.
* This Observable is useful primarily for testing purposes.
*
* @param <T>
* the type of item (not) emitted by the Observable
*/
private static class NeverObservable<T> extends Observable<T> {
public NeverObservable() {
super(new OnSubscribe<T>() {

@Override
public void call(Subscriber<? super T> observer) {
// do nothing
}

});
}
}

/**
* An Observable that invokes {@link Observer#onError onError} when the {@link Observer} subscribes to it.
*
* @param <T>
* the type of item (ostensibly) emitted by the Observable
*/
private static class ThrowObservable<T> extends Observable<T> {

public ThrowObservable(final Throwable exception) {
super(new OnSubscribe<T>() {

/**
* Accepts an {@link Observer} and calls its {@link Observer#onError onError} method.
*
* @param observer
* an {@link Observer} of this Observable
*/
@Override
/**
* Operator function for lifting into an Observable.
*/
public interface Operator<R, T> extends Func1<Subscriber<? super R>, Subscriber<? super T>> {
// cover for generics insanity
}
/**
* Lifts a function to the current Observable and returns a new Observable that when subscribed to will pass
* the values of the current Observable through the Operator function.
* <p>
* In other words, this allows chaining Observers together on an Observable for acting on the values within
* the Observable.
* <p> {@code
* observable.map(...).filter(...).take(5).lift(new OperatorA()).lift(new OperatorB(...)).subscribe()
* }
* <p>
* If the operator you are creating is designed to act on the individual items emitted by a source
* Observable, use {@code lift}. If your operator is designed to transform the source Observable as a whole
* (for instance, by applying a particular set of existing RxJava operators to it) use {@link #compose}.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code lift} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param lift the Operator that implements the Observable-operating function to be applied to the source
* Observable
* @return an Observable that is the result of applying the lifted Operator to the source Observable
* @see <a href="https://github.com/ReactiveX/RxJava/wiki/Implementing-Your-Own-Operators">RxJava wiki: Implementing Your Own Operators</a>
*/
public final <R> Observable<R> lift(final Operator<? extends R, ? super T> lift) {
return new Observable<R>(new OnSubscribe<R>() {
@Override
public void call(Subscriber<? super R> o) {
try {
Subscriber<? super T> st = hook.onLift(lift).call(o);
try {
// new Subscriber created and being subscribed with so 'onStart' it
st.onStart();
onSubscribe.call(st);
} catch (Throwable e) {
// localized capture of errors rather than it skipping all operators 
// and ending up in the try/catch of the subscribe method which then
// prevents onErrorResumeNext and other similar approaches to error handling
if (e instanceof OnErrorNotImplementedException) {
throw (OnErrorNotImplementedException) e;
}
st.onError(e);
}
} catch (Throwable e) {
if (e instanceof OnErrorNotImplementedException) {
throw (OnErrorNotImplementedException) e;
}
// if the lift function failed all we can do is pass the error to the final Subscriber
// as we don't have the operator available to us
o.onError(e);
}
}
});
}
/**
* Transform an Observable by applying a particular Transformer function to it.
* <p>
* This method operates on the Observable itself whereas {@link #lift} operates on the Observable's
* Subscribers or Observers.
* <p>
* If the operator you are creating is designed to act on the individual items emitted by a source
* Observable, use {@link #lift}. If your operator is designed to transform the source Observable as a whole
* (for instance, by applying a particular set of existing RxJava operators to it) use {@code compose}.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code compose} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param transformer implements the function that transforms the source Observable
* @return the source Observable, transformed by the transformer function
* @see <a href="https://github.com/ReactiveX/RxJava/wiki/Implementing-Your-Own-Operators">RxJava wiki: Implementing Your Own Operators</a>
*/
@SuppressWarnings("unchecked")
public <R> Observable<R> compose(Transformer<? super T, ? extends R> transformer) {
return ((Transformer<T, R>) transformer).call(this);
}
/**
* Transformer function used by {@link #compose}.
* @warn more complete description needed
*/
public static interface Transformer<T, R> extends Func1<Observable<T>, Observable<R>> {
// cover for generics insanity
}


/* 
* Operators Below Here
*
59
仍ム亠于亠 舒弍舒从亳亳
Source SinkFlow
60
仍ム亠于亠 舒弍舒从亳亳
Source SinkFlow
61
Source
val iterableSource = Source(1 to 50)
val tickSource = Source(1 second, 1 second, "Tick")
val singleSource = Source.single("CodeFest")
val emptySource = Source.empty()
val zmqSource = ???
62
Sink
val blackhole = Sink.ignore
val onComplete = Sink.onComplete { result =>
System.exit(0)
}
val foreach = Sink.foreach(println)
val firstElement = Sink.head[Int]
63
Flow
implicit val as = ActorSystem("CodeFest")
implicit val materializer = ActorFlowMaterializer()

val source = Source(1 to 50)
val sink = Sink.foreach[Int](println)
val flow = source.to(sink)
flow.run()
64
Flow
val flow2 = source
.map { x => x * 2 }
.filter { x => x % 3 == 0 }
.to(sink)
flow2.run()
65
Flow
val source = Source(1 to 50)
val sink = Sink.foreach[String](println)

val flow2 = source
.map { x => x.toString }
.map { x => x / 13 }
.to(sink)
flow2.run()
66
Flow
舒亳仆亞  API 于亠
67
Flow
val request: Source[Request] = ???
def parser: Request => Query = ???
def dbCall: Query => Future[List[Int]] = ???
def apiCall: List[Int] => Future[List[String]] = ???
def buildResponse: List[String] => Response = ???
val flow3 = request
.map(parser)
.mapAsync(dbCall)
.mapAsync(apiCall)
.map(buildResponse)
.to(response)
 MQ
 仂仂从亳 亟舒仆仆 (仂弍亳, 仄亠亳从亳, 舒亶仍, 于亳亟亠仂)
 UI
 亠亠亟亳 亰舒亟舒
68
亳仄亠仆亠仆亳
69
Adopters
 C#
 Java, Scala
 JavaScript
 Objective-C
 Python
 Ruby
 PHP
70
亊亰从亳 仗仂亞舒仄仄亳仂于舒仆亳
亳 亳亰仄亠仆亳仍
仂仗仂?
al.romanchuk@2gis.ru @1esha
 Reactive Streams
 Akka Stream
 Reactor
 Ratpack
 RxJava
 Reactive Manifesto
73
弌仍从亳
 Akka HTTP
 RxMongo
 https://github.com/pkinsky/akka-streams-example
74
弌仍从亳
 Reactive Extensions
 Reactive Extensions for JavaScript
 Reactive Cocoa
 Rx.py
 Rx.rb
 Rx.php
75
弌仍从亳
 drop, take
 group, mapConcat
 grouped, flatten
 buffer, conflate, expand
76
Flow
 Balance, Broadcast, Merge
 Zip, Unzip
 FlexiMerge, FlexiRoute
77
Graph

More Related Content

Codefest-2015 Reactive Streams