Did you try to implement one of the new java.util.concurrent.Flow.* interfaces yourself? Then you’re most probably doing it wrong.
The purpose of this talk is to show that implementing them yourself is far from trivial and to discuss the actual reasons why they have been included in the JDK.
Reactive Streams is a standard for asynchronous data processing in a streaming fashion with non-blocking backpressure. Starting from Java 9, they have become a part of the JDK in the form of the java.util.concurrent.Flow interfaces.
Having the interfaces at hand may tempt you to write your own implementations. Surprising as it may seem, that’s not what they are in the JDK for.
In this session, we’re going to go through the basic concepts of reactive stream processing and see how (not) to use the APIs included in JDK 9+. Plus we’re going to ponder the possible directions in which JDK’s Reactive Streams support may go in the future.
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
How (not) to use reactive streams in java 9+
1. HOW (NOT) TO USE
REACTIVE STREAMS
IN JAVA 9+
JACEK KUNICKI
@rucek
2. @rucekHOW (NOT) TO USE REACTIVE STREAMS IN JAVA9+
STREAM PROCESSING
PRODUCER CONSUMERPROCESSING
STAGE 1
PROCESSING
STAGE N
DATA
SubscriberPublisher
BACKPRESSURE
request(n)
Processor Processor
3. @rucek
REACTIVE STREAMS
▸ asynchronous
▸ non-blocking backpressure
▸ reuse threads whenever possible
▸ slow consumers are represented in the domain model, e.g.
▸ Twitter API can tell you that you’re consuming too slow
▸ conflate() in Akka Streams - aggregates when the downstream is slow
HOW (NOT) TO USE REACTIVE STREAMS IN JAVA9+
5. @rucek
j.u.c.Flow.Publisher<T>
▸ produces items of type T that subscribers are going to consume
▸ subscribers are registered via subscribe(Subscriber<? super T>)
HOW (NOT) TO USE REACTIVE STREAMS IN JAVA9+
6. @rucek
j.u.c.Flow.Subscriber<T>
▸ subscribes to a producer in order to receive:
▸ subscription confirmation via onSubscribe(Subscription)
▸ items via onNext(T)
▸ errors via onError(Throwable)
▸ completion signal via onComplete()
HOW (NOT) TO USE REACTIVE STREAMS IN JAVA9+
7. @rucek
j.u.c.Flow.Subscription
▸ connects a single producer to a single subscriber, allows to:
▸ backpressure with request(long)
▸ signal termination with cancel()
HOW (NOT) TO USE REACTIVE STREAMS IN JAVA9+
11. @rucek
FURTHER CHALLENGES
▸ unbounded recursion through request() -> onNext() -> request() -> …
▸ handling infinite demand
▸ just calling onNext() for each of MAX_VALUE elements will exhaust the threads
▸ long demand + incrementing is not enough - overflow
HOW (NOT) TO USE REACTIVE STREAMS IN JAVA9+
16. @rucek
MINIMUM OPERATION SET
▸ only interfaces at the moment
▸ no basic operations like filter, map etc.
▸ https://github.com/lightbend/reactive-streams-utils
▸ basic operations built-in, others pluggable like -Djava.flow.provider=akka
HOW (NOT) TO USE REACTIVE STREAMS IN JAVA9+
but politics: vs
17. @rucek
HTTP
▸ async Servlet IO (since 3.1)
▸ JDK 9+ HTTP client provides a POST(Publisher<ByteBuffer>)
▸ if the HttpServletRequest provided a body publisher, file upload would become:
HOW (NOT) TO USE REACTIVE STREAMS IN JAVA9+
POST(BodyPublisher.fromPublisher(req.getPublisher())
18. @rucek
DATABASE ACCESS
▸ ADBA (Asynchronous Database Access API)
▸ existing vendor-specific async drivers
▸ JPA - what if…
HOW (NOT) TO USE REACTIVE STREAMS IN JAVA9+
Publisher<User> users = entityManager
.createQuery("select u from users")
.getResultPublisher()
19. @rucek
AND MORE
▸ reactive file IO (like a Publisher<Byte>)
▸ JMS
▸ websockets
▸ AWS - on the way
▸ Alpakka?
HOW (NOT) TO USE REACTIVE STREAMS IN JAVA9+
20. @rucek
DEMO 2 - SIMPLE INTEGRATION
▸ Project Reactor’s Flux as a publisher
▸ Akka Streams Flow as a processor
▸ RxJava as a subscriber
HOW (NOT) TO USE REACTIVE STREAMS IN JAVA9+
21. @rucek
SUMMARY
▸ not a full Reactive Streams implementation
▸ an SPI that allows for interoperability between other implementations
▸ implementing it yourself is at least non-trivial
▸ use the TCK
HOW (NOT) TO USE REACTIVE STREAMS IN JAVA9+
22. @rucek
RESOURCES
▸ pluggable runtime: https://github.com/lightbend/reactive-streams-utils
▸ TCK: https://github.com/reactive-streams/reactive-streams-jvm#specification
▸ ADBA: https://blogs.oracle.com/java/jdbc-next:-a-new-asynchronous-api-for-
connecting-to-a-database
▸ Advanced Reactive Java: http://akarnokd.blogspot.com/
HOW (NOT) TO USE REACTIVE STREAMS IN JAVA9+