Reactive Streams
Reactive Streams is a specification for asynchronous stream processing with non-blocking back pressure. IntelliJ IDEA provides support for the following non-blocking reactive Java frameworks based on Reactive Streams:
The support for Reactive applications includes code completion, inspections, quick-fixes, and a dedicated debug mode.
Add Reactor support
There are several options for adding Reactor support to your project: you can add the necessary dependencies to the build file of Maven or Gradle projects, or you can manually download the Reactor library. For Spring Boot applications, IntelliJ IDEA allows you to add the required dependencies when you create a new project.
Add Reactor to a Maven project
For Maven projects, it's recommended that you import the Bill of Materials (BOM) together with the core, as it ensures that Reactor components work well together.
In the
pom.xml
file, add the following dependency to import the BOM:<dependencyManagement> <dependencies> <dependency> <groupId>io.projectreactor<groupId> <artifactId>reactor-bom<artifactId> <version>Dysprosium-SR1<version> <type>pom</type> <scope>import<scope> <dependency> <dependencies> <dependencyManagement>Add the following dependency to import Reactor:
<dependencies> <dependency> <groupId>io.projectreactor<groupId> <artifactId>reactor-core<artifactId> <dependency> <dependencies>
Add Reactor to Gradle (Gradle 5.0 and later)
For Gradle projects, it's recommended that you import the Bill of Materials (BOM) together with the core, as it ensures that Reactor components work well together.
In the
build.gradle
, add the following dependencies:dependencies { // import BOM implementation platform('io.projectreactor:reactor-bom:Dysprosium-SR1') // add dependencies without a version number implementation 'io.projectreactor:reactor-core' }
Add Reactor to Gradle (Gradle 4.x and earlier)
For Gradle projects, it's recommended that you import the Bill of Materials (BOM) together with the core, as it ensures that Reactor components work well together.
Previous Gradle versions don't support the BOM, however, you can use the Spring gradle-dependency-management plugin to import it to your project.
Obtain the plugin from Gradle Plugin Portal by adding the following code to
build.gradle
:plugins { id "io.spring.dependency-management" version "1.0.6.RELEASE" }Import the BOM by adding the following code:
dependencyManagement { imports { mavenBom "io.projectreactor:reactor-bom:Dysprosium-SR1" } }Add Reactor support:
dependencies { compile 'io.projectreactor:reactor-core' }
Add the Reactor library to a project
If you build your project with the native IDE builder, you can add Reactor support as a library.
From the main menu, select
Ctrl+Alt+Shift+S or click on the toolbar.Select Libraries, click , and then select From Maven.
In the dialog that opens, specify the library artifact
io.projectreactor:reactor-core:jar:3.3.0.RELEASE
and click OK.
Create a new Spring Boot project with Reactor
Launch IntelliJ IDEA.
If the Welcome screen opens, click New Project.
Otherwise, from the main menu, select
.From the Project SDK list, select the JDK that you want to use in your project.
If the JDK is installed on your computer, but not defined in the IDE, select Add JDK and specify the path to the JDK home directory.
If you don't have the necessary JDK on your computer, select Download JDK.
Enter the URL of the Initializr service that you want to use, or leave the default one.
Click Next.
Configure project metadata: select a language (Java or Kotlin), a build tool (Maven or Gradle), and specify an artifact ID and version.
If you want to build your project on a Java version different from your project JDK version, you can select it here.
Click Next.
On the Dependencies page, select Spring Reactive Web and click Next.
If necessary, change the default project location and click Finish.
Reactor debug mode
Reactor includes assembly-time instrumentation that is designed for debugging asynchronous code. When you enable the Reactor debug mode, you can get a more convenient view of the stack trace after the program has been suspended. You can examine the frames and understand why particular parameters were passed to a method in a Reactive application.
IntelliJ IDEA is aware of the Reactor debug mode and shows the traceback to the failed operation on the Frame tab of the Debug tool window. Moreover, IntelliJ IDEA can enable the debug mode without any changes in your code, making the necessary calls as you run the debugging session.
By default, the Reactor debug mode is enabled. This is convenient during development, testing, and debugging, but generally impacts the performance of the application.
Configure the Reactor debug mode
In the Settings/Preferences dialog Ctrl+Alt+S, select .
Configure the necessary options and click OK to apply the new settings.
Enable Reactor Debug mode Instruct IntelliJ IDEA to initialize the debug mode. If you have the Reactor debug mode enabled, select the initialization method:
Hooks.onOperatorDebug(): This method captures the stack trace on every operator, which is slow and requires a lot of resources. Do not use this method in production. For more information, see Activating Debug Mode.
ReactorDebugAgent.init(): Use this Java agent to debug exceptions in your application without much runtime impact. For more information, see Production-ready Global Debugging.
Additionally select Notify if cannot call ReactorDebugAgent.init() if you want to show a notification when the agent is not available.
None: Select this option if you don't have global debugging enabled and use the
checkpoint()
operator in your code for a more fine-grained approach. For more information, see The checkpoint() Alternative.
Advanced Reactor inspection
IntelliJ IDEA includes the Inappropriate thread-blocking method call inspection that detects inappropriate thread-blocking method calls in code fragments where a thread should not be blocked. Reactor support also adds an option that enables IntelliJ IDEA to locally understand on which thread the operator will be executed by processing the subscribeOn()
and publishIn()
operators.
The option is enabled by default, and if you want to disable it, do the following:
In the Settings/Preferences dialog Ctrl+Alt+S, select .
Clear the Use advanced analysis to detect non-blocking scopes checkbox and click OK to apply the new settings.
Postfix completion in Reactor
IntelliJ IDEA provides postfix code completion for projects that use Reactor. Postfix completion can transform an already-typed expression to a different one based on what you have typed.
In projects with Reactor support, the IDE can wrap an expression with a suitable reactor.core.publisher.Flux
factory or a reactor.core.publisher.Mono
factory.
Video tutorial
The following video demonstrates how to create a working Reactive Spring Boot application in IntelliJ IDEA: