What is Reactive Programming?
Reactive Programming = "Don't wait. React when data arrives."
In normal (blocking) programming:
- You ask for data
- Your code waits
- When data comes, the code continues
In Reactive Programming:
- You say: "Hi, tell me when data is ready"
- Your code does not wait
- When data arrives, your code reacts
Think of it like:
- Normal: You call someone and wait on the line
- Reactive: You leave your number and get a callback
This is great for:
- Web APIs
- WebSockets
- Streaming data
- High-traffic systems
What is Reactor?
Reactor = a Java library that implements reactive programming
- Made by the Spring team
- Used in Spring WebFlux
- Handles async, non-blocking streams
So:
- Reactive Programming -> concept
- Reactor -> Tool to use that concept in Java
Reactor mainly gives you:
MonoFlux
What is Publisher?
Publisher = something that sends data
This comes from the Reactive Streams specification.
A Publisher:
- Produces data
- Sends data to subscribers
- Can send:
- 0 items
- 1 item
- many items
- or an error
You usually don't implement Publisher itself.
Reactor already does that.
Mono and Flux are Publishers
What is Mono?
Mono = a Publisher that emits at most ONE value
Possible results:
- 1 value
- no value
- error
Examples:
- Get one user by ID
- Get account balance
- Call an external API that returns one result
Example:
Mono<String> mono = Mono.just("Hello");
Think of Mono as:
"I promise to give you one result later (or nothing)."
What is Flux?
Just for completeness:
Flux = 0 to many values
Example:
- Price stream
- WebSocket messages
- List of users
Flux<Integer> flux = Flux.just(1, ,2, 3);
Top comments (0)