In this blog, I would be providing an overview of the spring reactor project, which is a reactive library based on the reactive specifications so let’s get started.
Introduction
Spring framework started support for reactive programming in version 5 via project reactor.
Project Reactor is a fully non-blocking foundation with back-pressure support also included. It’s the foundation of the reactive stack in the Spring ecosystem and is featured in projects such as Spring WebFlux, Spring Data, and Spring Cloud Gateway.
The Project reactor is foundation of the reactive stack in spring ecosystem. Even spring webflux aw well as other spring reactive framework uses reactor as a core dependency.
Modules
Project reactor consist a set of modules and Reactive Core is main module which implement the reactive specifications. It holds reactive types Flux, Mono with some set of operators which help in the transform or processing of reactive streams. Find the list of some other reactor modules below –
- Reactor-Test – This module provides some utilities to test the reactive streams.
- Reactor-Extra – provides some additional operators for reactive stream transformations.
- Reactor-Netty – based on Netty framework and it provides non-blocking and backpressure ready client and servers.
- Reactor-Kafka – This module helps for publishing and consume data from Kafka.
- Reactor-RabbitMQ – helps for publishing and consume data from RabbitMQ.
Reactive Specification
Reactive specification is the set of rules which every reactive library needs to follow in order to become reactive in nature. In reactive specification we have 4 interfaces which reactive library implements and these interfaces would talk to each other for the reactive flow.
- Publisher
- Subscriber
- Subscription
- Processor
Publisher
public interface Publisher<T> {
public void subscribe(Subscriber<? super T> s);
}
The implementation of publisher act as data source in the application also as data emitter. Publisher interface has only one method which would invoked by subscriber with its own reference, passed as parameter.
Subscriber
public interface Subscriber<T> {
public void onSubscribe(Subscription s);
public void onNext(T t);
public void onError(Throwable t);
public void onComplete();
}
The implementation of subscriber act as data receiver. It has 4 method, called by publisher instance. onSubscribe method is called when subscribe method and publisher is successfully completed. onNext method is called each time the publisher wants to send data to the subscriber. onError method is called when publisher comes across any error while sending the response to subscriber. onComplete method is called on successful completion of the request by publisher.
Subscription
public interface Subscription {
public void request(long n);
public void cancel();
}
The implementation of subscription is used to request data from publisher, also cancel a request.
Processor
public interface Processor<T, R> extends Subscriber<T>, Publisher<R> {
}
The processor interface is the combination of both publisher and subscriber interfaces. It has methods of both interfaces.
That’s pretty much it for this article. Also, If you have any feedback or queries, please do let me know in the comments. Also, if you liked the article, please give me a thumbs up also, I will keep writing blogs like this for you in the future as well. Keep reading and Keep coding.
References
Reactive streams
Project Reactor doc
