reactive streams. slava schmidt
TRANSCRIPT
![Page 1: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/1.jpg)
Reactive
Streams
![Page 2: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/2.jpg)
THENJava DeveloperLATEREnterprise ArchitectNOWScala Consultant
TWITTER@[email protected]
Reactive
Streams
![Page 3: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/3.jpg)
Stream
![Page 4: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/4.jpg)
A stream can be defined as a sequence of
data.
A powerful concept that greatly simplifies I/
O operations.
Java 5/6/7
![Page 5: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/5.jpg)
A sequence of elements
supporting sequential and
parallel aggregate operations.
Java 8
![Page 6: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/6.jpg)
•sequence of data or instructions•hot or cold•bounded or unbounded•focus on data transfer and/or transformation
![Page 7: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/7.jpg)
Uses
![Page 8: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/8.jpg)
•bulk data transfer•batch processing of large data sets•micro-batching•real-time data sources•embedded data-processing•monitoring and analytics•metrics, statistics composition •event processing•error handling
![Page 9: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/9.jpg)
![Page 10: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/10.jpg)
![Page 11: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/11.jpg)
MEETALICE AND BORIS
![Page 12: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/12.jpg)
MEETALICE AND BORIS
![Page 13: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/13.jpg)
Java 6
![Page 14: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/14.jpg)
Java 7
![Page 15: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/15.jpg)
![Page 16: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/16.jpg)
![Page 17: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/17.jpg)
![Page 18: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/18.jpg)
Java 8
![Page 19: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/19.jpg)
NAK (NACK)
![Page 20: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/20.jpg)
Backpressure
![Page 21: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/21.jpg)
Reactive Stream
![Page 22: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/22.jpg)
MEETALICE AND BORIS
•Typical threads
•Do some work
•Exchange data
![Page 23: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/23.jpg)
Direct Callsprivate static final SThread alice = new SThread("RS-Alice") { @Override public void swap(int count) { super.swap(count); borice.swap(count); } };
private static final SThread borice = new SThread("RS-Borice") { @Override public void run() { while (!stopped()) { SwapEnvironment.work(); } } @Override public void swap(int count) { super.swap(count); } };
![Page 24: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/24.jpg)
Java IO
“Let’s move some data”
![Page 25: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/25.jpg)
Java IO
A PipedOutputStream
PipedInputStreamprotected byte buffer[];
B
![Page 26: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/26.jpg)
Java IOA PipedOutputStream BPipedInputStream
protected byte buffer[];
![Page 27: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/27.jpg)
Java IOA PipedOutputStream BPipedInputStream
Write
Block
Transfer
Block
Read
Block
![Page 28: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/28.jpg)
Java IOval alice = new SThread("RS-Alice") { override def swap(n: Int) { super.swap(n) for (i <- 0 to n * rateA) out.write(Env.BEER) }}
val borice = new SThread("RS-Borice") { override def swap(n: Int) { super.swap(n) for (i <- 0 to n * rateB) in.read() }}
val out = new PipedOutputStream()val in = new PipedInputStream(out, size)
![Page 29: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/29.jpg)
Java IOval alice = new SThread("RS-Alice") { override def swap(n: Int) { super.swap(n) for (i <- 0 to n * rateA) out.write(Env.BEER) }}
val borice = new SThread("RS-Borice") { override def swap(n: Int) { super.swap(n) for (i <- 0 to n * rateB) in.read() }}
val out = new PipedOutputStream()val in = new PipedInputStream(out, size)
INCREASE
![Page 30: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/30.jpg)
Java IOval alice = new SThread("RS-Alice") { override def swap(n: Int) { super.swap(n) for (i <- 0 to n * rateA) out.write(Env.BEER) }}
val borice = new SThread("RS-Borice") { override def swap(n: Int) { super.swap(n) for (i <- 0 to n * rateB) in.read() }}
val out = new PipedOutputStream()val in = new PipedInputStream(out, size)
INCREASE
![Page 31: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/31.jpg)
Java IOval alice = new SThread("RS-Alice") { override def swap(n: Int) { super.swap(n) for (i <- 0 to n * rateA) out.write(Env.BEER) }}
val borice = new SThread("RS-Borice") { override def swap(n: Int) { super.swap(n) for (i <- 0 to n * rateB) in.read() }}
val out = new PipedOutputStream()val in = new PipedInputStream(out, size)
INCREASE
![Page 32: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/32.jpg)
Blocking is only a matter of time :(
Java IO
![Page 33: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/33.jpg)
Java NIO“There is a better way”
![Page 34: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/34.jpg)
Java NIOA SinkChannel BSourceChannel
Write
Block
Transfer
BufferBuffer
Read
Block
Read
Fill
![Page 35: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/35.jpg)
Java NIOval alice = new SThread("RS-Alice") { override def swap(n: Int) { val cnt = n * rateA val buffer = ByteBuffer.allocate(cnt) buffer.put(Vector.fill(cnt)(BEER).toArray) buffer.flip() val written = sinkChannel.write(buffer) super.swap(written) }}
val borice = new SThread("RS-Borice") { override def swap(n: Int) { val buffer = ByteBuffer.allocate(n * rateB) val cnt = sourceChannel.read(buffer) super.swap(cnt) }}
val pipe = Pipe.open()val sinkChannel = pipe.sinkval sourceChannel = pipe.sourcesourceChannel.configureBlocking(true) sinkChannel.configureBlocking(false)
BLOCKING
![Page 36: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/36.jpg)
Java NIOval alice = new SThread("RS-Alice") { override def swap(n: Int) { val cnt = n * rateA val buffer = ByteBuffer.allocate(cnt) buffer.put(Vector.fill(cnt)(BEER).toArray) buffer.flip() val written = sinkChannel.write(buffer) super.swap(written) }}
val borice = new SThread("RS-Borice") { override def swap(n: Int) { val buffer = ByteBuffer.allocate(n * rateB) val cnt = sourceChannel.read(buffer) super.swap(cnt) }}
val pipe = Pipe.open()val sinkChannel = pipe.sinkval sourceChannel = pipe.sourcesourceChannel.configureBlocking(false) sinkChannel.configureBlocking(false)
NON-BLOCKING
![Page 37: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/37.jpg)
“Choose how to fail”
Java NIO
![Page 38: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/38.jpg)
IO & NIO
![Page 39: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/39.jpg)
IO & NIO
Blocking
Dropping
or
Unbounded
or
Non-Determinism
OutOfMemory
Scalability
![Page 40: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/40.jpg)
Solution
Backpressure
![Page 41: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/41.jpg)
Solution
Backpressure
Dynamic Push/Pull
Fast Boris
![Page 42: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/42.jpg)
Solution
Backpressure
Dynamic Push/Pull
Fast Alice
![Page 43: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/43.jpg)
BackpressureA BStream
Data
Demand
Data
Demand
![Page 44: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/44.jpg)
Java 8
![Page 45: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/45.jpg)
Java 8public class ConsumerBorice extends SThread implements Consumer<byte[]> { public ConsumerBorice(String name) { super(name); } @Override public Consumer andThen(Consumer after) { return after; } @Override public void accept(byte[] bytes) { super.swap(bytes.length); } }
![Page 46: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/46.jpg)
Java 8private static final ConsumerBorice borice =
new ConsumerBorice("RS-Borice") { public void swap(int count) { }};
private static final SThread alice = new SThread ("RS-Alice") { byte[] items(int count) { byte[] result = new byte[count]; Arrays.fill(result, Env.BEER()); return result; } public void swap(int count) { Stream.of(items(count)).parallel() .filter(s -> s.equals(Env.BEER())) .forEach(borice); super.swap(count); } };
![Page 47: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/47.jpg)
Java 8private static final ConsumerBorice borice =
new ConsumerBorice("RS-Borice") { public void swap(int count) { }};
private static final SThread alice = new SThread ("RS-Alice") { byte[] items(int count) { byte[] result = new byte[count]; Arrays.fill(result, Env.BEER()); return result; } public void swap(int count) { Stream.of(items(count)).parallel() .filter(s -> s.equals(Env.BEER())) .forEach(borice); super.swap(count); } }; transformation
static push (or pull)
or sequential
![Page 48: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/48.jpg)
Java 8private static final ConsumerBorice borice =
new ConsumerBorice("RS-Borice") { public void swap(int count) { }};
private static final SThread alice = new SThread ("RS-Alice") { byte[] items(int count) { byte[] result = new byte[count]; Arrays.fill(result, Env.BEER()); return result; } public void swap(int count) { Stream.of(items(count)).parallel() .filter(s -> s.equals(Env.BEER())) .forEach(borice); super.swap(count); } }; Synchronous
Non-Deterministic
Limited Scalability
Terminates on error
![Page 49: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/49.jpg)
Problems
Synchronous
Non-Deterministic
Limited Scalability
Terminates on error
![Page 50: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/50.jpg)
Problems
Synchronous
Non-Deterministic
Limited Scalability Terminates on error
![Page 51: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/51.jpg)
Message Driven
Responsive
Elastic Resilient
![Page 52: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/52.jpg)
Reactive
Message Driven
Responsive
Elastic Resilient
http://www.reactivemanifesto.org
![Page 53: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/53.jpg)
Reactive Streams
… a standard for asynchronous stream processing with non-
blocking backpressure.
http://www.reactive-streams.org
![Page 54: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/54.jpg)
ParticipantsNetflix rxJava, rxScala, …
Oracle
Pivotal Spring Reactor
RedHat Vert.x
Typesafe Akka Streams
Ratpack
![Page 55: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/55.jpg)
Reactive Streams
• Semantics (Specification)
• API (Application Programming Interface)
• TCK (Technology Compatibility Kit)
![Page 56: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/56.jpg)
APIpublic interface Publisher<T> { void subscribe(Subscriber<? super T> var1); }
public interface Subscriber<T> { void onSubscribe(Subscription var1); void onNext(T var1); void onError(Throwable var1); void onComplete(); }
public interface Subscription { void request(long var1); void cancel(); }
![Page 57: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/57.jpg)
Subscription
Subscription
subscribe
onSubscribe
Producer Subscriber
![Page 58: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/58.jpg)
Streaming
request
Subscription
onNext
onComplete
onError
cancel
Subscriber
![Page 59: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/59.jpg)
trait BytePublisher extends Publisher[Byte] { var subscriber: Subscriber[_ >: Byte] = _ override def subscribe(subscriber: Subscriber[_ >: Byte]) { this.subscriber = subscriber }}
val alice = new SThread("RS-Alice") with BytePublisher {
override def swap(n: Int) { for { i <- 1 to n } subscriber.onNext(Env.BEER) super.swap(n) }
}
Publisher
![Page 60: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/60.jpg)
trait ByteSubscriber[T >: Byte] extends Subscriber[T] { var subscription: Subscription = _ override def onSubscribe(subscription: Subscription) { this.subscription = subscription } override def onError(t: Throwable) { } override def onComplete() { }}
val borice = new SThread(“RS-Borice") with ByteSubscriber[Byte]{
def onNext(t: Byte) { super.swap(1) }}
Subscriber
![Page 61: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/61.jpg)
val borice = new SThread(“RS-Borice") with ByteSubscriber[Byte]{ alice.subscribe(this) override def swap(n: Int) { subscription.request(n) } def onNext(t: Byte) { super.swap(1) }}
Right Subscriber
![Page 62: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/62.jpg)
val alice = new SThread("RS-Alice") with BytePublisher { override def swap(n: Int) { val cnt = math.min(n, counter.get()) counter.addAndGet(-cnt) for { i <- 1 to cnt } subscriber.onNext(Env.BEER) super.swap(n) }}
Right Publishertrait BytePublisher extends Publisher[Byte] { var subscriber: Subscriber[_ >: Byte] = _ var subscription: Subscription = _ val counter = new AtomicInteger(0) def request(l: Long): Unit = { counter.addAndGet(l.toInt) } override def subscribe(subscriber: Subscriber[_ >: Byte]) { this.subscription = new ByteSub(this) this.subscriber = subscriber subscriber.onSubscribe(this.subscription) }}
![Page 63: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/63.jpg)
implicit val mat = FlowMaterializer()(system)Source(alice).runWith(Sink(borice))
Akka Streams
![Page 64: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/64.jpg)
Streams.create(alice).subscribe(borice)
Reactor Stream
![Page 65: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/65.jpg)
ratpack.stream.Streams.buffer(alice).subscribe(borice)
Ratpack.io
![Page 66: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/66.jpg)
import rx.RxReactiveStreams._ subscribe(toObservable(alice), borice)
RxReactiveStreams
![Page 67: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/67.jpg)
//vert.x 3.0 - only supports Streams[Buffer] val rws = ReactiveWriteStream.writeStream()rws.subscribe(borice)val rrs = ReactiveReadStream.readStream() alice.subscribe(rrs)val pump = Pump.pump(rrs, rws)pump.start()
Vert.X
![Page 68: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/68.jpg)
Why should I care?
![Page 69: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/69.jpg)
Because
•Simplifies reactive programming•Rises program’s abstraction level•May be future JDK standard
![Page 70: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/70.jpg)
Abstraction levels
![Page 71: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/71.jpg)
Abstraction levels
Runnable & Thread
![Page 72: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/72.jpg)
Abstraction levels
java.util.concurrent
![Page 73: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/73.jpg)
Abstraction levels
Promise & Future
![Page 74: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/74.jpg)
Abstraction levels
Actors
![Page 75: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/75.jpg)
Abstraction levels
Streams
![Page 76: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/76.jpg)
Abstraction levels
![Page 77: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/77.jpg)
Interoperability
![Page 78: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/78.jpg)
Shop Admin
Delivery
WebShop
Merchant
ADs Stats
![Page 79: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/79.jpg)
Shop Admin
Delivery
WebShop
Merchant
ADs Stats
DB
REST
File System
Messaging
REST
![Page 80: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/80.jpg)
Shop Admin(vert.x)
Delivery(Spring)
WebShop(Akka)
Merchant(ratpack.io)
ADs(Rx…)
Stats(Spray)
DB
REST
File System
Messaging
REST
![Page 81: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/81.jpg)
Shop Admin(vert.x)
Delivery(Spring)
WebShop(Akka)
Merchant(ratpack.io)
ADs(Rx…)
Stats(Spray)
DB
![Page 82: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/82.jpg)
Shop Admin(vert.x)
Data Flow
Backpressure
Delivery(Spring)
WebShop(Akka)
Merchant(ratpack.io)
ADs(Rx…)
Stats(Spray)
DB
![Page 83: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/83.jpg)
Example Akka
![Page 84: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/84.jpg)
Example Akka
![Page 85: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/85.jpg)
val display = { def ellipse (i: Input) = setColor(i).fillOval(i(3), i(4), i(5), i(6)) def rect (i: Input) = setColor(i).fillRect(i(3), i(4), i(5), i(6)) val logics: Seq[(Input) => Unit] = Seq(ellipse, rect) def rnd = Random.nextInt(255) val timer = Source(0.seconds, 1.second, () => rnd ) val randoms = Source { () => Some(rnd) } val functions = timer map { i => logics(i % logics.size) } val display = functions map { f => val groups = randoms.take(7) val params = groups.fold(List.empty[Int])((l, i) => i :: l) for { p <- params } f(p) } display} def start = { display.runWith(BlackholeSink)}
Example Akka
![Page 86: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/86.jpg)
Use Case
Price Correction System
![Page 87: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/87.jpg)
Product schedulesCompetitors
Volatile sourcesAudit
Historical trendsAlerts
Sales HeuristicsPricing Rules
Adjustments
![Page 88: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/88.jpg)
CompetitorsVolatile sources
Audit
Historical trendsAlerts
Sales HeuristicsPricing Rules
Adjustments
Timer Scheduler
![Page 89: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/89.jpg)
Volatile sourcesAudit
Historical trendsAlerts
Sales HeuristicsPricing Rules
Adjustments
Timer Scheduler Robots
![Page 90: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/90.jpg)
Audit
Historical trendsAlerts
Sales HeuristicsPricing Rules
Adjustments
Validators
Timer Scheduler Robots
![Page 91: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/91.jpg)
Historical trendsAlerts
Sales HeuristicsPricing Rules
Adjustments
ValidatorsLogger
Timer Scheduler Robots
![Page 92: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/92.jpg)
Historical trendsAlerts
Pricing RulesAdjustments
Stock ValidatorsLogger
Timer Scheduler Robots
![Page 93: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/93.jpg)
Historical trendsAlerts
Adjustments
Stock ValidatorsLogger
Timer Scheduler Robots
Pricing
![Page 94: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/94.jpg)
Historical trendsAlerts
Stock ValidatorsLogger
Timer Scheduler Robots
Pricing Ruler
![Page 95: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/95.jpg)
Historical trends
Stock ValidatorsLogger
Timer Scheduler Robots
Pricing Ruler Safety
![Page 96: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/96.jpg)
Stock ValidatorsLogger
Timer Scheduler Robots
Pricing Ruler Safety
Archive
![Page 97: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/97.jpg)
Stock
Validators
Logger
Timer Scheduler
Robots
Pricing
RulerSafety
Archive
Data Flow
Backpressure
![Page 98: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/98.jpg)
val schedules = Flow[Date] mapConcat generateSchedules val robots = Flow[Schedule] map scrape
val validations = Flow[ScrapeResult] map { site => logSite(site) validate(site)}
val pricing = Flow[Validation] map checkPrice
val stock = Flow[Validation] map checkStock
val ruler = Flow[(Price, Stock)] map applyRule
val safety = Flow[Rule] map checkSafety
val zip = Zip[Price, Stock] val split = Broadcast[Validation]
![Page 99: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/99.jpg)
Stock
Validators
Logger
Timer Scheduler
Robots
Pricing
RulerSafety
Archive
Data Flow
Backpressure
![Page 100: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/100.jpg)
val timer = Source(0.seconds, 1.minute, () => now) val archive = ForeachSink[SafetyCheck] { logRule }
val graph = FlowGraph { implicit builder => timer ~> schedules ~> robots ~> validations ~> split split ~> stock ~> zip.right split ~> pricing ~> zip.left ~> ruler ~> safety ~> archive} graph.run()
![Page 101: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/101.jpg)
24 cores
48 Gb
24 cores
48 Gb150 000 000
positions daily
![Page 102: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/102.jpg)
Reactive Streams
![Page 103: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/103.jpg)
Reactive Streams
•Simplify reactive programming•Rise program’s abstraction level•May be future JDK standard
![Page 104: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/104.jpg)
Now I care!
![Page 105: Reactive streams. Slava Schmidt](https://reader034.vdocuments.net/reader034/viewer/2022042602/55ca377cbb61ebaa698b45a7/html5/thumbnails/105.jpg)
Thank you
https://github.com/slavaschmidt/reactive_streams_talk
@slavaschmidt