Share
Adventures with ReactiveStreams
In today's world, more and more people are creating software that is expected to run at large scale. Large scale and cost-effectiveness often lead programmers to asynchronous programming models for handling vertical scalability concerns. Creating an asynchronous application on the JVM is nontrivial and often leads to disparate abstractions. Reactive Streams is one such abstraction that is widely accepted on the JVM today. This presentation is a deep dive into what it takes to implement the Reactive Streams specifications. It delves into various trade-offs that library developers have to consider while implementing Reactive Streams and which aspects of practical applications make such trade-offs easier.
Presented at Oracle Code One 2018:
https://oracle.rainfocus.com/widget/oracle/oow18/catalogcodeone18?search.codeonetracks=1522259325398009d52s&search=DEV5269
Nitesh Kant
Transcript
116 slides
-
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() HTTP/1.1 200 OK
Nitesh, Kant, SF Jo, Voordeckers, SF … -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() transformToJson() HTTP/1.1 200
OK Nitesh, Kant, SF Jo, Voordeckers, SF … HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() transformToJson() HTTP/1.1 200
OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() HTTP/1.1 200 OK Nitesh, Kant, SF Jo, Voordeckers, SF … HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … -
HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First:
Jo, Last: Voordeckers, Location: SF} … response.send() Find my friends getFriendsCsv() HTTP/1.1 200 OK Nitesh, Kant, SF Jo, Voordeckers, SF … 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() HTTP/1.1 200 OK
Nitesh, Kant, SF Jo, Voordeckers, SF … 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() response.send() HTTP/1.1 200
OK Nitesh, Kant, SF Jo, Voordeckers, SF … 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() response.send() HTTP/1.1 200
OK Nitesh, Kant, SF Jo, Voordeckers, SF … HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() response.send() HTTP/1.1 200
OK Nitesh, Kant, SF Jo, Voordeckers, SF … 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() response.send() HTTP/1.1 200
OK Nitesh, Kant, SF Jo, Voordeckers, SF … 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() HTTP/1.1 200 OK
Nitesh, Kant, SF Jo, Voordeckers, SF … Producer Consumer HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() -
HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First:
Jo, Last: Voordeckers, Location: SF} … Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() response.send() HTTP/1.1 200 OK Nitesh, Kant, SF Jo, Voordeckers, SF … Producer Consumer Control response.send() -
I am interested in consuming data! Give me 10 items!
I am not interested any more. Control -
Java Message Service https://docs.oracle.com/javaee/5/tutorial/doc/bncdx.html // Synchronous Message m = consumer.receive();
Message m = consumer.receive(1000); // time out 1000ms // Asynchronous MessageListener myListener = new MyListener(); // void onMessage(Message message) consumer.setMessageListener(myListener); request(1) -
Java Message Service https://docs.oracle.com/javaee/5/tutorial/doc/bncdx.html // Synchronous Message m = consumer.receive();
Message m = consumer.receive(1000); // time out 1000ms // Asynchronous MessageListener myListener = new MyListener(); // void onMessage(Message message) consumer.setMessageListener(myListener); flow control depends on broker -
Apache Kafka https://kafka.apache.org/10/javadoc/?org/apache/kafka/clients/consumer/KafkaConsumer.html KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); consumer.subscribe(Arrays.asList("foo",
"bar")); while (true) { ConsumerRecords<String, String> records = consumer.poll(100); // time out for (ConsumerRecord<String, String> record : records) { // iterate over batch } consumer.commitSync(); } // Flow control public void pause(java.util.Collection<TopicPartition> partitions) public void resume(java.util.Collection<TopicPartition> partitions) request(n) -
Apache Kafka https://kafka.apache.org/10/javadoc/?org/apache/kafka/clients/consumer/KafkaConsumer.html KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); consumer.subscribe(Arrays.asList("foo",
"bar")); while (true) { ConsumerRecords<String, String> records = consumer.poll(100); // time out for (ConsumerRecord<String, String> record : records) { // iterate over batch } consumer.commitSync(); } // Flow control public void pause(java.util.Collection<TopicPartition> partitions) public void resume(java.util.Collection<TopicPartition> partitions) additional flow control by topic/partition -
Apache Kafka JMS // Synchronous Message m = consumer.receive(); Message
m = consumer.receive(1000); // Asynchronous // void onMessage(Message message) MessageListener myListener = new MyListener(); consumer.setMessageListener(myListener); KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); consumer.subscribe(Arrays.asList("foo", "bar")); while (true) { ConsumerRecords<String, String> records = consumer.poll(100); for (ConsumerRecord<String, String> record : records) { // iterate over batch } consumer.commitSync(); } // Flow control public void pause(java.util.Collection<TopicPartition> partitions) public void resume(java.util.Collection<TopicPartition> partitions) -
Apache Kafka JMS // Synchronous Message m = consumer.receive(); Message
m = consumer.receive(1000); // Asynchronous // void onMessage(Message message) MessageListener myListener = new MyListener(); consumer.setMessageListener(myListener); KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); consumer.subscribe(Arrays.asList("foo", "bar")); while (true) { ConsumerRecords<String, String> records = consumer.poll(100); for (ConsumerRecord<String, String> record : records) { // iterate over batch } consumer.commitSync(); } // Flow control public void pause(java.util.Collection<TopicPartition> partitions) public void resume(java.util.Collection<TopicPartition> partitions) how do we bridge these APIs? -
Service A Service B cache Service C DB ? define
contract for interoperability -
Service A Service B cache Service C DB write adapters
for all the things \o/ ? -
Service A Service B cache Service C DB write adapters
for all the things \o/ ? ✓ cancelation ✓ buffering -
Reactive Streams public interface Publisher<T> { public void subscribe(Subscriber<? super
T> s); } I am interested in consuming data! -
Reactive Streams public interface Subscriber<T> { public void onSubscribe(Subscription s);
public void onNext(T t); public void onError(Throwable t); public void onComplete(); } -
Reactive Streams public interface Subscriber<T> { public void onSubscribe(Subscription s);
public void onNext(T t); public void onError(Throwable t); public void onComplete(); } Control exchange -
Reactive Streams public interface Subscription { public void request(long n);
public void cancel(); } -
Reactive Streams public interface Subscription { public void request(long n);
public void cancel(); } Give me “n” items! -
Reactive Streams public interface Subscription { public void request(long n);
public void cancel(); } I am not interested any more. -
under-specification risk of chaos between actors of the interaction rules
Image source: https://newrepublic.com/article/118416/what-dhaka-bangladesh-traffic-capital-world-can-teach-us -
under-specification risk of chaos between actors target least common denominator
of the interaction rules Image source: https://newrepublic.com/article/118416/what-dhaka-bangladesh-traffic-capital-world-can-teach-us -
under-specification risk of chaos between actors target least common denominator
systemic inefficiency of the interaction rules Image source: https://newrepublic.com/article/118416/what-dhaka-bangladesh-traffic-capital-world-can-teach-us -
onNext() onComplete() onError() onSubscribe() subscribe() Publisher Subscription onNext() onComplete() onError()
onSubscribe() Subscriber request() -
sequence onSubscribe() onComplete() subscribe() request() onSubscribe() subscribe() onError() request() onSubscribe()
subscribe() onNext() onNext() onComplete() request() -
sequence onSubscribe() onComplete() subscribe() request() onSubscribe() subscribe() onError() request() onSubscribe()
subscribe() onNext() onError() request() onSubscribe() subscribe() onNext() onNext() onComplete() request() -
sequence onSubscribe() onComplete() subscribe() request() onSubscribe() subscribe() onError() request() onSubscribe()
subscribe() onNext() onError() request() onSubscribe() subscribe() onNext() onNext() onComplete() request() request() cancel() onSubscribe() subscribe() -
sequence onSubscribe() onComplete() subscribe() request() onSubscribe() subscribe() onError() request() onSubscribe()
subscribe() onNext() onError() request() onSubscribe() subscribe() onNext() onNext() onComplete() request() request() cancel() onSubscribe() subscribe() request() cancel() onNext() onComplete() onSubscribe() subscribe() -
lifecycle management onNext() X subscribe() onNext() onSubscribe() onNext() request() onComplete()
X onError() subscribe() X -
lifecycle management onNext() X subscribe() cancel() request() subscribe() onSubscribe() onNext()
request() X onNext() onSubscribe() onNext() request() onComplete() X onError() subscribe() X -
Single item, asynchronously produced and delivered (Attempt #1) class SingleValue<T>
implements Publisher<T> { private T value; private boolean subscriberReady; private Subscriber<? super T> subscriber; @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { if (value != null) { s.onNext(value); } else { subscriberReady = true; } } @Override public void cancel() { subscriberReady = false; } }); } public void emit(T value) { if (subscriber != null && subscriberReady) { subscriber.onNext(value); } else { this.value = value; } } } * This is a hot/eager source, i.e. the work has started before a Subscriber arrives -
Single item, asynchronously produced and delivered (Attempt #1) class SingleValue<T>
implements Publisher<T> { private T value; private boolean subscriberReady; private Subscriber<? super T> subscriber; @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { if (value != null) { s.onNext(value); } else { subscriberReady = true; } } @Override public void cancel() { subscriberReady = false; } }); } public void emit(T value) { if (subscriber != null && subscriberReady) { subscriber.onNext(value); } else { this.value = value; } } } * This is a hot/eager source, i.e. the work has started before a Subscriber arrives emit() subscribe() request() cancel() -
Single item, asynchronously produced and delivered (Attempt #1) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives Asynchrony emit() subscribe() request() cancel() class SingleValue<T> implements Publisher<T> { private T value; private boolean subscriberReady; private Subscriber<? super T> subscriber; @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { if (value != null) { s.onNext(value); } else { subscriberReady = true; } } @Override public void cancel() { subscriberReady = false; } }); } public void emit(T value) { if (subscriber != null && subscriberReady) { subscriber.onNext(value); } else { this.value = value; } } } -
Single item, asynchronously produced and delivered (Attempt #1) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives Asynchrony emit() subscribe() request() cancel() class SingleValue<T> implements Publisher<T> { private T value; private boolean subscriberReady; private Subscriber<? super T> subscriber; @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { if (value != null) { s.onNext(value); } else { subscriberReady = true; } } @Override public void cancel() { subscriberReady = false; } }); } public void emit(T value) { if (subscriber != null && subscriberReady) { subscriber.onNext(value); } else { this.value = value; } } } -
Single item, asynchronously produced and delivered (Attempt #1) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives Asynchrony emit() subscribe() request() cancel() class SingleValue<T> implements Publisher<T> { private T value; private boolean subscriberReady; private Subscriber<? super T> subscriber; @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { if (value != null) { s.onNext(value); } else { subscriberReady = true; } } @Override public void cancel() { subscriberReady = false; } }); } public void emit(T value) { if (subscriber != null && subscriberReady) { subscriber.onNext(value); } else { this.value = value; } } } -
class SingleValue<T> implements Publisher<T> { private T value; private boolean
subscriberReady; private Subscriber<? super T> subscriber; @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { if (value != null) { s.onNext(value); } else { subscriberReady = true; } } @Override public void cancel() { subscriberReady = false; } }); } public void emit(T value) { if (subscriber != null && subscriberReady) { subscriber.onNext(value); } else { this.value = value; } } } Single item, asynchronously produced and delivered (Attempt #1) * This is a hot/eager source, i.e. the work has started before a Subscriber arrives Asynchrony emit() subscribe() request() cancel() -
Single item, asynchronously produced and delivered (Attempt #1) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives Concurrency emit() subscribe() request() cancel() class SingleValue<T> implements Publisher<T> { private T value; private boolean subscriberReady; private Subscriber<? super T> subscriber; @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { if (value != null) { s.onNext(value); } else { subscriberReady = true; } } @Override public void cancel() { subscriberReady = false; } }); } public void emit(T value) { if (subscriber != null && subscriberReady) { subscriber.onNext(value); } else { this.value = value; } } } -
Single item, asynchronously produced and delivered (Attempt #1) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives Concurrency emit() subscribe() request() cancel() class SingleValue<T> implements Publisher<T> { private T value; private boolean subscriberReady; private Subscriber<? super T> subscriber; @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { if (value != null) { s.onNext(value); } else { subscriberReady = true; } } @Override public void cancel() { subscriberReady = false; } }); } public void emit(T value) { if (subscriber != null && subscriberReady) { subscriber.onNext(value); } else { this.value = value; } } } -
Single item, asynchronously produced and delivered (Attempt #1) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives Concurrency emit() subscribe() request() cancel() class SingleValue<T> implements Publisher<T> { private T value; private boolean subscriberReady; private Subscriber<? super T> subscriber; @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { if (value != null) { s.onNext(value); } else { subscriberReady = true; } } @Override public void cancel() { subscriberReady = false; } }); } public void emit(T value) { if (subscriber != null && subscriberReady) { subscriber.onNext(value); } else { this.value = value; } } } -
Single item, asynchronously produced and delivered (Attempt #1) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives Concurrency emit() subscribe() request() cancel() X class SingleValue<T> implements Publisher<T> { private T value; private boolean subscriberReady; private Subscriber<? super T> subscriber; @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { if (value != null) { s.onNext(value); } else { subscriberReady = true; } } @Override public void cancel() { subscriberReady = false; } }); } public void emit(T value) { if (subscriber != null && subscriberReady) { subscriber.onNext(value); } else { this.value = value; } } } -
Single item, asynchronously produced and delivered (Attempt #1) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives Concurrency emit() subscribe() request() cancel() https://github.com/reactive-streams/reactive-streams-jvm#1.3 class SingleValue<T> implements Publisher<T> { private T value; private boolean subscriberReady; private Subscriber<? super T> subscriber; @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { if (value != null) { s.onNext(value); s.onComplete(); } else { subscriberReady = true; } } @Override public void cancel() { subscriberReady = false; } }); } public void emit(T value) { if (subscriber != null && subscriberReady) { subscriber.onNext(value); } else { this.value = value; } } } -
Single item, asynchronously produced and delivered (Attempt #1) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives Concurrency emit() subscribe() request() cancel() https://github.com/reactive-streams/reactive-streams-jvm#2.7 class SingleValue<T> implements Publisher<T> { private T value; private boolean subscriberReady; private Subscriber<? super T> subscriber; @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { if (value != null) { s.onNext(value); s.onComplete(); } else { subscriberReady = true; } } @Override public void cancel() { subscriberReady = false; } }); } public void emit(T value) { if (subscriber != null && subscriberReady) { subscriber.onNext(value); } else { this.value = value; } } } -
Single item, asynchronously produced and delivered (Attempt #2) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives Concurrency emit() subscribe() request() cancel() class SingleValue<T> implements Publisher<T> { private T value; private boolean subscriberReady; private Subscriber<? super T> subscriber; @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { if (value != null) { s.onNext(value); s.onComplete(); } else { subscriberReady = true; } } @Override public void cancel() { subscriberReady = false; } }); } public void emit(T value) { if (subscriber != null && subscriberReady) { subscriber.onNext(value); } else { this.value = value; } } } -
Single item, asynchronously produced and delivered (Attempt #2) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives Concurrency emit() subscribe() request() cancel() class SingleValue<T> implements Publisher<T> { private T value; private boolean subscriberReady; private Subscriber<? super T> subscriber; @Override public synchronized void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { if (value != null) { s.onNext(value); s.onComplete(); } else { subscriberReady = true; } } @Override public void cancel() { subscriberReady = false; } }); } public synchronized void emit(T value) { if (subscriber != null && subscriberReady) { subscriber.onNext(value); } else { this.value = value; } } } -
Single item, asynchronously produced and delivered (Attempt #3) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives https://github.com/reactive-streams/reactive-streams-jvm#3.4 -
class SingleValue<T> implements Publisher<T> { private static final int IDLE
= 0; private static final int REQUESTED = 1; private static final int EMITTED = 2; private T value; private Subscriber<? super T> subscriber; private final AtomicInteger state = new AtomicInteger(IDLE); @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { int old = state.getAndSet(REQUESTED); if (old == EMITTED) { s.onNext(value); s.onComplete(); } } @Override public void cancel() { } }); } public void emit(T value) { this.value = value; if (state.getAndSet(EMITTED) == REQUESTED) { subscriber.onNext(value); subscriber.onComplete(); } } } Single item, asynchronously produced and delivered (Attempt #4) * This is a hot/eager source, i.e. the work has started before a Subscriber arrives State Machine IDLE EMITTED REQUESTED -
Single item, asynchronously produced and delivered (Attempt #4) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives State Machine IDLE class SingleValue<T> implements Publisher<T> { private static final int IDLE = 0; private static final int REQUESTED = 1; private static final int EMITTED = 2; private T value; private Subscriber<? super T> subscriber; private final AtomicInteger state = new AtomicInteger(IDLE); @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { int old = state.getAndSet(REQUESTED); if (old == EMITTED) { s.onNext(value); s.onComplete(); } } @Override public void cancel() { } }); } public void emit(T value) { this.value = value; if (state.getAndSet(EMITTED) == REQUESTED) { subscriber.onNext(value); subscriber.onComplete(); } } } EMITTED REQUESTED -
Single item, asynchronously produced and delivered (Attempt #5) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives State Machine IDLE class SingleValue<T> implements Publisher<T> { private static final int IDLE = 0; private static final int REQUESTED = 1; private static final int EMITTED = 2; private static final int CANCELLED = 3; private T value; private Subscriber<? super T> subscriber; private final AtomicInteger state = new AtomicInteger(IDLE); @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { int old = state.getAndSet(REQUESTED); if (old == EMITTED) { s.onNext(value); s.onComplete(); } } @Override public void cancel() { state.set(CANCELLED); } }); } public void emit(T value) { this.value = value; if (state.getAndSet(EMITTED) == REQUESTED) { subscriber.onNext(value); subscriber.onComplete(); } } } EMITTED REQUESTED CANCELLED -
class SingleValue<T> implements Publisher<T> { private static final int IDLE
= 0; private static final int REQUESTED = 1; private static final int EMITTED = 2; private static final int CANCELLED = 3; private T value; private Subscriber<? super T> subscriber; private final AtomicInteger state = new AtomicInteger(IDLE); @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { int old = state.getAndSet(REQUESTED); if (old == EMITTED) { s.onNext(value); s.onComplete(); } } @Override public void cancel() { state.set(CANCELLED); } }); } public void emit(T value) { this.value = value; if (state.getAndSet(EMITTED) == REQUESTED) { subscriber.onNext(value); subscriber.onComplete(); } } } Single item, asynchronously produced and delivered (Attempt #5) * This is a hot/eager source, i.e. the work has started before a Subscriber arrives State Machine IDLE EMITTED REQUESTED CANCELLED -
Single item, asynchronously produced and delivered (Attempt #5) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives subscription.cancel(); subscription.request(1) class SingleValue<T> implements Publisher<T> { private static final int IDLE = 0; private static final int REQUESTED = 1; private static final int EMITTED = 2; private static final int CANCELLED = 3; private T value; private Subscriber<? super T> subscriber; private final AtomicInteger state = new AtomicInteger(IDLE); @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { int old = state.getAndSet(REQUESTED); if (old == EMITTED) { s.onNext(value); s.onComplete(); } } @Override public void cancel() { state.set(CANCELLED); } }); } public void emit(T value) { this.value = value; if (state.getAndSet(EMITTED) == REQUESTED) { subscriber.onNext(value); subscriber.onComplete(); } } } -
Single item, asynchronously produced and delivered (Attempt #6) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives State Machine IDLE class SingleValue<T> implements Publisher<T> { private static final int IDLE = 0; private static final int REQUESTED = 1; private static final int EMITTED = 2; private T value; private Subscriber<? super T> subscriber; private final AtomicInteger state = new AtomicInteger(IDLE); @Override public void subscribe(Subscriber<? super T> s) { subscriber = s; s.onSubscribe(new Subscription() { @Override public void request(long n) { int old = state.getAndSet(REQUESTED); if (old == EMITTED) { s.onNext(value); s.onComplete(); } } @Override public void cancel() { } }); } public void emit(T value) { this.value = value; if (state.getAndSet(EMITTED) == REQUESTED) { subscriber.onNext(value); subscriber.onComplete(); } } } EMITTED REQUESTED https://github.com/reactive-streams/reactive-streams-jvm#3.6 -
Single item, asynchronously produced and delivered (Attempt #6) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives class SingleValue<T> implements Publisher<T> { private static final int IDLE = 0; private static final int REQUESTED = 1; private static final int EMITTED = 2; private static final int CANCELLED = 3; private T value; private Subscriber<? super T> subscriber; private final AtomicInteger state = new AtomicInteger(IDLE); @Override public void subscribe(Subscriber<? super T> s) { s.onSubscribe(new Subscription() { @Override public void request(long n) { int old = state.getAndUpdate(val -> val == CANCELLED ? CANCELLED : REQUESTED); if (old == EMITTED) { s.onNext(value); s.onComplete(); } } @Override public void cancel() { state.set(CANCELLED); } }); subscriber = s; } public void emit(T value) { this.value = value; int old = state.getAndUpdate(val -> val == CANCELLED ? CANCELLED : EMITTED); if (old == REQUESTED) { subscriber.onNext(value); subscriber.onComplete(); } } } -
class SingleValue<T> implements Publisher<T> { private static final int IDLE
= 0; private static final int REQUESTED = 1; private static final int EMITTED = 2; private static final int CANCELLED = 3; private T value; private Subscriber<? super T> subscriber; private final AtomicInteger state = new AtomicInteger(IDLE); @Override public void subscribe(Subscriber<? super T> s) { s.onSubscribe(new Subscription() { @Override public void request(long n) { int old = state.getAndUpdate(val -> val == CANCELLED ? CANCELLED : REQUESTED); if (old == EMITTED) { s.onNext(value); s.onComplete(); } } @Override public void cancel() { state.set(CANCELLED); } }); subscriber = s; } public void emit(T value) { this.value = value; int old = state.getAndUpdate(val -> val == CANCELLED ? CANCELLED : EMITTED); if (old == REQUESTED) { subscriber.onNext(value); subscriber.onComplete(); } } } Single item, asynchronously produced and delivered (Attempt #6) * This is a hot/eager source, i.e. the work has started before a Subscriber arrives -
Single item, asynchronously produced and delivered (Attempt #6) * This
is a hot/eager source, i.e. the work has started before a Subscriber arrives class SingleValue<T> implements Publisher<T> { private static final int IDLE = 0; private static final int REQUESTED = 1; private static final int EMITTED = 2; private static final int TERMINATED = 3; private T value; private Subscriber<? super T> subscriber; private final AtomicInteger state = new AtomicInteger(IDLE); @Override public void subscribe(Subscriber<? super T> s) { s.onSubscribe(new Subscription() { @Override public void request(long n) { if (n <= 0 && state.getAndSet(TERMINATED) != TERMINATED) { s.onError(new IllegalArgumentException("Non positive request signals are illegal.")); return; } int old = state.getAndUpdate(val -> val == TERMINATED ? TERMINATED : REQUESTED); if (old == EMITTED) { state.set(TERMINATED); s.onNext(value); s.onComplete(); } } @Override public void cancel() { state.set(TERMINATED); } }); subscriber = s; } public void emit(T value) { this.value = value; int old = state.getAndUpdate(val -> val == TERMINATED ? TERMINATED : EMITTED); if (old == REQUESTED) { state.set(TERMINATED); subscriber.onNext(value); subscriber.onComplete(); } } } Want to see how multiple item emission will look like? -
response.send() Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() response.send() HTTP/1.1
200 OK Nitesh, Kant, SF Jo, Voordeckers, SF … 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 011101100 101110011 101100101 110011100 101110111 011001101 HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() transformToJson() HTTP/1.1 200
OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() HTTP/1.1 200 OK Nitesh, Kant, SF Jo, Voordeckers, SF … HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() transformToJson() HTTP/1.1 200
OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() HTTP/1.1 200 OK Nitesh, Kant, SF Jo, Voordeckers, SF … HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … Socket Socket Subscriber Publisher -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() transformToJson() HTTP/1.1 200
OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() HTTP/1.1 200 OK Nitesh, Kant, SF Jo, Voordeckers, SF … HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … Socket Socket Subscriber Publisher Control -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() transformToJson() HTTP/1.1 200
OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() HTTP/1.1 200 OK Nitesh, Kant, SF Jo, Voordeckers, SF … HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … Socket Socket Subscriber Publisher subscription.request(1) -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() transformToJson() HTTP/1.1 200
OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() HTTP/1.1 200 OK Nitesh, Kant, SF Jo, Voordeckers, SF … HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … Socket Socket Subscriber Publisher subscription.request(1) Demand is required to compose through multiple components -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() transformToJson() HTTP/1.1 200
OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() HTTP/1.1 200 OK Nitesh, Kant, SF Jo, Voordeckers, SF … HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … Socket Socket Subscriber Publisher subscription.cancel() Cancellation is required to compose through multiple components -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() transformToJson() HTTP/1.1 200
OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() HTTP/1.1 200 OK Nitesh, Kant, SF Jo, Voordeckers, SF … HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … Socket Socket Subscriber Publisher Publisher client.get("friends?id=1") response.send(Publisher resp) -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() transformToJson() HTTP/1.1 200
OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() HTTP/1.1 200 OK Nitesh, Kant, SF Jo, Voordeckers, SF … HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … Socket Socket Subscriber Publisher response.send( client.get("friends?id=1") ); -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() transformToJson() HTTP/1.1 200
OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() HTTP/1.1 200 OK Nitesh, Kant, SF Jo, Voordeckers, SF … HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … Socket Socket Subscriber Publisher List<Friend> transformToJson(List<String> csvs) { List<Friend> friends; for (String csv : csvs) { friends.add(toJson(csv)); } return friends; } -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() transformToJson() HTTP/1.1 200
OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() HTTP/1.1 200 OK Nitesh, Kant, SF Jo, Voordeckers, SF … HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … Socket Socket Subscriber Publisher response.send( client.get("friends?id=1") ); -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() transformToJson() HTTP/1.1 200
OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() HTTP/1.1 200 OK Nitesh, Kant, SF Jo, Voordeckers, SF … HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … Socket Socket Subscriber Publisher response.send( client.get("friends?id=1") .map(csv -> toJson(csv)) ); -
Find my friends GET /friends?id=3 HTTP/1.1 getFriendsCsv() transformToJson() HTTP/1.1 200
OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … response.send() HTTP/1.1 200 OK Nitesh, Kant, SF Jo, Voordeckers, SF … HTTP/1.1 200 OK {First: Nitesh, Last: Kant, Location: SF} {First: Jo, Last: Voordeckers, Location: SF} … Socket Socket Subscriber Publisher response.send( client.get("friends?id=1") .map(csv -> toJson(csv)) ); Function composition. -
Function Composition Lack of standards Project Reactor RxJava JDK Streams
Akka Streams Kafka Streams -
Not everything is a stream Publisher<User> getUser(String id); Publisher<Void> fireAndForget(String
log); Publisher<HttpResponse> request(HttpRequest request); -
Reactive Streams HTTP Kafka Cassandra RPC Redis () Function Composition
Not everything is a stream Blocking safety