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()的更多相关文章

  1. Flux 和 Mono 的区别

    Flux 和 Mono 是 Reactor 中的两个基本概念.Flux 表示的是包含 0 到 N 个元素的异步序列.在该序列中可以包含三种不同类型的消息通知:正常的包含元素的消息.序列结束的消息和序列 ...

  2. Reactor之发射器(Flux、Mono)转换操作函数

    数据合并函数 由于业务需求有的时候需要将多个数据源进行合并,Reactor提供了concat方法和merge方法: concat public static <T> Flux<T&g ...

  3. Reactor系列(三)创建Flux,Mono(续)

    创建Mono 视频讲解:https://www.bilibili.com/video/av78944069/ FluxMonoTestCase.java package com.example.rea ...

  4. Java反应式框架Reactor中的Mono和Flux

    1. 前言 最近写关于响应式编程的东西有点多,很多同学反映对Flux和Mono这两个Reactor中的概念有点懵逼.但是目前Java响应式编程中我们对这两个对象的接触又最多,诸如Spring WebF ...

  5. springweb flux 编程模型

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

  6. Reactor by Example--转

    原文地址:https://www.infoq.com/articles/reactor-by-example Key takeaways Reactor is a reactive streams l ...

  7. Spring 5 新特性:函数式Web框架

    举例 我们先从示例应用程序的一些摘录开始.下面是暴露Person对象的响应信息库.很类似于传统的,非响应信息库,只不过它返回Flux<Person>而传统的返回List<Person ...

  8. springboot2 webflux 响应式编程学习路径

    springboot2 已经发布,其中最亮眼的非webflux响应式编程莫属了!响应式的weblfux可以支持高吞吐量,意味着使用相同的资源可以处理更加多的请求,毫无疑问将会成为未来技术的趋势,是必学 ...

  9. Spring Framework 5 中的新特性

    https://www.ibm.com/developerworks/cn/java/j-whats-new-in-spring-framework-5-theedom/index.html Spri ...

随机推荐

  1. leetcode1261 Find Elements in a Contaminated Binary Tree

    """ Given a binary tree with the following rules: root.val == 0 If treeNode.val == x ...

  2. 吴裕雄--天生自然java开发常用类库学习笔记:多对多关系范例

    import java.util.List ; import java.util.ArrayList ; public class Course{ private String name ; priv ...

  3. NO13 Linux的基础优化-关闭SELinux功能-Linux的7种运行级别-防火墙设置-中文显示设置

    壹  安装Linux系统后调优及安全设置: 1 关闭SELinux功能: [root@localhost data]# sed 's#SELINUX=enforcing#SELINUX=disable ...

  4. node核心 http模块

    node作为服务器更多的是web服务器 1.http模块 首先:http是一个协议.里面有通信机制,状态码一大堆乱七八糟的东西.自己写猴年马月都写不出来,这个对象帮我们集成.直接用 服务器对象: ht ...

  5. Golang的标识符命名规则

    Golang的标识符命名规则 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关键字 1>.Go语言有25个关键字 Go语言的25个关键字如下所示: break,defau ...

  6. vSphere vSwitch网络属性配置详解

    1.安全 混杂模式:把vSwitch当成是一个hub,同一台交换机上面所有的虚拟机都能接受到二层数据包. MAC地址更改:当vSwitch上面连接的某一个虚拟机MAC地址发生更改时,vSwitch是否 ...

  7. linux下安装jdk&&Tomcat环境

    linux系统 Centos6 下部署应用服务 jdk-1.7 环境安装:(切换到root用户下操作)1. 在 /usr/local 目录下创建jdk7文件 mkdir /usr/local/jdk7 ...

  8. yeoman 介绍、安装 和 使用

    一.介绍.安装 1, 是什么 Yeoman其实是3个工具的总和: ü  yo --- 脚手架,自动生成工具 ü  Grunt.gulp --- 构建工具 (最初只有grunt,后面gulp火了添加进来 ...

  9. 042-PHP使用闭包函数递归无限级分类

    <?php //使用闭包函数递归无限级分类 function demo($array){ # 用于存储递归后的队列 $data = []; # 递归函数 $func = function (&a ...

  10. JavaWeb学习记录

    服务器端跳转(请求重定向): 1.jsp内跳转 : <jsp:forward page="page_scope_03.jsp"/> 客户端跳转(请求转发): 1.通过超 ...