Spring WebFlux 编程模型是在spring5.0开始,springbot2.0版本设计出来的新的一种反应式变成模型。它脱胎于reactor模式,是java nio 异步编程模型。

传统一般采用servelt 那一套,是阻塞式编程,现在换了种模式,大大提高程序处理问题能力。

不是很明白概念请看官网:https://spring.io/

代码如下:

import com.example.demo.exception.ResourceNotFoundException;
import com.example.demo.po.JsonResult;
import com.example.demo.po.User;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; /**
* 重要的两个 FLux 与 Mono
* Created by mingge on 2018/5/10.
*/
@Service
public class UserService { private final Map<String, User> data = new ConcurrentHashMap<>(); {
data.put("1",new User("1","张三"));
data.put("2",new User("1","李四"));
data.put("3",new User("1","王五")); } public Flux<User> list() {
return Flux.fromIterable(this.data.values());
} public Flux<JsonResult> lists(){
JsonResult jsonResult=new JsonResult(200,"ok",this.data.values());
return Flux.just(jsonResult);
} public Flux<User> getById(final Flux<String> ids) {
return ids.flatMap(id -> Mono.justOrEmpty(this.data.get(id)));
} public Mono<User> getById(final String id) {
return Mono.justOrEmpty(this.data.get(id))
.switchIfEmpty(Mono.error(new ResourceNotFoundException()));
} public Flux<User> createOrUpdate(final Flux<User> users) {
return users.doOnNext(user -> this.data.put(user.getId(), user));
} public Mono<User> createOrUpdate(final User user) {
this.data.put(user.getId(), user);
return Mono.just(user);
} public Mono<User> delete(final String id) {
return Mono.justOrEmpty(this.data.remove(id));
}
}

控制层的使用方式:

@RestController
@RequestMapping("/user")
public class UserController { private final UserService userService; @Autowired
public UserController(final UserService userService) {
this.userService = userService;
} @ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Resource not found")
@ExceptionHandler(ResourceNotFoundException.class)
public void notFound() {
} @GetMapping("/list")
public Flux<User> list() {
Flux<User> users=this.userService.list();
return users;
} @GetMapping("/lists")
public Flux<JsonResult> lists(){
return this.userService.lists();
}
@GetMapping("/{id}")
public Mono<User> getById(@PathVariable("id") final String id) {
return this.userService.getById(id);
} @PostMapping("/edit")
public Flux<User> create(@RequestBody final Flux<User> users) {
return this.userService.createOrUpdate(users);
} @PutMapping("/{id}")
public Mono<User> update(@PathVariable("id") final String id, @RequestBody final User user) {
Objects.requireNonNull(user);
user.setId(id);
return this.userService.createOrUpdate(user);
} @DeleteMapping("/{id}")
public Mono<User> delete(@PathVariable("id") final String id) {
return this.userService.delete(id);
}

看起来似乎跟传统没有什么区别,其实主要区别就是返回值的一种变化,其底层处理的方法不一样啦...

一些小的测试:

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests { @Test
public void testMonoBasic(){
Mono.fromSupplier(() -> "Hello").subscribe(System.out::println);
Mono.justOrEmpty(Optional.of("Hello")).subscribe(System.out::println);
Mono.create(sink -> sink.success("Hello")).subscribe(System.out::println);
} @Test
public void testBasic(){
Flux.just("Hello", "World").subscribe(System.out::println);
Flux.fromArray(new Integer[] {1, 2, 3}).subscribe(System.out::println);
Flux.empty().subscribe(System.out::println);
Flux.range(1, 10).subscribe(System.out::println);
Flux.interval(Duration.of(10, ChronoUnit.SECONDS)).subscribe(System.out::println);
} @Test
public void testA(){
Flux.generate(sink -> {
sink.next("Hello");
sink.complete();
}).subscribe(System.out::println); final Random random = new Random();
Flux.generate(ArrayList::new, (list, sink) -> {
int value = random.nextInt(100);
list.add(value);
sink.next(value);
if (list.size() == 10) {
sink.complete();
}
return list;
}).subscribe(System.out::println);
} @Test
public void testB(){
Flux.create(sink -> {
for (int i = 0; i < 10; i++) {
sink.next(i);
}
sink.complete();
}).subscribe(System.out::println);
} @Test
public void testC(){
Flux.range(1, 100).buffer(20).subscribe(System.out::println);
Flux.range(1, 10).bufferUntil(i -> i % 2 == 0).subscribe(System.out::println);
Flux.range(1, 10).bufferWhile(i -> i % 2 == 0).subscribe(System.out::println);
Flux.range(1, 10).filter(i -> i % 2 == 1).subscribe(System.out::println);
} @Test
public void testD(){
Flux.range(1, 100).window(20).subscribe(System.out::println);
} }

有机会搭建新系统我就打算尝试这种新的方法啦...

更多:https://www.cnblogs.com/davidwang456/p/4589439.html

springweb flux 编程模型的更多相关文章

  1. JS魔法堂:深究JS异步编程模型

    前言  上周5在公司作了关于JS异步编程模型的技术分享,可能是内容太干的缘故吧,最后从大家的表情看出"这条粉肠到底在说啥?"的结果:(下面是PPT的讲义,具体的PPT和示例代码在h ...

  2. 多线程之异步编程: 经典和最新的异步编程模型,async与await

    经典的异步编程模型(IAsyncResult) 最新的异步编程模型(async 和 await) 将 IAsyncInfo 转换成 Task 将 Task 转换成 IAsyncInfo 示例1.使用经 ...

  3. 多线程之异步编程: 经典和最新的异步编程模型, IAsyncInfo 与 Task 相互转换

    经典的异步编程模型(IAsyncResult) 最新的异步编程模型(async 和 await) 将 IAsyncInfo 转换成 Task 将 Task 转换成 IAsyncInfo 示例1.使用经 ...

  4. jQuery插件编写及链式编程模型小结

    JQuery极大的提高了我们编写JavaScript的效率,让我们可以愉快的编写代码,做出各种特效.大多数情况下,我们都是使用别人开发的JQuery插件,今天我们就来看看如何把我们常用的功能做出JQu ...

  5. 云巴:基于MQTT协议的实时通信编程模型

    概要 有人常问,云巴实时通信系统到底提供了一种怎样的服务,与其他提供推送或 IM 服务的厂商有何本质区别.其实,从技术角度分析,云巴与其它同类厂商都是面向开发者的通信服务,宏观的编程模型都是大同小异, ...

  6. 第3章 窗口与消息_3.1Windows编程模型

    第3章窗口与消息 3.1 Windows_编程模型 (1)窗口程序的运行过程   ①设计窗口   ②注册窗口类(RegisterClassEx).在注册之前,要先填写RegisterClassEx的参 ...

  7. MFC-01-Chapter01:Hello,MFC---1.1 Windows 编程模型

    1.1 Windows编程模型 为传统的操作系统编写的程序使用的是过程化模型,即程序从头到尾按顺序执行.例如C程序,从main函数入口开始执行,中间调用不同的函数一直到程序结束返回,这种过程是程序本身 ...

  8. 金蝶 K/3 Cloud 服务端控件编程模型

    如下图是服务端已有的控件编程模型

  9. 谈谈c#中异步编程模型的变迁

    大家在编程过程中都会用到一些异步编程的情况.在c#的BCL中,很多api都提供了异步方法,初学者可能对各种不同异步方法的使用感到迷惑,本文主要为大家梳理一下异步方法的变迁以及如何使用异步方法. Beg ...

随机推荐

  1. Date日期操作

    获取年月日时分秒: package com.util; import java.text.DateFormat; import java.util.Calendar; import java.util ...

  2. ActiveMq 配置多队列

    一直在赶项目,好久没有写博文了,中间偶尔有些代码什么的,也都是放到github了,不过大多都是测试代码,毕竟有些成型的东西是给公司写的,鉴于职业道德,还是不好公开. 言归正传,这两天在接入第三方的收费 ...

  3. MySQLdb & pymsql

    python有两个模块可以连接和操作mysql数据库,分别是MySQLdb和pymysql,python3建议使用pymysql MySQLdb安装 pip install mysql-python ...

  4. Spring Boot 2 实践记录之 MyBatis 集成的启动时警告信息问题

    按笔者 Spring Boot 2 实践记录之 MySQL + MyBatis 配置 中的方式,如果想正确运行,需要在 Mapper 类上添加 @Mapper 注解. 但是加入此注解之后,启动时会出现 ...

  5. asp.net core mvc 管道之中间件

    asp.net core mvc 管道之中间件 http请求处理管道通过注册中间件来实现各种功能,松耦合并且很灵活 此文简单介绍asp.net core mvc中间件的注册以及运行过程 通过理解中间件 ...

  6. 消息中间件(Kafka/RabbitMQ)收录集

    https://blog.csdn.net/u013256816/article/details/54743481

  7. 667. Beautiful Arrangement II

    Given two integers n and k, you need to construct a list which contains n different positive integer ...

  8. Trie-648. Replace Words

    In English, we have a concept called root, which can be followed by some other words to form another ...

  9. Android逆向进阶——让你自由自在脱壳的热身运动(dex篇)

    本文作者:HAI_ 0×00 前言 来看看我们今天的主题. 让你自由自在脱壳的热身运动. 现在很多第厂家都是使用第三方的加固方式来进行加固的.或者使用自己的加固方式进行加固. 那么我们必不可少的就是脱 ...

  10. 为什么子元素设置margin-top会作用在父元素上?

    原因在于:CSS 外边距合并 复现: <!DOCTYPE html> <html lang="en"> <head> <meta char ...