Reactive Programming in Java with Project Reactor and Spring WebFlux
Jul 29, 2025 am 12:04 AMResponsive programming implements high concurrency, low latency non-blocking services in Java through Project Reactor and Spring WebFlux. 1. Project Reactor provides two core types: Mono and Flux, supports declarative processing of asynchronous data flows, and converts, filters and other operations through operator chains; 2. Spring WebFlux is built on Reactor, supports two programming models of annotation and functional. It runs on non-blocking servers such as Netty, and can efficiently handle large amounts of concurrent connections; 3. Using WebFlux Reactor can improve concurrency capabilities and resource utilization in I/O-intensive scenarios, naturally supports streaming communications such as SSE and WebSocket, and seamlessly integrates with responsive databases such as R2DBC and MongoDB; 4. In actual applications, blocking code should be avoided, business logic should not be written in subscribe, and timeout and error guarantee mechanisms should be used reasonably; 5. Although the learning curve is steep and not suitable for CPU-intensive tasks, this combination can significantly improve system throughput and resilience in microservices, gateways, and real-time systems, and is worth adopting in suitable scenarios.
Reactive programming has become increasingly important in Java in recent years, especially in modern web applications with high concurrency and low latency. With Project Reactor and Spring WebFlux , Java developers can easily build non-blocking, event-driven services. Let's take a look at how they work together and why you should consider using them.

What is responsive programming?
Responsive programming is a programming paradigm for data flow and change propagation . It allows you to handle asynchronous data streams in a declarative manner, such as HTTP requests, database responses, message queues, etc.
In traditional blocking programming, each request occupies one thread and waits for the I/O operation to complete. In responsive programming, threads are not blocked, but are processed only when data is available through the "callback" or "publish-subscribe" mechanism.

Project Reactor: The core library for responsive programming
Project Reactor is a basic JVM library created by the Spring team for responsive systems. It is an implementation of Reactor 3 and is fully compatible with the Reactive Streams specification.
It provides two core types:

-
Mono<t></t>
: represents an asynchronous sequence of 0 or 1 element (for example: single user query results) -
Flux<t></t>
: represents an asynchronous data stream of 0 to N elements (for example: get all users)
// Example: Create a Flux Flux<String> names = Flux.just("Alice", "Bob", "Charlie"); // Create a Mono Mono<String> single = Mono.just("Hello");
You can chain these streams, such as map
, filter
, flatMap
, delayElements
, etc.:
names .map(String::toUpperCase) .delayElements(Duration.ofMillis(500)) .subscribe(System.out::println);
Note: The above code is non-blocking and will not be executed immediately, but "declare" the behavior of the data flow and will not be triggered until a "subscriber" appears.
Spring WebFlux: Responsive Web Framework
Spring WebFlux is a responsive web framework introduced by Spring 5. It can replace traditional Spring MVC and supports two programming models:
Annotated controller (similar to Spring MVC):
@RestController public class UserController { @GetMapping("/users") public Flux<User> getAllUsers() { return userService.findAll(); // Return Flux<User> } @GetMapping("/users/{id}") public Mono<User> getUserById(@PathVariable String id) { return userService.findById(id); // Return Mono<User> } }
Functional routing model (lightweight, functional):
@Bean public RouterFunction<ServerResponse> route(UserHandler handler) { return route(GET("/users"), handler::getAllUsers) .andRoute(GET("/users/{id}"), handler::getUserById); }
WebFlux supports non-blocking servers such as Netty and Undertow, Netty is recommended (the default is embedded in Spring Boot to use Reactor Netty).
Why use WebFlux Reactor?
Higher concurrency capability
Traditional Tomcat occupies one thread per request, and the thread pool is limited. WebFlux uses a small number of event loop threads (Event Loops) that can handle thousands of concurrent connections.Better resource utilization
Non-blocking I/O reduces thread waiting, making CPU and memory usage more efficient.Natural support for streaming data
Such scenarios as SSE (Server-Sent Events), WebSocket, file flow, real-time push, etc. are very suitable.Seamless integration with Spring Eco
Supports responsive MongoDB, PostgreSQL (via R2DBC), Redis (Lettuce), Kafka, etc.
Recommendations and precautions for use
Although the advantages of responsive programming are obvious, it is not a silver bullet. Here are a few key points:
- ? Suitable for I/O-intensive scenarios : such as calling external APIs, database access, and message processing.
- ?Not suitable for CPU-intensive tasks : Responsiveness does not improve computing performance, but may increase complexity.
- ??Don't mix blocking code : calling
Thread.sleep()
or blocking database access inMono/Flux
will destroy the responsive advantage. - ? Avoid writing business logic in subscribe : try to use operator chain processing to maintain functional style.
- ? The learning curve is steeper : the concepts of backpressure, scheduler, hot and cold flow require time to master.
Practical example: Calling external API
Suppose we want to obtain user information from an external service and do the conversion:
@Service public class UserService { private final WebClient webClient = WebClient.create("https://api.example.com"); public Mono<User> fetchUser(String id) { return webClient.get() .uri("/users/{id}", id) .retrieve() .bodyToMono(ExternalUser.class) .map(this::convertToUser) .timeout(Duration.ofSeconds(3)) // Timeout control.onErrorReturn(User.empty()); // Error guarantee} }
The entire process is non-blocking, and multiple requests can be processed in parallel without consuming additional threads.
Summarize
- Project Reactor provides basic operations for responsive streams (
Mono
/Flux
) - Spring WebFlux is built on Reactor to implement responsive Web services
- Combination of the two, suitable for high concurrency, I/O-intensive applications
- When using it, you need to avoid blocking operations and understand the backpressure and scheduling mechanism
Responsive programming is not necessary everywhere, but in the right scenario, it can significantly improve the system's throughput and resilience. If you are building a microservice, gateway, real-time interface, or stream processing system, WebFlux Reactor deserves serious consideration.
Basically all is it, not complicated but it is easy to ignore details.
The above is the detailed content of Reactive Programming in Java with Project Reactor and Spring WebFlux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Laravel supports the use of native SQL queries, but parameter binding should be preferred to ensure safety; 1. Use DB::select() to execute SELECT queries with parameter binding to prevent SQL injection; 2. Use DB::update() to perform UPDATE operations and return the number of rows affected; 3. Use DB::insert() to insert data; 4. Use DB::delete() to delete data; 5. Use DB::statement() to execute SQL statements without result sets such as CREATE, ALTER, etc.; 6. It is recommended to use whereRaw, selectRaw and other methods in QueryBuilder to combine native expressions to improve security

Use JUnit5 and Mockito to effectively isolate dependencies for unit testing. 1. Create a mock object through @Mock, @InjectMocks inject the tested instance, @ExtendWith enables Mockito extension; 2. Use when().thenReturn() to define the simulation behavior, verify() to verify the number of method calls and parameters; 3. Can simulate exception scenarios and verify error handling; 4. Recommend constructor injection, avoid over-simulation, and maintain test atomicity; 5. Use assertAll() to merge assertions, and @Nested organizes the test scenarios to improve test maintainability and reliability.

Go generics are supported since 1.18 and are used to write generic code for type-safe. 1. The generic function PrintSlice[Tany](s[]T) can print slices of any type, such as []int or []string. 2. Through type constraint Number limits T to numeric types such as int and float, Sum[TNumber](slice[]T)T safe summation is realized. 3. The generic structure typeBox[Tany]struct{ValueT} can encapsulate any type value and be used with the NewBox[Tany](vT)*Box[T] constructor. 4. Add Set(vT) and Get()T methods to Box[T] without

table-layout:fixed will force the table column width to be determined by the cell width of the first row to avoid the content affecting the layout. 1. Set table-layout:fixed and specify the table width; 2. Set the specific column width ratio for the first row th/td; 3. Use white-space:nowrap, overflow:hidden and text-overflow:ellipsis to control text overflow; 4. Applicable to background management, data reports and other scenarios that require stable layout and high-performance rendering, which can effectively prevent layout jitter and improve rendering efficiency.

json.loads() is used to parse JSON strings into Python data structures. 1. The input must be a string wrapped in double quotes and the boolean value is true/false; 2. Supports automatic conversion of null→None, object→dict, array→list, etc.; 3. It is often used to process JSON strings returned by API. For example, response_string can be directly accessed after parsing by json.loads(). When using it, you must ensure that the JSON format is correct, otherwise an exception will be thrown.

Choosetheappropriateindextypebasedonusecase,suchassinglefield,compound,multikey,text,geospatial,orTTLindexes.2.ApplytheESRrulewhencreatingcompoundindexesbyorderingfieldsasequality,sort,thenrange.3.Designindexestosupportcoveredqueriesbyincludingallque

Maven is a standard tool for Java project management and construction. The answer lies in the fact that it uses pom.xml to standardize project structure, dependency management, construction lifecycle automation and plug-in extensions; 1. Use pom.xml to define groupId, artifactId, version and dependencies; 2. Master core commands such as mvnclean, compile, test, package, install and deploy; 3. Use dependencyManagement and exclusions to manage dependency versions and conflicts; 4. Organize large applications through multi-module project structure and are managed uniformly by the parent POM; 5.

VSCode portable mode can be implemented by downloading the ZIP version and correctly configuring it. 1. Download the Windows ZIP version and unzip it to the specified folder; 2. Create a data folder in the same level directory for storing configuration and extensions; 3. Create a batch script to set user-data-dir and extensions-dir to point to the data directory; 4. Run the script to start VSCode and verify that the settings and plug-ins are saved in data; after success, you can carry it with you, leaving no traces when used on different computers. Note that only Windows supports this mode and cannot use the installation version.
