In this post, we intend to bridge the gap between the world of Reactor and the powerful trends on the horizon of programming. Here we’ll discuss and demonstrate concepts, advantages, drawbacks and an approach for building an API using Project Reactor.
There are currently several programming paradigms; in this post we’ll discuss reactive programming which focuses on the asynchronous management of finite and infinite data flow. *Reactor *is a reactive programming library for the Java language which provides the basis for developing non-blocking applications, thus representing a change in how we think about an application’s execution model. Reactor was developed by Pivotal, a software and services company, which takes care of developments worldwide in different divisions of software engineering.
We would like to share some highlights, new trends in Java, and the world of programming that we have learned thanks to our project experiences and research. Our aim was to achieve better results with more complex applications but in a simplified way.
Taking as an example the development of an event manager with Twitter, we will explain how we consume and save responses to various requests in a non-blocking manner.
To talk about Project Reactor we must first define what Reactive Programmingis. It’s a paradigm or microarchitecture that involves the routing or consumption of Streams (data stream emitted over time). Data-flows and the propagation of changes that can be generated in the application analyzed, providing fast and consistent response times (responsive), remaining responsive to error situations (resilient) and to workload increases (elastic) based on the exchange of asynchronous messages (oriented to messages). Reactive Programming follows the Observer design pattern which means that when there is a change of status in an object, the other objects are notified and updated, thus reducing the inefficient use of resources. More can be learned about this topic in the Programming Reactive documentation.
Within this conceptual frame of reactive programming, we can begin to examine Project Reactor. As mentioned above, this is a library that exhibits the following characteristics:
Backpressure grants the Consumer of an asynchronous stream the ability to tell the Producer the amount of data that must be sent to prevent the issuance of events at a rate that is faster than the processing capabilities. Reactor provides several strategies to reduce the amount of data that gets sent, including engaging buffers and using the Windowing technique, that allows a program to analyze data from the last n seconds every m seconds.
Summing up, Project Reactor can maintain a high-performance message rate and also work with very low memory space. Thanks to these features it is suitable for creating efficient applications based on events. It allows these to cope with more requests at the same time in an asynchronous way which is ideal for high-latency applications.
The main artifact Project Reactor employs is reactor-core, which is a reactive library that focuses on the specification of reactive Streams and the Java 8 objectives. Reactor has two reactive types that implement the Publisher interface, but also provide a broad set of operators: Flux and *Mono. *These types allow applications to serve more requests at the same time and both support non-blocking backpressure.
There are standard or basic methods for its creation that are used by these operators, among which we find: create, defer and error.
Flux
Source: Reactor 3 Reference Guide. https://projectreactor.io/docs/core/release/reference/
A Flux object represents a reactive sequence of 0 to N elements, and also allows the generation of sources from arbitrary callback types. The following code fragment shows one of the most common and basic examples for creating a Flux.
// Creates a Flux containing integer values
Flux<Integer> integerFlux = Flux.just(1, 2, 3);
// Creates a Flux containing string values
Flux<String> stringFlux = Flux.just("Hello", "World", "Wolox");
// Creates a Flux from an already existing list
List<String> stringList = Arrays.asList("Hello", "World", "Wolox");
Flux<String> fluxFromList = Flux.fromIterable(stringList);
// It works the same with Java Streams (which are not reactive).
Stream<String> stringStream = stringList.stream();
Flux<String> fluxFromStream = Flux.fromStream(stringStream);
In the snippet, we see the creation of Fluxs of integers, String and even from a Java stream. We have a Flux.just(…) method that creates a Flux that emits the specified element, which is then captured at the time of instance creation. The Flux.fromIterable(…) method creates a Flux that emits the elements contained in the provided Iterable, and this will in turn create a new iterator for each Subscriber. Subsequently, we’ll see more elaborate Flux implementations to fetch values that are needed to subscribe and consume data from an external API.
Mono
Source: Reactor 3 ReferenceGuide. https://projectreactor.io/docs/core/release/reference/
A Mono object represents a single or empty value result (0…1) and allows deterministic generation from scratch or a sequence from arbitrary callback types. Below, some of the most common creations of Mono are shown. The Mono.empty() method creates a Mono that is completed without emitting any element.
// Creating a Mono containing "Hello World Wolox"
Mono<String> helloWorldWolox = Mono.just("Hello World Wolox");
// Creating an empty Mono
Mono<T> empty = Mono.empty();
// Creating a mono from a Callable
Mono<String> helloWorldWoloxCallable = Mono.fromCallable(() -> "Hello World Wolox");
// Same with Java 8 method reference
Mono<User> user = Mono.fromCallable(UserService::fetchAnyUser);
Schedulers
Reactor uses a Scheduler that determines the context for an execution of arbitrary tasks, providing the assurance required by a reactive Stream. We can also use or create efficient Schedulers for subscribeOn and publishOn. It’s possible to use multiple reactor instances that can be instantiated with different schedulers.
// Insert a person, calling your DAO
Mono personWrapper = Mono
.fromCallable(
() -> personDao.insertPerson(person));
return personWrapper.subscribeOn(Schedulers.elastic());
// Get a person by identification
Mono<Person> personWrapper = Mono
.fromCallable(() -> {
return personDao.findByRut(id);
});
return personWrapper.subscribeOn(Schedulers.elastic());
The snippet shows two examples that are analyzed with Schedulers. The first one defines a variable of type Mono that calls the Dao to insert a Person object. A method called Mono.fromCallable is observed, and this method expects that the supplier method (in this case insertPerson) returns a value of type T, and creates a type Mono, which is non-blocking. This method even captures errors implicitly and maps them to a Mono.error(…).
It should be clarified that Reactor has implemented its own error capture, and as it was just mentioned, it does so through the error() method. The *Mono.defer(…) *method works in a similar way to the fromCallable but has a difference: its method supplier must return a Mono value. Since it doesn’t capture the errors, we need to do it ourselves. Once the Mono is captured, it’s returned through a subscribeOn, passing the Schedulers.elastic() as parameters, which returns a shared Schedulers instance. This means that multiple calls to this function will return the same Scheduler.
In other words, it dynamically creates groups of execution-services based workers as necessary and reuses the inactive ones, saving the groups of sub-processes or workers in cache. Groups of sub-processes that remain inactive for 60 seconds are eliminated. Elastic() method is a useful way to assign it’s own sub-processes to a blocking process so that other resources aren’t compromised. Therefore, elastic() is considered as the default Scheduler. Note that in the other example, though the method returns a Mono, it follows the same behavior as in the previous case in order to abstract its data.
Now that we have a clear concept of Reactor we are going to demonstrate and develop an implementation of how it works with the consumption of external APIs, in this case with Twitter API. We will observe how it responds asynchronously to all the tweets that are captured from the application and show how it processes the information in a non-blocking way.
We must first configure the build.gradle file with the necessary dependencies for the example. In the following snippet we see the Reactor dependencies, Twitter (we’ll use the properties of Twitter4J), Spring Boot, among others. IntellijIDE is used as the text editor.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.json:json:20180813'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testCompile group: 'com.github.javafaker', name: 'javafaker', version: '0.15'
testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
testCompile group: 'io.projectreactor', name: 'reactor-test'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'com.h2database:h2'
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
compile group: 'com.google.guava', name: 'guava', version: '27.0-jre'
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.9.8'
compile group: 'io.projectreactor', name: 'reactor-core', version: '3.2.6.RELEASE'
compile "io.projectreactor.netty:reactor-netty:0.8.5.RELEASE"
compile group: 'org.twitter4j', name: 'twitter4j-stream', version: '4.0.2'
}
Coding
A service called TwitterService is created, which validates that our stream always exists, in this case as subscribers on Twitter. It does so through the abstract ConnectableFlux class, which allows subscribers to accumulate before connecting to their data source. This means that when we call the subscribe() method it doesn’t start broadcasting immediately and therefore we can add several subscriptions in advance. It then validates the Twitter stream calling a method that configures or builds it by adding credentials, methods to implement and finally adds the stream to a Listener. All of this in the case when the stream has no connection and hasn’t started accumulating subscriptions.
There are several properties to configure Twitter4J, either by creating an instance of the ConfigurationBuilder class to do it manually, or it could also be done through the creation of a twitter4j.properties file. The following example was done in the service using the first method. In this configuration, it’s necessary to create the consumerKey, consumerSecret, accessToken and accessTokenSecret, each with their respective credentials.
public class TwitterService {
private static ConnectableFlux twitterStream;
public static synchronized ConnectableFlux getTwitterStream() {
if (twitterStream == null) {
initTwitterStream();
}
return twitterStream;
}
private static void initTwitterStream() {
Flux<Status> stream = Flux.create(emitter -> {
StatusListener listener = new StatusListener() {
@Override
public void onException(Exception e) {
emitter.error(e);
}
@Override
public void onDeletionNotice(StatusDeletionNotice arg) {
}
@Override
public void onScrubGeo(long userId, long upToStatusId) {
}
@Override
public void onStallWarning(StallWarning warning) {
}
@Override
public void onStatus(Status status) {
emitter.next(status);
}
@Override
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println(numberOfLimitedStatuses);
}
};
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("YOUR_ACCESS_KEY")
.setOAuthConsumerSecret("YOUR_ACCESS_SECRET")
.setOAuthAccessToken("YOUR_ACCESS_TOKEN")
.setOAuthAccessTokenSecret("YOUR_ACCESS_TOKEN_SECRET");
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
twitterStream.addListener(listener);
twitterStream.sample();
});
twitterStream = stream.publish();
twitterStream.connect();
}
}
Following this, an instance of the TwitterStream interface is created, passing the configuration that was made in the previous step as parameter. Then a StatusListener with its properties is added, working as a streams reader. With the sample() method we start listening to a random sample of all public statuses. The final result is that we have a stream.publish() method that allows us to publish our own messages in the TwitterStream instance while also having the *connect() *method that sends a connection request to the API to open up the data stream and receive the tweets. The data transmission model opens up a Pipeline for data to be sent as they occur, having an indefinite period of existence.
With the configured service, a controller called TwitterController is created, and with it, the communication of the external service with the application to obtain different results with the data of the captured tweets. We show that four endpoints will be created where we have different behaviors and different data in order to show the different actions that can be achieved with reactor and the Twitter API.
@RestController
@RequestMapping("/api/tweets")
public class TwitterController {
@GetMapping(path = "/filtered", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> filtered() {
ConnectableFlux<Status> flux = TwitterService.getTwitterStream();
return flux
.filter(status -> status.getText().contains("the"))
.map(status -> status.getText());
}
@GetMapping(path = "/feed", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> feed() {
ConnectableFlux<Status> flux = TwitterService.getTwitterStream();
return flux.map(status -> status.getText());
}
@GetMapping(path = "/onePerSecond", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> onePerSecond() {
ConnectableFlux<Status> flux = TwitterService.getTwitterStream();
Flux<Status> filtered = flux.filter(status -> {
Place place = status.getPlace();
if (place != null) {
return status.getPlace().getCountryCode().equalsIgnoreCase("us");
}
return false;
});
return filtered
.map(status -> status.getCreatedAt().toGMTString() + " " + status.getPlace().getCountryCode() + " " + status.getText());
}
@GetMapping(path = "/grouped", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux grouped() {
ConnectableFlux<Status> flux = TwitterService.getTwitterStream();
Flux<Status> filtered = flux.filter(status -> {
Place place = status.getPlace();
if (place != null) {
return status.getPlace().getCountryCode().equalsIgnoreCase("us");
}
return false;
});
return Flux.interval(Duration.ofSeconds(1))
.zipWith(filtered, (tick, status) -> status)
.map(status -> status.getText());
}
}
The first endpoint called Filtered() is a Get method that returns a Flux; in this function, we get the connection to the external API to gather all tweets, then we make a filter to only get those statuses that contain the word “the”, and finally a map of the result is made and the obtained tweets are returned. Regarding the second endpoint feed() the Twitter service that was implemented is once again called, and we get all the news or published statuses through a map.
In the third endpoint onePerSecond() the same process of obtaining all tweets through the service is followed: a Flux is created, filtering by the tweet’s place or location if present and a Place instance is created, which is an interface provided by the Twitter API that extends TwitterResponse and other interfaces. The snippet shows the methods that can be used. It validates that a Placeexists, and only tweets within states in the United States are returned. Finally, using the map function, parsed tweets are returned in a cleaner format.
public interface Place extends TwitterResponse, Comparable<Place>, java.io.Serializable {
String getName();
String getStreetAddress();
String getCountryCode();
String getId();
String getCountry();
String getPlaceType();
String getURL();
String getFullName();
String getBoundingBoxType();
GeoLocation[][] getBoundingBoxCoordinates();
String getGeometryType();
GeoLocation[][] getGeometryCoordinates();
Place[] getContainedWithIn();
}
In the fourth and final endpoint grouped(), tweets are grouped every 1 second, and once again the filter is defined by tweets originating in the United States, but this time the Flux.interval(…) method is used to assign a grouping duration, together with the zipWith(…) method to merge the previously obtained filter and then return its publications.
As illustrated by these examples, we can deepen and get fascinating things in today’s applications using Reactor — especially for microservices, a growing trend in digital transformation. Therefore, I invite readers to continue to engage with this amazing topic. In summary, I’d like to highlight the advantages and drawbacks of reactive and reactor programming.
Advantages
We can answer many requests or call messages by generating one or a few threads.
It’s possible to do a callback asynchronously and this could potentially save us calling resources.
It achieves weakly coupled programming and tends to isolate faults or errors, so it’s easily scalable, anticipating the handling of the number of events it can receive.
Through the efficient use of resources, this is absolutely doing much more with less. Specifically, we can process higher workloads with fewer threads.
Drawbacks
More intensive memory usage is needed to store large data-flows as they are maintained for a long time.
It may be a little different from conventional programming, and it may be hard to understand in the beginning.
Most of the complexities must be dealt with at the time of declaring the service.
It doesn’t work well for applications that have very little data flow, as it can deem simple programming unnecessarily complex, or possibly even affect the performance.
Although Reactor has only been around for a short time, it achieved a great impact on applications that suffer from high latency, allowing better processing and response performance. This makes it ideal for the programming world’s new trend, as well as zooming in and allowing reactive programming in Java.
On the other hand, it’s proving itself to be a strong library since nowadays we have devices and applications connected to the Internet 24/7 so we need to show information almost instantaneously to millions of users, generating very intense loads. That’s why I see great potential in Reactor being able to respond in an optimal and correct way to these massive data demands, meaning that the application responds as the user expects it.
Finally, I’d like to thank Matias de Santi for being one of the contributors to the code proposed in this post.
#java