



{"id":602,"date":"2023-07-13T22:49:48","date_gmt":"2023-07-13T22:49:48","guid":{"rendered":"https:\/\/www.w3computing.com\/articles\/?p=602"},"modified":"2023-08-23T16:21:17","modified_gmt":"2023-08-23T16:21:17","slug":"reactive-programming-java-project-reactor-rxjava","status":"publish","type":"post","link":"https:\/\/www.w3computing.com\/articles\/reactive-programming-java-project-reactor-rxjava\/","title":{"rendered":"Reactive Programming in Java with Project Reactor and RxJava"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Defining Reactive Programming<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Reactive Programming is a paradigm shift from the traditional, imperative style of programming that has long dominated the software industry. Unlike its predecessor, which organizes programming logic around a sequence of commands, Reactive Programming is based on asynchronous data streams and the propagation of change. It encourages building systems that are more flexible, responsive, and resilient by design. These systems readily respond to changes, like user interactions or I\/O operations, maintaining high performance and usability.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Rise of Reactive Programming<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In today&#8217;s world, the scale, complexity, and real-time nature of software applications demand an approach that can handle large volumes of data efficiently, propagate changes across various components instantaneously, and maintain robustness in the face of failures. Reactive Programming does just that and is gaining traction in modern application development for its ability to address these needs effectively. It is no longer a luxury but a necessity to ensure smooth, responsive user experiences, especially in an increasingly asynchronous and event-driven world.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Java and Reactive Programming: Project Reactor and RxJava<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">While Reactive Programming is language-agnostic, this article will focus on its implementation using Java, a language widely used in enterprise applications. When it comes to Java-based tools for Reactive Programming, two names stand out: Project Reactor and RxJava.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Project Reactor, often associated with the Spring Framework, is a library for building non-blocking applications on the JVM. It&#8217;s based on the Reactive Stream specification and offers a rich toolbox for handling back-pressure, a concept vital in Reactive Programming.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On the other hand, RxJava is another popular library for composing asynchronous and event-based programs using observable sequences. It offers powerful, flexible, and abstracted tools to work with asynchronous data streams.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Through the course of this article, we will take a deep dive into both Project Reactor and RxJava, understanding their philosophies, examining their unique features, and exploring practical code examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Project Reactor?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Project Reactor is a fully non-blocking foundation for Java that allows developers to build efficient, concurrent applications on the JVM. It is a fourth-generation reactive library, based on the Reactive Stream specification, which promotes a reactive programming model to support high throughput and low latency.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Non-Blocking Programming with Project Reactor<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In traditional imperative programming, threads often get blocked while waiting for a slow process to complete. This blocking nature can limit system resources, negatively impact performance, and complicate scalability.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Project Reactor, on the other hand, is designed to support non-blocking programming. It enables processing to continue while waiting for slow operations, like I\/O, to complete. This asynchronous processing means a single thread can handle multiple requests concurrently, leading to better utilization of system resources.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advantages of Project Reactor<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Back-Pressure<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">One of the key advantages of Project Reactor is its ability to manage back-pressure. Back-pressure is a critical strategy to ensure that a slower subscriber does not get overwhelmed by a faster publisher. By giving subscribers the power to dictate how much data they can handle, Project Reactor prevents system crashes due to data overflow.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Higher Efficiency under Heavy Load<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Another advantage of Project Reactor is its ability to maintain high performance even under heavy load. Thanks to its non-blocking nature and support for back-pressure, Project Reactor efficiently handles large amounts of data with minimal threads, thereby reducing the overhead of context switching.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Project Reactor in Action: A Simple Code Example<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s look at a basic example of creating a reactive stream with Project Reactor:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">import<\/span> reactor.core.publisher.Flux;\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ProjectReactorExample<\/span> <\/span>{\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\n        Flux&lt;String&gt; flux = Flux.just(<span class=\"hljs-string\">\"Hello\"<\/span>, <span class=\"hljs-string\">\"Reactive\"<\/span>, <span class=\"hljs-string\">\"World\"<\/span>);\n        flux.subscribe(System.out::println);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this code, we create a <code>Flux<\/code> which is a publisher that can emit multiple elements. We subscribe to this <code>Flux<\/code> and print each of the emitted elements. When this program is run, it will output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">Hello\nReactive\nWorld<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The beauty of this example lies in its simplicity and power. With just a few lines of code, we&#8217;ve created an asynchronous data stream. We&#8217;ll build on this foundation as we dive deeper into the world of Reactive Programming with Project Reactor and RxJava.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is RxJava?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">RxJava stands for Reactive Extensions for the Java programming language. It is a library that lets developers compose asynchronous and event-based programs using observable sequences. RxJava is part of the ReactiveX project, which provides similar functionality across different programming languages.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Purpose of RxJava<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The primary purpose of RxJava is to enable efficient event handling in applications. It promotes the observer pattern, making it easier to program dynamic, asynchronous, and concurrent systems. With RxJava, developers can model and manipulate data and events as observable streams.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Core Components of RxJava: Observables, Observers, and Schedulers<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Observables:<\/strong> These are the sources of data in RxJava. An Observable emits items, which could be anything &#8211; strings, numbers, data structures, etc. They can be thought of as a data stream that one or more observers can subscribe to.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Observers:<\/strong> These are the consumers of the data emitted by Observables. An Observer can take three types of actions: consume the emitted data (onNext), handle an error (onError), or react to the completion of the Observable&#8217;s sequence (onComplete).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Schedulers:<\/strong> Schedulers control the concurrency in RxJava. They decide on which thread the execution should happen and manage the switching of the threads.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advantages of RxJava<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functional-style operations:<\/strong> RxJava allows developers to perform complex transformations and operations on the data using functional-style operators like map, filter, reduce, etc. This makes the code more readable, efficient, and easier to reason about.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Handling high amounts of data:<\/strong> RxJava can efficiently handle large amounts of data due to its asynchronous nature and back-pressure handling capabilities. It can deal with data streams that emit thousands of events without overwhelming the system or slowing down the application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">RxJava in Action: A Basic Code Example<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Below is a simple example of an Observable-Observer model in RxJava:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">import<\/span> io.reactivex.Observable;\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">RxJavaExample<\/span> <\/span>{\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\n        Observable&lt;String&gt; observable = Observable.just(<span class=\"hljs-string\">\"Hello\"<\/span>, <span class=\"hljs-string\">\"RxJava\"<\/span>, <span class=\"hljs-string\">\"World\"<\/span>);\n        observable.subscribe(System.out::println);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we create an Observable using the <code>just<\/code> method, which emits a fixed sequence of items. Then, we subscribe to this Observable and print each item to the console. The output will be:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">Hello\nRxJava\nWorld<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This is a basic demonstration of how RxJava works. It&#8217;s easy to see how this model can be extended and adapted to handle more complex, real-world scenarios in Reactive Programming. In the coming sections, we will further delve into the intricacies of both Project Reactor and RxJava, enhancing our understanding of the Reactive Programming world.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Project Reactor vs RxJava<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When it comes to reactive programming in Java, both Project Reactor and RxJava are robust choices. They are similar in many ways, given that both implement the Reactive Streams specification and share a common goal of enabling efficient, scalable, and resilient applications. However, there are also a few distinctions that set them apart.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Philosophy<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">While both libraries advocate the reactive programming paradigm, their philosophies have subtle differences. RxJava, originating from the broader ReactiveX project, is designed with an emphasis on cross-platform applicability. It&#8217;s part of a larger family of libraries designed to provide similar capabilities across different programming languages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On the other hand, Project Reactor is explicitly designed for Java 8 and above, with a deep integration with the Spring ecosystem, notably Spring WebFlux for non-blocking web applications. Its design is heavily influenced by the functional programming features introduced in Java 8.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Features<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Both libraries offer similar sets of operators for manipulating data streams. However, Project Reactor provides two main types of reactive types: <code>Flux<\/code> (0 to N emissions) and <code>Mono<\/code> (0 or 1 emission). On the other hand, RxJava offers more reactive types like <code>Observable<\/code>, <code>Single<\/code>, <code>Maybe<\/code>, and <code>Completable<\/code>, each designed for specific use cases.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Performance<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Both libraries showcase impressive performance and can handle a large number of requests concurrently with minimal resource utilization. However, their performance might vary depending on the specific use case and application requirements. It&#8217;s always a good practice to benchmark both libraries against your specific use case to decide the best fit.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Community Support<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Both libraries enjoy strong community support. RxJava, being older and part of the larger ReactiveX family, has a wider user base. Project Reactor, however, benefits from its tight integration with Spring, one of the most widely used frameworks in the Java ecosystem.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax and Approach<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s look at an example of creating a stream of integers and applying a transformation operation:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Project Reactor:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">import<\/span> reactor.core.publisher.Flux;\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ProjectReactorExample<\/span> <\/span>{\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\n        Flux.range(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">5<\/span>)\n            .map(i -&gt; i * <span class=\"hljs-number\">2<\/span>)\n            .subscribe(System.out::println);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>RxJava:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> io.reactivex.Observable;\n\npublic <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">RxJavaExample<\/span> <\/span>{\n\n    public <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> main(<span class=\"hljs-built_in\">String<\/span>&#91;] args) {\n        Observable.range(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">5<\/span>)\n            .map(i -&gt; i * <span class=\"hljs-number\">2<\/span>)\n            .subscribe(System.out::println);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Both pieces of code achieve the same outcome, doubling a range of integers. However, you&#8217;ll notice that we&#8217;re using <code>Flux<\/code> for Project Reactor and <code>Observable<\/code> for RxJava. These are fundamental data types in each library and a clear example of the differences in syntax and approach.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In conclusion, both Project Reactor and RxJava are excellent choices for implementing reactive programming in Java. The selection between the two often comes down to specific project requirements, team expertise, and the specific features that each library offers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Building a Reactive Application with Project Reactor and RxJava<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s build a simple RESTful API using Spring WebFlux along with both Project Reactor and RxJava. Our API will have a single endpoint that returns a stream of book titles. For this purpose, we&#8217;ll create a reactive book service that generates a stream of random book titles over a period of time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s start with setting up the dependencies.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Setting Up Dependencies<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For Maven, add the following dependencies to your <code>pom.xml<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">dependencies<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">dependency<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">groupId<\/span>&gt;<\/span>org.springframework.boot<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">groupId<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">artifactId<\/span>&gt;<\/span>spring-boot-starter-webflux<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">artifactId<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">dependency<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">dependency<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">groupId<\/span>&gt;<\/span>io.projectreactor<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">groupId<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">artifactId<\/span>&gt;<\/span>reactor-core<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">artifactId<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">dependency<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">dependency<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">groupId<\/span>&gt;<\/span>io.reactivex.rxjava3<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">groupId<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">artifactId<\/span>&gt;<\/span>rxjava<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">artifactId<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">version<\/span>&gt;<\/span>3.x.x<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">version<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">dependency<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">dependencies<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Now, let&#8217;s create the <code>Book<\/code> model, <code>BookService<\/code>, and <code>BookController<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating the Book Model<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Our Book model will be a simple class with a single field: <code>title<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Book<\/span> <\/span>{\n\n    <span class=\"hljs-keyword\">private<\/span> String title;\n\n    <span class=\"hljs-comment\">\/\/ Constructor, getters and setters<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Creating the Book Service<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>BookService<\/code> will generate a stream of <code>Book<\/code> objects. Here, we&#8217;ll create two methods, one using Project Reactor and the other using RxJava.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Using Project Reactor<\/h4>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">import<\/span> reactor.core.publisher.Flux;\n\n<span class=\"hljs-keyword\">import<\/span> java.time.Duration;\n<span class=\"hljs-keyword\">import<\/span> java.util.Random;\n\n<span class=\"hljs-meta\">@Service<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BookService<\/span> <\/span>{\n\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">final<\/span> List&lt;String&gt; bookTitles = Arrays.asList(<span class=\"hljs-string\">\"Book1\"<\/span>, <span class=\"hljs-string\">\"Book2\"<\/span>, <span class=\"hljs-string\">\"Book3\"<\/span>, <span class=\"hljs-string\">\"Book4\"<\/span>);\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Flux&lt;Book&gt; <span class=\"hljs-title\">getBookStreamReactor<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> Flux.interval(Duration.ofSeconds(<span class=\"hljs-number\">1<\/span>))\n                .map(i -&gt; <span class=\"hljs-keyword\">new<\/span> Book(bookTitles.get(<span class=\"hljs-keyword\">new<\/span> Random().nextInt(<span class=\"hljs-number\">4<\/span>))));\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Here, <code>Flux.interval(Duration.ofSeconds(1))<\/code> creates a Flux that emits a sequence of Longs every second. We then map each Long to a <code>Book<\/code> object with a random title.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Using RxJava<\/h4>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">import<\/span> io.reactivex.rxjava3.core.Observable;\n\n<span class=\"hljs-meta\">@Service<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BookService<\/span> <\/span>{\n\n    <span class=\"hljs-comment\">\/\/...<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Observable&lt;Book&gt; <span class=\"hljs-title\">getBookStreamRxJava<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> Observable.interval(<span class=\"hljs-number\">1<\/span>, TimeUnit.SECONDS)\n                .map(i -&gt; <span class=\"hljs-keyword\">new<\/span> Book(bookTitles.get(<span class=\"hljs-keyword\">new<\/span> Random().nextInt(<span class=\"hljs-number\">4<\/span>))));\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><code>Observable.interval(1, TimeUnit.SECONDS)<\/code> works similarly to <code>Flux.interval<\/code>, creating an Observable that emits a sequence of Longs every second.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating the Book Controller<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, we&#8217;ll create a <code>BookController<\/code> that exposes two endpoints. The <code>\/reactor<\/code> endpoint uses the Project Reactor-based method, while the <code>\/rxjava<\/code> endpoint uses the RxJava-based one.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">import<\/span> org.springframework.http.MediaType;\n<span class=\"hljs-keyword\">import<\/span> org.springframework.web.bind.annotation.GetMapping;\n<span class=\"hljs-keyword\">import<\/span> org.springframework.web.bind.annotation.RestController;\n<span class=\"hljs-keyword\">import<\/span> reactor.core.publisher.Flux;\n<span class=\"hljs-keyword\">import<\/span> io.reactivex.rxjava3.core.Observable;\n\n<span class=\"hljs-meta\">@RestController<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BookController<\/span> <\/span>{\n\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">final<\/span> BookService bookService;\n\n    <span class=\"hljs-comment\">\/\/ Constructor injection<\/span>\n\n    <span class=\"hljs-meta\">@GetMapping<\/span>(value = <span class=\"hljs-string\">\"\/reactor\"<\/span>, produces = MediaType.APPLICATION_STREAM_JSON_VALUE)\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Flux&lt;Book&gt; <span class=\"hljs-title\">getBooksReactor<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> bookService.getBookStreamReactor();\n    }\n\n    <span class=\"hljs-meta\">@GetMapping<\/span>(value = <span class=\"hljs-string\">\"\/rxjava\"<\/span>, produces = MediaType.APPLICATION_STREAM_JSON_VALUE)\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Observable&lt;Book&gt; <span class=\"hljs-title\">getBooksRxJava<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> bookService.getBookStreamRxJava();\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><code>MediaType.APPLICATION_STREAM_JSON_VALUE<\/code> allows Spring WebFlux to return the Book objects as they are generated, creating a streaming effect.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To test this API, start the application and access <code>http:\/\/localhost:8080\/reactor<\/code> and <code>http:\/\/localhost:8080\/rxjava<\/code> with a tool like curl. You should see a new book title being sent every second.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This example demonstrated how to create a reactive API with both Project Reactor and RxJava. Both approaches provide the same functionality, but as shown, they have different methods and objects. As always, the choice between the two should be based on your specific use case and requirements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reactive Programming Best Practices<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Reactive programming offers powerful mechanisms to write efficient, responsive, and resilient applications. However, to leverage its full potential, it&#8217;s important to follow some best practices and patterns. Let&#8217;s explore some of these:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Proper Error Handling<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In Reactive Programming, errors are just another type of event. They can be piped and transformed, just like data. It&#8217;s important to handle these errors appropriately so they don&#8217;t crash your application or leave it in an inconsistent state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Project Reactor and RxJava, you can use the <code>onErrorReturn<\/code>, <code>onErrorResume<\/code>, and <code>doOnError<\/code> methods to handle errors:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">Flux.range(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">10<\/span>)\n    .map(i -&gt; {\n        <span class=\"hljs-keyword\">if<\/span> (i == <span class=\"hljs-number\">5<\/span>) <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> RuntimeException(<span class=\"hljs-string\">\"Something went wrong!\"<\/span>);\n        <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">return<\/span> i;\n    })\n    .onErrorReturn(-<span class=\"hljs-number\">1<\/span>) <span class=\"hljs-comment\">\/\/ If an error occurs, emit -1 and complete the sequence.<\/span>\n    .subscribe(System.out::println, System.err::println);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">2. Utilizing Back-pressure<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Back-pressure is a key feature of Reactive Programming. It prevents slower consumers from being overwhelmed by faster producers. Both Project Reactor and RxJava provide mechanisms to control back-pressure.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance, in Project Reactor, you can control the demand from the subscriber side using the <code>request(n)<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">Flux.range(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">10<\/span>)\n    .doOnRequest(n -&gt; System.out.println(<span class=\"hljs-string\">\"Request of \"<\/span> + n))\n    .subscribe(<span class=\"hljs-keyword\">new<\/span> BaseSubscriber&lt;Integer&gt;() {\n        <span class=\"hljs-meta\">@Override<\/span>\n        <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">hookOnSubscribe<\/span><span class=\"hljs-params\">(Subscription subscription)<\/span> <\/span>{\n            request(<span class=\"hljs-number\">5<\/span>); <span class=\"hljs-comment\">\/\/ Only request 5 elements initially<\/span>\n        }\n\n        <span class=\"hljs-meta\">@Override<\/span>\n        <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">hookOnNext<\/span><span class=\"hljs-params\">(Integer value)<\/span> <\/span>{\n            System.out.println(<span class=\"hljs-string\">\"Cancelling after having received \"<\/span> + value);\n            cancel();\n        }\n    });<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, the subscriber only requests five items initially and then cancels the subscription.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Ensuring Non-blocking Code<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">One of the primary advantages of Reactive Programming is its non-blocking nature. However, it&#8217;s crucial to ensure that your code does not inadvertently introduce blocking calls. In Project Reactor and RxJava, you can offload blocking tasks to a separate thread using <code>publishOn<\/code> (Reactor) and <code>observeOn<\/code> (RxJava).<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">Flux.range(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">10<\/span>)\n    .publishOn(Schedulers.boundedElastic()) <span class=\"hljs-comment\">\/\/ Use a separate thread for the map operation<\/span>\n    .map(i -&gt; {\n        <span class=\"hljs-comment\">\/\/ Simulate a blocking call<\/span>\n        <span class=\"hljs-keyword\">try<\/span> {\n            Thread.sleep(<span class=\"hljs-number\">1000<\/span>);\n        } <span class=\"hljs-keyword\">catch<\/span> (InterruptedException e) {\n            e.printStackTrace();\n        }\n        <span class=\"hljs-keyword\">return<\/span> i * <span class=\"hljs-number\">2<\/span>;\n    })\n    .subscribe(System.out::println);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this code, <code>Schedulers.boundedElastic()<\/code> provides a separate thread for the <code>map<\/code> operation, which could potentially be a blocking call.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These practices can significantly improve the resilience and efficiency of your reactive applications. Always remember that reactive programming is not only about using certain libraries, but also about thinking in a certain way &#8211; thinking reactively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Challenges and Pitfalls in Reactive Programming<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">While Reactive Programming promises numerous benefits, it&#8217;s not without its share of challenges. Below, we will discuss some common pitfalls and how to navigate around them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Steep Learning Curve<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The shift from imperative to reactive programming is often not straightforward. It requires a change in mindset and the understanding of new programming constructs like streams, publishers, and subscribers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Mitigation:<\/strong> Start small and gradually refactor your applications to be reactive. Invest time in learning the fundamentals, such as the observer pattern, the reactive streams specification, and the core operators provided by your chosen library. Use online resources, like the official documentation for Project Reactor and RxJava, and engage with the community to learn from others&#8217; experiences.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Debugging Challenges<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Debugging reactive applications can be more complex due to their asynchronous and non-blocking nature. Stack traces may not be as useful since they only show the state of a different thread pool, not the sequence of operators that led to the error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Mitigation:<\/strong> Both Project Reactor and RxJava provide utilities to make debugging easier. For instance, Project Reactor&#8217;s <code>Hooks.onOperatorDebug()<\/code> and RxJava&#8217;s <code>RxJavaPlugins.setHook()<\/code> can provide more meaningful stack traces. Additionally, testing libraries like Reactor&#8217;s StepVerifier and RxJava&#8217;s TestObserver can help verify the behavior of your reactive chains.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Dealing with Legacy Blocking Code<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Incorporating reactive programming in a project with existing blocking code can be tricky. If not handled correctly, a single blocking call can negate the benefits of your reactive stack.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Mitigation:<\/strong> Encapsulate blocking calls and run them on separate threads using <code>subscribeOn<\/code> or <code>publishOn<\/code>. This can help you gradually transition to a fully non-blocking model. If possible, consider using non-blocking alternatives to your blocking APIs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Handling Back-pressure<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding and managing back-pressure is one of the more advanced aspects of reactive programming. Mismanagement can lead to out-of-memory errors or overwhelmed consumers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Mitigation:<\/strong> Both Project Reactor and RxJava provide strategies to deal with back-pressure, like buffering, dropping, and sampling. Understand these strategies and choose the one that best fits your use case.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Despite these challenges, it&#8217;s important to remember that Reactive Programming is a powerful paradigm that can help manage complexity and improve the performance of your applications. As you gain more experience with it, you&#8217;ll be better equipped to tackle these challenges and fully leverage its potential.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Reactive Programming is not merely a tool or a library \u2013 it&#8217;s a different way of thinking about programming. As more applications demand real-time, high-load processing, this paradigm becomes increasingly valuable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Defining Reactive Programming Reactive Programming is a paradigm shift from the traditional, imperative style of programming that has long dominated the software industry. Unlike its predecessor, which organizes programming logic around a sequence of commands, Reactive Programming is based on asynchronous data streams and the propagation of change. It encourages building systems that are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[5,4],"tags":[],"class_list":["post-602","post","type-post","status-publish","format-standard","category-java","category-programming-languages","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Reactive Programming in Java with Project Reactor and RxJava<\/title>\n<meta name=\"description\" content=\"While Reactive Programming is language-agnostic, this article will focus on its implementation using Java, which is used in enterprise apps\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.w3computing.com\/articles\/reactive-programming-java-project-reactor-rxjava\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Reactive Programming in Java with Project Reactor and RxJava\" \/>\n<meta property=\"og:description\" content=\"While Reactive Programming is language-agnostic, this article will focus on its implementation using Java, which is used in enterprise apps\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.w3computing.com\/articles\/reactive-programming-java-project-reactor-rxjava\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-13T22:49:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-23T16:21:17+00:00\" \/>\n<meta name=\"author\" content=\"w3compadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"w3compadmin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/reactive-programming-java-project-reactor-rxjava\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/reactive-programming-java-project-reactor-rxjava\\\/\"},\"author\":{\"name\":\"w3compadmin\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"headline\":\"Reactive Programming in Java with Project Reactor and RxJava\",\"datePublished\":\"2023-07-13T22:49:48+00:00\",\"dateModified\":\"2023-08-23T16:21:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/reactive-programming-java-project-reactor-rxjava\\\/\"},\"wordCount\":2405,\"commentCount\":0,\"articleSection\":[\"Java\",\"Programming Languages\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/reactive-programming-java-project-reactor-rxjava\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/reactive-programming-java-project-reactor-rxjava\\\/\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/reactive-programming-java-project-reactor-rxjava\\\/\",\"name\":\"Reactive Programming in Java with Project Reactor and RxJava\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\"},\"datePublished\":\"2023-07-13T22:49:48+00:00\",\"dateModified\":\"2023-08-23T16:21:17+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"description\":\"While Reactive Programming is language-agnostic, this article will focus on its implementation using Java, which is used in enterprise apps\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/reactive-programming-java-project-reactor-rxjava\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/reactive-programming-java-project-reactor-rxjava\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/reactive-programming-java-project-reactor-rxjava\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Articles Home\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programming Languages\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/programming-languages\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Reactive Programming in Java with Project Reactor and RxJava\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\",\"name\":\"Developer Articles Hub\",\"description\":\"\",\"alternateName\":\"Developer Articles\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\",\"name\":\"w3compadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1782562654\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1782562654\",\"contentUrl\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1782562654\",\"caption\":\"w3compadmin\"},\"sameAs\":[\"http:\\\/\\\/w3computing.com\\\/articles\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Reactive Programming in Java with Project Reactor and RxJava","description":"While Reactive Programming is language-agnostic, this article will focus on its implementation using Java, which is used in enterprise apps","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.w3computing.com\/articles\/reactive-programming-java-project-reactor-rxjava\/","og_locale":"en_US","og_type":"article","og_title":"Reactive Programming in Java with Project Reactor and RxJava","og_description":"While Reactive Programming is language-agnostic, this article will focus on its implementation using Java, which is used in enterprise apps","og_url":"https:\/\/www.w3computing.com\/articles\/reactive-programming-java-project-reactor-rxjava\/","article_published_time":"2023-07-13T22:49:48+00:00","article_modified_time":"2023-08-23T16:21:17+00:00","author":"w3compadmin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"w3compadmin","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/www.w3computing.com\/articles\/reactive-programming-java-project-reactor-rxjava\/#article","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/reactive-programming-java-project-reactor-rxjava\/"},"author":{"name":"w3compadmin","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"headline":"Reactive Programming in Java with Project Reactor and RxJava","datePublished":"2023-07-13T22:49:48+00:00","dateModified":"2023-08-23T16:21:17+00:00","mainEntityOfPage":{"@id":"https:\/\/www.w3computing.com\/articles\/reactive-programming-java-project-reactor-rxjava\/"},"wordCount":2405,"commentCount":0,"articleSection":["Java","Programming Languages"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.w3computing.com\/articles\/reactive-programming-java-project-reactor-rxjava\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.w3computing.com\/articles\/reactive-programming-java-project-reactor-rxjava\/","url":"https:\/\/www.w3computing.com\/articles\/reactive-programming-java-project-reactor-rxjava\/","name":"Reactive Programming in Java with Project Reactor and RxJava","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/#website"},"datePublished":"2023-07-13T22:49:48+00:00","dateModified":"2023-08-23T16:21:17+00:00","author":{"@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"description":"While Reactive Programming is language-agnostic, this article will focus on its implementation using Java, which is used in enterprise apps","breadcrumb":{"@id":"https:\/\/www.w3computing.com\/articles\/reactive-programming-java-project-reactor-rxjava\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.w3computing.com\/articles\/reactive-programming-java-project-reactor-rxjava\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.w3computing.com\/articles\/reactive-programming-java-project-reactor-rxjava\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Articles Home","item":"https:\/\/www.w3computing.com\/articles\/"},{"@type":"ListItem","position":2,"name":"Programming Languages","item":"https:\/\/www.w3computing.com\/articles\/programming-languages\/"},{"@type":"ListItem","position":3,"name":"Reactive Programming in Java with Project Reactor and RxJava"}]},{"@type":"WebSite","@id":"https:\/\/www.w3computing.com\/articles\/#website","url":"https:\/\/www.w3computing.com\/articles\/","name":"Developer Articles Hub","description":"","alternateName":"Developer Articles","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.w3computing.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561","name":"w3compadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1782562654","url":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1782562654","contentUrl":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1782562654","caption":"w3compadmin"},"sameAs":["http:\/\/w3computing.com\/articles"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"w3compadmin","author_link":"https:\/\/www.w3computing.com\/articles\/author\/w3compadmin\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/602","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/comments?post=602"}],"version-history":[{"count":10,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/602\/revisions"}],"predecessor-version":[{"id":612,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/602\/revisions\/612"}],"wp:attachment":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/media?parent=602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/categories?post=602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/tags?post=602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}