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. C#动态获取本机可用串口的两种方式

    1. private void GetSerialPort() //获取串口列表 { RegistryKey keyCom = Registry.LocalMachine.OpenSubKey(&qu ...

  2. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-play-circle

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  3. 整合 nginx php-fpm

    start 继上一篇 整合两个images  完成 LNMP github  https://github.com/shiphp/nginx-env     稍加修改 vim   dockerfile ...

  4. sql server 2012插入排序后的数据到临时表无效

    IF OBJECT_ID('TEMPDB..#list') IS NOT NULLBEGIN DROP TABLE TEMPDB.#list END CREATE TABLE #list(OFC_ID ...

  5. P1051复数乘法

    P1051复数乘法 转跳点:

  6. 【pwnable.kr】 unlink

    pwnable.kr 第一阶段的最后一题! 这道题目就是堆溢出的经典利用题目,不过是把堆块的分配与释放操作用C++重新写了一遍,可参考<C和C++安全编码一书>//不是广告 #includ ...

  7. 2.14 Java web 复习总结

    1.空指针异常原因(NullPointerExceptio)之一: 在Dao层里边 声明 Connection conn = DBUtil.getConn(); //不能少 Statement sta ...

  8. metaspace 元空间

    为何移除持久代 它的大小是在启动时固定好的, 很难进行调优 -XX:MaxPermSize(默认64M) HotSpot 的内部类型也是Java对象: 它可能会在Full GC中被移动, 同时它对应用 ...

  9. PHP笔记01

    php 环境 xamp wamp phpstudy等集成软件网上很多 PHP基础语法 PHP语法是以<?php开始 ?>结束的//php 文件的默认扩展名是.php 例如(用PHP输出he ...

  10. ROS2学习日志:TurtleSim测试日志(基于ROS2 Eloquent Elusor)

    TurtleSim测试日志(基于ROS2 Eloquent Elusor) 1.ros2 run 1.1 ros2 run turtlesim turtlesim_node --ros-args -- ...