Spring Webflux Interview Questions and Answers

More Spring WebFlux related Topics,

1. What is Reactive Programming?

Reactive programming is a programming paradigm that deals with asynchronous data streams and the specific propagation of change, which means it implements modifications to the execution environment in a certain order.

The primary benefits of reactive programming are:
  • Incremented utilization of computing resources on multicore and multi-CPU hardware
  • Incremented performance by truncating serialization


2. What is Spring Web flux?

Spring WebFlux is Spring's reactive-stack web framework, and it's an alternative to Spring MVC. Spring WebFlux is a web framework that’s built on top of Project Reactor, to give you asynchronous I/O, and sanction our application to perform better.
Reactive web programming is great for applications that have streaming data and clients that consume it and stream it to their users. It ain’t great for developing CRUD apps.

3. What are the advantages of Spring Webflux?

A non-blocking approach that makes it possible to handle concurrency with a small number of threads and to scale effectively,
Functional programming, which avails inscribe more declarative code with the utilization of fluent APIs.

4. What is a Publisher?

The publisher is the source that will send the data to one or more subscribers.
A Publisher (for example Publisher<Object>) can return zero or multiple, possibly infinite, results. To make it more clear how many results we can expect, Project Reactor  introduced two implementations of Publisher:
  • A Mono<T>, which can either return zero or one result afore consummating,
  • And a Flux<T>, which can return zero to many, possibly infinite, results afore consummating.


5. What is Mono?

The first type of publisher is a Mono. The Mono API sanctions us to emit only a single value, after which it will immediately consummate. This denotes that the Mono is the reactive obverse of returning a simple object or an Optional.
 
Mono, Returns 0 or 1 element.
Mono<String> mono = Mono.just("Knowledgefactory.net");
Mono<String> mono = Mono.empty();
 

6. What is Flux?

Returns 0…N elements. A Flux can be illimitable, designating that it can keep emitting elements forever. Withal it can return a sequence of elements and then send a completion notification when it has returned all of its elements.
 
Flux<String> flux = Flux.just("Java", "Kotlin", "C");
Flux<String> flux = Flux.fromArray(new String[]{"Java", "Kotlin", "C"});
Flux<String> flux = Flux.fromIterable(Arrays.asList("Java", "Kotlin", "C"));
//To subscribe call method
flux.subscribe();
 

7. What is a Subscriber?

A subscriber will subscribe itself to a publisher, will designate how much data the publisher may send, and will process the data.
 
It has four methods to handle various kinds of responses received.
 
public interface Subscriber<T>
{
public void onSubscribe(Subscription s);
public void onNext(T t);
public void onError(Throwable t);
public void onComplete();
}
 

8. What is a Subscription?

Defines a one-to-one relationship between a Publisher and a Subscriber. It can only be used once by a single Subscriber. It is utilized to both signal desire for data and sanctions resource cleanup.
 
public interface Subscription<T>
{
public void request(long n);
public void cancel();
}
 

9. What is Processor?

Represents a processing stage consisting of both a Subscriber and a Publisher and obeys the contracts of both.

public interface Processor<T, R> extends Subscriber<T>, Publisher<R>
{
}
 

10. What Is the Use of WebClient and WebTestClient?

WebClient is a component that can act as a reactive client for performing non-blocking HTTP requests. Being a reactive client, it can handle reactive streams with backpressure, and it can thoroughly capitalize on Java lambdas. It can withal handle both sync and async scenarios.
WebTestClient class is used for testing. It can connect to any server over an HTTP connection. It can additionally bind directly to WebFlux applications utilizing mock request and replication objects, without the desideratum for an HTTP server.
 

11. What is bodyToMono?

The method bodyToMono() extracts the body to a Mono instance. The method Mono.block() subscribes to this Mono instance and blocks until the response is received.

public class WebClientExample {
public static void main(String[] args) throws InterruptedException {
WebClient webClient = WebClient.create("http://localhost:8088");
Mono<String> str = webClient.get()
.retrieve()
.bodyToMono(String.class);
String str = result.block();
System.out.println(str);
}
}
 

12. What is bodyToFlux?

For multiple items, use bodyToFlux. It emits 0-N items.
 
public Mono<Score<User>> getUsers(String user_id) {

return webClient
.get()
.uri(uriBuilder -> uriBuilder
.path("/users/{0}")
.build(id))
.retrieve()
.onStatus(HttpStatus::isError, resp -> resp.createException()
.map(WebClientGraphqlException::new)
.flatMap(Mono::error)
).bodyToFlux(User.class).collect(Collectors.toList());
}


13. What is the difference between Spring MVC Async and Spring Webflux?

  • Spring Async I/O model during its communication with the client is blocking. It may cause a performance quandary with slow clients. On the other hand, Spring WebFlux provides a non-blocking I/O model.
  • Reading the request body or request components is blocking in Spring Async, whiles it is non-blocking in Spring WebFlux.
  • In Spring Async, Filters and Servlets are working synchronously, but Spring WebFlux fortifies full asynchronous communication.
  • Spring WebFlux is compatible with wider ranges of Web/Application servers than Spring Async, like Netty, and Undertow. 

More Java interview questions and answers...

Popular posts from this blog

Spring boot video streaming example-HTML5

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

DataTable-Pagination example with Spring boot, jQuery and ajax

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Node JS mini projects with source code - free download

Spring boot web project free download:User Registration System

Spring Boot + Apache Commons Export Data to CSV Example