Introduction: Mr. Tu Zipei, known as “China’s first Big data”, mentioned in his famous book Top of Data that Moore’s Law, social media and data mining are the three causes of big data. According to the IBM study, 90 percent of all data obtained by the entire human civilization has been generated in the past two years. In this context, a number of new technologies have emerged, including NoSQL, Hadoop, Spark, Storm, and Kylin. Among them, Reactive programming technology represented by RxJava and Reactor is aimed at Velocity in the classic definition of big data 4V (Volume, Variety, Velocity, Value), namely, high concurrency problem. In the upcoming Release of Spring 5, Responsive programming support has also been introduced. Over the next few weeks, I’ll be sharing some of my lessons on responsive programming in three installments. As a first article, I’ll start with Spring 5.

1 Responsive declaration

Like the Agile manifesto, when it comes to responsive programming, you must first mention the responsive manifesto.

We want systems that are Responsive, Resilient, Elastic and Message Driven. We call these Reactive Systems. – The Reactive Manifesto

Photo credit:The Reactive Manifesto

In homage to the Agile Manifesto, the responsive Manifesto also contains four sets of keywords:

  • -Jenny: Hey, we’re Responsive. The system is required to be as responsive as possible at all times.
  • Resilient: Resilient. The system is required to remain responsive even when things go wrong.
  • They’re Elastic. The system is required to remain responsive under various loads.
  • Message Driven: Message Driven. The system is required to connect components via asynchronous messages.

As you can see, for any responsive system, the first thing to ensure is responsiveness, otherwise it is not a responsive system. In this sense, the Windows system, prone to blue screens, is clearly not a responsive system.

PS: If you agree with the responsive manifesto, please go to the official website to leave your electronic signature, my number is 18989, and see if you can find me.

2 Spring 5 Foresight

As the first responsive Web framework in the Java world, Spring 5 offers full support for end-to-end responsive programming.

Photo credit:Spring Framework Reference Documentation

On the left is the traditional Servlet-based Spring Web MVC framework, and on the right is the new Spring WebFlux framework based on Reactive Streams introduced in version 5.0. Router Functions, WebFlux, Reactive Streams has three new components.

  • Router Functions: Spring MVC annotations (@controller, @requestMapping, etc.) provide a functional style API for creating Routers, handlers, and filters.
  • WebFlux: Core component that coordinates upstream and downstream components to provide responsive programming support.
  • Reactive Streams: An asynchronous data stream processing standard that supports Backpressure. Mainstream implementations include RxJava and Reactor, and Spring WebFlux integrates Reactor by default.

In terms of the choice of Web containers, Spring WebFlux supports both traditional containers like Tomcat and Jetty (provided that Servlet 3.1 non-blocking IO API is supported) and asynchronous containers like Netty and Undertow. Regardless of the container, Spring WebFlux ADAPTS its input and output streams to the Flux

format for uniform processing.

Notably, in addition to the new Router Functions interface, Spring WebFlux also supports declaring Reactive Controllers using the old Spring MVC annotations. Unlike traditional MVC controllers, Reactive Controllers operate on non-blocking ServerHttpRequest and ServerHttpResponse, Instead of HttpServletRequest and HttpServletResponse in Spring MVC.

Here’s an example from my sample project on GitHub,

@RestController
public class RestaurantController {

    private final RestaurantRepository restaurantRepository;

    private final ReactiveMongoTemplate reactiveMongoTemplate;

    public RestaurantController(RestaurantRepository restaurantRepository, ReactiveMongoTemplate reactiveMongoTemplate) {
        this.restaurantRepository = restaurantRepository;
        this.reactiveMongoTemplate = reactiveMongoTemplate;
    }

    @GetMapping("/reactive/restaurants")
    public Flux<Restaurant> findAll(a) {
        return restaurantRepository.findAll();
    }

    @GetMapping("/reactive/restaurants/{id}")
    public Mono<Restaurant> get(@PathVariable String id) {
        return restaurantRepository.findById(id);
    }

    @PostMapping("/reactive/restaurants")
    public Flux<Restaurant> create(@RequestBody Restaurant[] restaurants) {
        return Flux.just(restaurants)
                .log()
                .flatMap(r -> Mono.just(r).subscribeOn(Schedulers.parallel()), 10) .flatMap(reactiveMongoTemplate::insert); }}Copy the code

3 summary

In addition to responsive programming support, Spring 5 includes many features Java programmers have been waiting for, including JDK 9, Junit 5, Servlet 4, and HTTP/2 support. The latest version of Spring 5 is RC1, and Spring Boot has just released version 2.0.0 M1. According to the official Spring blog, Spring 5 will be released shortly after JDK 9 GA, around the end of July this year.

That’s my brief introduction to responsive programming support in Spring 5, and you’re welcome to share it on my message board. Stay tuned for my next post where I’ll talk about some of my understanding of reactive programming.

4 reference

  • New in Spring 5: Functional Web Framework
  • Spring Framework 5 – Preview & Roadmap
  • Spring Framework 5: History and Reactive features