Reactive programming is a developer paradigm focused on creating applications that are
responsive to changes, asynchronous in nature, and driven by events. It emphasizes handling data
streams and events as first-class citizens, enabling developers to write more efficient,
scalable, and resilient code.
Java reactive programming is a programming approach that emphasizes the development of
applications that are both responsive and scalable, capable of handling both concurrent and
asynchronous operations effectively.
Producer:The producer acts as a data source and it will publish events.
Subscriber: It listens to the events from the producer, it
subscribes and consumes those events.
Processor: It acts as a middleman that connects publisher to
subscriber.
Stream: It is a series of events that are ordered in time. A stream
can emit three different types of signals: A value of a certain type, an error, or a completed
signal.
Reactive Streams: A standard for asynchronous stream processing
with non-blocking backpressure.
Flux and Mono: Specific types of observables provided by Project
Reactor for handling streams of data. Flux represents a stream of multiple items, while Mono
represents a stream of at most one item.
Backpressure: The mechanism used to handle situations where the
rate of data production exceeds the rate of data consumption.
Asynchronous Data Stream: A stream of data that emits values one at
a time with a delay between them is known as an asynchronous data stream.
@Table("products") public class Product { @Id private Long id; private String name; private String description; private double value; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public double getValue() { return value; } public void setValue(double value) { this.value = value; } public Product() { super(); } public Product(Long id, String name, String description, double value) { super(); this.id = id; this.name = name; this.description = description; this.value = value; } }
@RestController public class ProductController { @Autowired ProductService productService; @GetMapping("/allproducts") public FluxgetAllProducts() { return productService.getAllProducts(); } @GetMapping("/getproductbyid/{id}") public Mono getProductById(@PathVariable Long id) { return productService.getProductById(id); } @DeleteMapping("/deleteproductbyid{id}") public Mono deleteProductById(@PathVariable Long id) { return productService.deleteProduct(id); } @PostMapping("/saveproduct") public Mono saveProduct(@RequestBody Product product) { return productService.saveProduct(product); } }
import org.springframework.data.repository.reactive.ReactiveCrudRepository; public interface ProductRepository extends ReactiveCrudRepository{ }
@Service public class ProductService { @Autowired ProductRepository productRepository; public FluxgetAllProducts() { return productRepository.findAll(); } public Mono getProductById(Long id) { return productRepository.findById(id); } public Mono deleteProduct(Long id) { return productRepository.deleteById(id); } public Mono saveProduct(Product product) { product.setId(null); return productRepository.save(product); } }
4.0.0 org.springframework.boot spring-boot-starter-parent 3.1.5 com.ill.test sqlrx 0.0.1-SNAPSHOT sqlrx MySQL Rx Test UTF-8 UTF-8 17 1.0.1 org.springframework.boot spring-boot-starter-webflux org.springframework.boot spring-boot-starter-data-r2dbc io.asyncer r2dbc-mysql ${mysql-driver.version} org.projectlombok lombok true org.springframework.boot spring-boot-devtools runtime true org.springframework.boot spring-boot-starter-test test io.projectreactor reactor-test test clean package org.apache.maven.plugins maven-enforcer-plugin enforce-maven enforce 3.6.3 org.apache.maven.plugins maven-compiler-plugin ${project.build.sourceEncoding} ${java.version} -Xlint:all true true org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
spring.r2dbc.url=r2dbc:mysql://localhost:3306/reactive spring.r2dbc.username=root spring.r2dbc.password=root logging.level.org.springframework.data.repository=DEBUG logging.level.org.springframework.r2dbc.core=DEBUG