Flux转Mono next()
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.Function;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; @Slf4j
public class TestFindResult {
private static final Map<String, String> templates;
private static final int sleep = 1000; static {
templates = new LinkedHashMap<>();
templates.put("aDB", "a");
templates.put("bDB", "b");
templates.put("cDB", "c");
} public Mono<String> findResult(Function<String, Mono<String>> query) {
return Flux.fromIterable(templates.values())
.flatMap(query)
.next()
.onErrorResume(NoSuchElementException.class, e -> Mono.empty())
.onErrorMap(IndexOutOfBoundsException.class, MultipleUpstreamException::new);
} public static void main(String[] args) {
TestFindResult test = new TestFindResult();
Function<String, Mono<String>> query = (value) -> {
try {
Thread.sleep(sleep); // mock DB query
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info(
"Thread id:{}, Thread name:{}, value:{}, used ms:{}",
Thread.currentThread().getId(),
Thread.currentThread().getName(),
value,
sleep);
return Mono.just(value);
};
System.out.println(test.findResult(query).subscribe());
}
}
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; @Slf4j
public class TestFindMongo {
private static final Map<String, String> templates;
private static final int sleep = 1000; static {
templates = new LinkedHashMap<>();
templates.put("aDB", "a");
templates.put("bDB", "b");
templates.put("cDB", "c");
} public Mono<String> findMongo() {
StopWatch stopWatch = StopWatch.createStarted();
return Flux.fromIterable(templates.entrySet())
.filterWhen(
template -> {
String key = template.getKey();
String value = template.getValue();
try {
Thread.sleep(sleep); // mock DB query
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info(
"Thread id:{}, Thread name:{}, query:{}, value:{} , used ms:{}",
Thread.currentThread().getId(),
Thread.currentThread().getName(),
key,
value,
sleep);
return Mono.just(value.equals("b"));
})
.next()
.doOnSuccess(templateEntry -> log.info("Match {} ", templateEntry.getKey()))
.map(Entry::getValue)
.onErrorResume(NoSuchElementException.class, e -> Mono.empty())
.onErrorMap(IndexOutOfBoundsException.class, MultipleUpstreamException::new)
.doOnTerminate(() -> log.info("Database recon took {} ms", stopWatch.getTime()));
} public static void main(String[] args) {
TestFindMongo test = new TestFindMongo();
System.out.println(test.findMongo().subscribe());
}
}
import static org.springframework.http.HttpStatus.*; import org.springframework.web.server.ResponseStatusException;
import reactor.core.publisher.Flux; public class MultipleUpstreamException extends ResponseStatusException { private static final String MULTILPLE_UPSTREAM_MATCH_ERR =
"Your query contains properties matching multiple upstreams. "
+ "Data for multiple upstreams can't be returned in one query. "
+ "Please either specify upstream by providing publisherSystem "
+ "(GSM,MUNI_ITICKET,MUNI_OASYS,TPSDERIV,EDLR) "
+ "and region or request deal properties matching only one upstream"; MultipleUpstreamException() {
super(BAD_REQUEST, MULTILPLE_UPSTREAM_MATCH_ERR);
} /**
* This constructor has syntax adapted to Mono API
*
* @param indexOutOfBoundsException emitted on {@link Flux#single()} when Flux has more than one
* elements
* @see Mono#onErrorMap(Class, java.util.function.Function))
*/
MultipleUpstreamException(IndexOutOfBoundsException indexOutOfBoundsException) {
this();
}
}
Flux转Mono next()的更多相关文章
- Flux 和 Mono 的区别
Flux 和 Mono 是 Reactor 中的两个基本概念.Flux 表示的是包含 0 到 N 个元素的异步序列.在该序列中可以包含三种不同类型的消息通知:正常的包含元素的消息.序列结束的消息和序列 ...
- Reactor之发射器(Flux、Mono)转换操作函数
数据合并函数 由于业务需求有的时候需要将多个数据源进行合并,Reactor提供了concat方法和merge方法: concat public static <T> Flux<T&g ...
- Reactor系列(三)创建Flux,Mono(续)
创建Mono 视频讲解:https://www.bilibili.com/video/av78944069/ FluxMonoTestCase.java package com.example.rea ...
- Java反应式框架Reactor中的Mono和Flux
1. 前言 最近写关于响应式编程的东西有点多,很多同学反映对Flux和Mono这两个Reactor中的概念有点懵逼.但是目前Java响应式编程中我们对这两个对象的接触又最多,诸如Spring WebF ...
- springweb flux 编程模型
Spring WebFlux 编程模型是在spring5.0开始,springbot2.0版本设计出来的新的一种反应式变成模型.它脱胎于reactor模式,是java nio 异步编程模型. 传统一般 ...
- Reactor by Example--转
原文地址:https://www.infoq.com/articles/reactor-by-example Key takeaways Reactor is a reactive streams l ...
- Spring 5 新特性:函数式Web框架
举例 我们先从示例应用程序的一些摘录开始.下面是暴露Person对象的响应信息库.很类似于传统的,非响应信息库,只不过它返回Flux<Person>而传统的返回List<Person ...
- springboot2 webflux 响应式编程学习路径
springboot2 已经发布,其中最亮眼的非webflux响应式编程莫属了!响应式的weblfux可以支持高吞吐量,意味着使用相同的资源可以处理更加多的请求,毫无疑问将会成为未来技术的趋势,是必学 ...
- Spring Framework 5 中的新特性
https://www.ibm.com/developerworks/cn/java/j-whats-new-in-spring-framework-5-theedom/index.html Spri ...
随机推荐
- 编译安装常用包+阿里镜像源-常用资源-系统-下载-科莱软件下载-docker仓库包-安全圈-杏雨梨云-图形界面安装-docker私有双仓库-阿里源报错处理-centos7目录大小
yum install apr-util apr-util-devel apr apr-devel pcre pcre-devel zlib zlib-devel openssl openssl-de ...
- C++ mfc 简易文本编辑器 遇到的一些问题
[题目40]简易文本编辑器. 设计一个简易的文本编辑器. 设计要求: (1) 具有图形菜单界面: (2) 查找,替换(等长,不等长),插入(插串,文本块的插入).文本块移动(行块,列块移动),删除; ...
- FC 与 FB 与 OB 的区别,时间标记冲突与一致性检查 有详细的步骤
关键字1 组织块的程序是由用户自己编写. 关键字2 时间标记冲突与一致性检查 有详细的步骤. 关键字3 FC 与 FB 与 OB 的区别? (一)功能 功能块 区别 ? FB 和FC均为 用户编写 ...
- 一百一十一、SAP的OO-ALV之五,显示ALV表格
一.在屏幕里面有2部分,(PROCESS BEFORE OUTPUT 用于显示, PROCESS AFTER INPUT用于数据处理).我们创建的display_alv函数, 二.display_al ...
- Aspen安装过程报错总结
前几天一直帮朋友安装Aspen v11,因为之前的老版本总是报错,报错内容大概是证书过期了, 一开始朋友电脑上的老版本的Aspen 8卸载了,删除之前的数据库SqlServer 2012 ,然后重新安 ...
- Python 删除文件与文件夹
版权所有,未经许可,禁止转载 章节 Python 介绍 Python 开发环境搭建 Python 语法 Python 变量 Python 数值类型 Python 类型转换 Python 字符串(Str ...
- List中bean某属性值转换为list
List<类> lst = new ArrayList<>() ; lst.stream().map(类::get需要取得仠的属性名).collect(Collectors.t ...
- Linux每日一练20200220
- SPOJ - AMR11H Array Diversity (排列组合)
题意:给定n个数,求包含最大值和最小值的子集(数字连续)和子序列(数字不连续)的个数. 分析: 1.如果n个数都相同,则子集个数为N * (N + 1) / 2,子序列个数为2N-1. 2.将序列从头 ...
- 屏幕适配 - JS - 网站布局元素
网页可见区域宽:document.body.clientWidth; 网页可见区域高:document.body.clientHeight; 网页可见区域高:document.body.offsetW ...