- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the steps to execute Flow API in Java 9?
85 Lectures 6 hours
Flow API in Java 9 corresponds to Reactive Streams specification, which is a defacto standard. It contains a minimal set of interfaces that capture the heart of asynchronous publication and subscription.
Below are the key interfaces of Flow API:
1) Flow.Publisher: It produces items for subscribers to consume, and it contains only method: subscribe(Subscriber), whose purpose should be obvious.
void subscribe(Flow.Subscriber<? super T> subscriber)
2) Flow.Subscriber: It subscribes to publishers (usually only one) to receive items (via method onNext(T)), error messages (onError(Throwable)), or a signal that no more items are to be expected (onComplete()). Before any of those things happen, the publisher calls onSubscription(Subscription) method.
void onSubscribe(Flow.Subscription subscription) void onNext(T item) void onError(Throwable throwable) void onComplete()
3) Flow.Subscription: The connection between a single publisher and a single subscriber. The subscriber can use it to request more items (request(long)) or break the connection (cancel()).
void request(long n) void cancel()
Execution steps for Flow API:
- First, we need to create a Publisher and a Subscriber.
- Subscribe the subscriber with Publisher:: subscribe.
- The publisher creates a Subscription and calls Subscriber::onSubscription with it, so the subscriber can store the subscription.
- At some point, the subscriber calls Subscription:: request to request a number of items.
- The publisher starts handing items to the subscriber by calling Subscriber::onNext. It never publishes more than the requested number of items.
- The publisher might at some point be run into trouble and call Subscriber::onComplete or Subscriber::onError, respectively.
- The subscriber might either continue to request more items every now and then or cut the connection by calling Subscription:: cancel.
- What are the CompletableFuture API improvements in Java 9?
- What are the different steps involved to execute a Java program?
- How to implement reactive streams using Flow API in Java 9?
- What are the improvements in Process API in Java 9?
- What are the new features added to Stream API in Java 9?
- What are the new methods added to Process API in Java 9?
- What are the core library changes in Process API in Java 9?
- How can we implement Flow API using Publisher-Subscriber in Java 9?
- StackWalker API in Java 9?
- What is Platform Logging API in Java 9?
- How to use the collect() method in Stream API in Java 9?
- What are the enhancements in Internationalization in Java 9?
- What are the steps to read static members in a Java class?
- How to define control flow statements in JShell in Java 9?
- How to get the parent process of the Process API in Java 9?