What is Web?

  • Web” generally refers to the traditional way of building web applications using servlets and other standard Java EE technologies.
    In Spring Framework, the web module (spring-web) is responsible for handling web-related features like HTTP requests, responses, and MVC (Model-View-Controller) architecture.
  • The servlet-based approach of Spring Web is suitable for most applications and provides synchronous, blocking operations, where each request is handled by a dedicated thread from a thread pool.
  • It is suitable for apps that follow a synchronous, blocking request-response model, where each incoming request is handled y a dedicated thread until it gets a response.it have simpler and straightforward request handling pattern.

    What is Webflux?

  • WebFlux is a module introduced in Spring Framework 5, designed for reactive programming in web applications.
  • Reactive programming is an asynchronous, non-blocking programming paradigm that allows applications to handle a large number of concurrent users efficiently.
  • WebFlux is built on top of Reactor, which is a reactive library, and it provides support for both Reactive Streams and Project Reactor.
  • WebFlux offers two programming models: Annotated Controllers (similar to Spring Web’s MVC) and Functional Endpoints (a more functional, declarative style of defining request handling).
  • It’s particularly beneficial for building highly scalable, event-driven, and responsive applications, such as real-time applications and APIs.
  • This asynchronous processing pipelines are quite similar with java stream APIs of collection class.

Consider below example

List<Integer> nums= Arrays.asList(1,2,3,4,5,6,7,8,9,10);
for(int I = 0; I < nums.size(); i++){
System.out.println(nums.get(i));
}

With Stream API
List<Integer> nums= Arrays.asList(1,2,3,4,5,6,7,8,9,10);
Nums.forEach(num -> System.out.println(num) );

With Reactive Stream API

⦁ The Reactive Streams API is designed for handling asynchronous and non-blocking data streams with backpressure support. It enables the development of reactive and event-driven applications that can handle large datasets and concurrent processing efficiently.
⦁ Reactive Streams handle data asynchronously and follow the observer pattern, where data is pushed from the publisher to the subscriber.
⦁ Example Time Complexity: The time complexity of Reactive Streams operations depends on the specific use case and how it’s implemented. Reactive Streams are more focused on efficient handling of data emissions and backpressure, rather than typical time complexity concerns associated with synchronous data processing.

Stream API in Java collections and Reactive Operators in Reactive Streams?

The main difference between the Stream API in Java collections and Reactive Operators in Reactive Streams lies in their approach and capabilities for handling asynchronous and event-driven data processing:
Difference between Stream API in Java collections and Reactive Streams
1. Synchronous vs. Asynchronous:
Stream API in Java collections processes data synchronously, meaning the operations are performed on each element sequentially.
Reactive Operators in Reactive Streams handle data asynchronously, enabling non-blocking processing and the ability to handle backpressure when dealing with large datasets or slow consumers.
2. Pull vs. Push:
Stream API uses a “pull” approach, where the consumer pulls data from the producer. The consumer dictates the pace of data processing.
Reactive Operators use a “push” approach, where the producer pushes data to the consumer. The producer controls the rate of data emission, and the consumer reacts accordingly.
3. Backpressure handling:
Stream API does not directly support backpressure handling, which means that if the producer generates data faster than the consumer can process, it can lead to issues like OutOfMemoryError.
Reactive Operators in Reactive Streams offer built-in backpressure handling. The consumer can signal the producer to slow down or stop data emission when it’s overwhelmed.
4. Use cases:
Stream API is suitable for synchronous processing of in-memory collections and small datasets.
Reactive Operators are designed for handling asynchronous and event-driven scenarios, especially in applications dealing with large datasets, real-time data streams, or distributed systems.
Reactive Operators provide a more flexible and efficient way to handle asynchronous data streams with backpressure support, making them well-suited for reactive and event-driven programming paradigms. On the other hand, the Stream API in Java collections is more focused on traditional in-memory collection processing.

Reactive Streams are based on the Observer pattern?

There are 2 types of patterns
⦁ Iterator pattern : provides a way to sequential access elements of a collection. Iterator provides methods like next(), hasNext() and sometimes remove() to navigate through the collection.
⦁ Observer pattern : when subject’s state changes, it notifies all registered observers, and they react accordingly. This pattern is used for one-to many communication where a single subjects informs multiple observers.

When mouse click event or any event happens it notifies the observer?

clicksChannel.filter(event -> event != null).addObserver(event-> System.out.println(num) );
With Reactive Programming
clicksChannel.filter(event -> event != null).subscribe(event-> System.out.println(num) );

Click here to know more WEB&WEBFLUX?

Leave a Reply

Your email address will not be published. Required fields are marked *