
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to implement reactive streams using Flow API in Java 9?
Flow API is official support for reactive streams specification since Java 9. It is a combination of both Iterator and Observer patterns. The Flow API is an interoperation specification and not an end-user API like RxJava.
Flow API consists of four basic interfaces:
- Subscriber: The Subscriber subscribes to Publisher for callbacks.
- Publisher: The Publisher publishes the stream of data items to the registered subscribers.
- Subscription: The link between publisher and subscriber.
- Processor: The processor sits between Publisher and Subscriber, and transforms one stream to another.
In the below example, we have created a basic subscriber that asks for one data object, prints it and asks for one more. We can use a publisher implementation provided by Java (SubmissionPublisher) to complete our session.
Example
import java.util.concurrent.Flow; import java.util.List; import java.util.concurrent.SubmissionPublisher; class MySubscriber<T>implements Flow.Subscriber<T> { private Flow.Subscription subscription; @Override public void onSubscribe(Flow.Subscription subscription) { this.subscription = subscription; this.subscription.request(1); } @Override public void onNext(T item) { System.out.println(item); subscription.request(1); } @Override public void onError(Throwable throwable) { throwable.printStackTrace(); } @Override public void onComplete() { System.out.println("Done"); } } // main class public class FlowTest { public static void main(String args[]) { List<String> items = List.of("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"); SubmissionPublisher<String> publisher = new SubmissionPublisher<>(); publisher.subscribe(new MySubscriber<>()); items.forEach(s -> { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } publisher.submit(s); }); publisher.close(); } }
Output
1 2 3 4 5 6 7 8 9 10 Done
- Related Articles
- How can we implement Flow API using Publisher-Subscriber in Java 9?
- What are the core interfaces of Reactive Streams in Java 9?
- What are the steps to execute Flow API in Java 9?
- How can we implement methods of Stream API in Java 9?
- How to implement java.time.LocalDate using JShell in Java 9?
- How to filter stack frames using StackWalker API in Java 9?
- How to implement an ArrayList using JShell in Java 9?
- How to implement JShell using JavaFX in Java 9?\n
- How to terminate/destroy a process using Process API in Java 9?
- How to iterate List Using Streams in Java?
- How can we implement a JSON array using Streaming API in Java?
- StackWalker API in Java 9?
- How to get all children of a process using Process API in Java 9?
- How to implement Flow.Publisher interface in Java 9?
- How to print all attributes in StackFrame API in Java 9?

Advertisements