Java Spring WebFlux vs RxJava
Asked Answered
C

3

55

I’m starting to learn reactive programming in Java. The whole reactive paradigm is new to me.

In my learning process, i have come across few terms/libraries such as Spring WebFlux, projectreactor, and RxJava. I hope someone can explain what the differences are, and which one you used for your project and why you have made this choice.

I read that Spring WebFlux actually uses projectreactor's Flux/Mono internally. This simplified the question to pros and cons of Spring Webflux over RxJava?

Carrigan answered 5/6, 2019 at 13:14 Comment(1)
i did read about them but i have hard time deciding and that is why i post this question.Carrigan
C
29

In general, RxJava support project which based on JDK8- and Project Reactor supports JDK 8+. But for a beginner, you can learn RxJava at first. Since Project Reactor you can consider it fix the drawbacks in RxJava and more suitable for Backend development. RxJava has too many problems which can cause Out of Memory if you can't use it well. But in final, if you want to use Spring 5.2+ very well, you need to learn from RxJava->Reactor->NIO->Netty->Reactor netty.

Cassaba answered 6/6, 2019 at 15:16 Comment(0)
H
34

Reactive programming is a programming paradigm, but I wouldn’t call it new. It’s actually been around for a while. Just like object-oriented programming, functional programming, or procedural programming, reactive programming is another programming paradigm.

Paradigm is defined by Reactive Manifesto

Reactive Streams, on the other hand, is a specification.

For Java programmers, Reactive Streams is an API. It is the product of a collaboration between engineers from Kaazing, Netflix, Pivotal, Red Hat, Twitter, Typesafe, and many others. Reactive Streams is much like JPA or JDBC. Both are API specifications.

The Reactive Streams API consists of just 4 high interfaces.

  1. Publisher : A publisher is a provider of a potentially unbounded number of sequenced elements, publishing them according to the demand received from its Subscribers.
  2. Subscriber : Will receive call to Subscriber.onSubscribe(Subscription) once after passing an instance of Subscriber to Publisher. subscribe(Subscriber).
  3. Subscription : A Subscription represents a one-to-one lifecycle of a Subscriber subscribing to a Publisher.
  4. Processor : A Processor represents a processing stage—which is both a Subscriber and a Publisher and obeys the contracts of both.

These concepts take different manifestations at different levels and areas. For eg. A java dev could think about how to program his at the application level, a database engineer could think how a database could react to reactive API calls (For example, Mongo DB has implemented a Reactive Streams driver), a network programmer could think how reactive calls could be made effective at the network level.

Some of the JVM-based frameworks that follow the reactive streams spec are

  • Akka Streams framework
  • Ratpack
  • Vert.x
  • ReactiveX (RxJava 2.x and Reactor)
  • Java 1.9 Flow classes (You’ll notice that the Reactive Streams interfaces move under the Flow class in Java 9)

ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming. It extends the observer pattern to support sequences of data and/or events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety, concurrent data structures, and non-blocking I/O.

RxJava 2.0 and Reactor are based on ReactiveX project. And Spring WebFlux uses Reactor internally.

Hives answered 6/9, 2020 at 3:10 Comment(0)
C
29

In general, RxJava support project which based on JDK8- and Project Reactor supports JDK 8+. But for a beginner, you can learn RxJava at first. Since Project Reactor you can consider it fix the drawbacks in RxJava and more suitable for Backend development. RxJava has too many problems which can cause Out of Memory if you can't use it well. But in final, if you want to use Spring 5.2+ very well, you need to learn from RxJava->Reactor->NIO->Netty->Reactor netty.

Cassaba answered 6/6, 2019 at 15:16 Comment(0)
A
17

Both Spring WebFlux (project-reactor) and RxJava2+ are implementation of reactive-streams.

Spring is using project-reactor, therefore it is more supported, advertised and has bigger community, so I would go with that.

Andrewandrewes answered 6/6, 2019 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.