Reactor

WebFlux系列(三) Server-Sent Events(续)

佐手、 提交于 2020-01-06 21:52:05
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> #编程#入门#java#spring#webflux#SSE#reactor# SSE数据结构 视频讲解: https://www.bilibili.com/video/av82297051/ WebfluxServerApplication.java package com.example.webfluxserver; import lombok.extern.log4j.Log4j2; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.codec.ServerSentEvent; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import

Reactor系列(十六)disposable停止Flux流

青春壹個敷衍的年華 提交于 2020-01-06 16:13:01
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> #java#reactor#flux#disposable# 停止flux流 视频讲解: https://www.bilibili.com/video/av81385859/ FluxMonoTestCase.java package com.example.reactor; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import reactor.core.Disposable; import reactor.core.publisher.Flux; import java.time.Duration; @Slf4j public class FluxMonoTestCase extends BaseTestCase { @Test public void disposable() throws InterruptedException { Flux<Long> longFlux =Flux.interval(Duration.ofMillis(1)); //take方法准确获取订阅数据量 Disposable disposable = longFlux.take(50).subscribe(x->log.info(

How to convert List<Mono<T>> to Mono<List<T>>?

爷,独闯天下 提交于 2020-01-06 11:10:10
问题 I have a method that returns Mono<Output> : interface Processor { Mono<Output> process(Input input); } And I want to execute this processor method for a collection: List<Input> inputs = // get inputs Processor processor = // get processor List<Mono<Output>> outputs = inputs.stream().map(supplier::supply).collect(toList()); But instead of a List<Mono<Output>> I want to get Mono<List<Output>> that will contain aggregated results. I tried reduce , but the final result looks very clumsy: Mono

Reactor Mono - execute parallel tasks

て烟熏妆下的殇ゞ 提交于 2020-01-05 10:09:22
问题 I am new to Reactor framework and trying to utilize it in one of our existing implementations. LocationProfileService and InventoryService both return a Mono and are to executed in parallel and have no dependency on each other (from the MainService). Within LocationProfileService - there are 4 queries issued and the last 2 queries have a dependency on the first query. What is a better way to write this? I see the calls getting executed sequentially, while some of them should be executed in

Returning a null in a .map() versus .flatMap() in Reactor

淺唱寂寞╮ 提交于 2020-01-04 06:09:58
问题 The following piece of code works : // emitting employees... .flatMap(employee -> { boolean isAlive = employee.isAlive(); return Mono.just(isAlive) .flatMap(myBoolean -> { if (myBoolean) { return Mono.empty(); } else { return Mono.just(employee); } }); }) But I was wondering why I can't use a .map upon processing myBoolean (NullPointerException when it returns the null) .map(myBoolean -> { if (myBoolean) { return null; } else { return employee; } }); I believe I lack some understanding about

Why does it make sense to use asynchronous clients for Redis?

你说的曾经没有我的故事 提交于 2020-01-01 12:12:09
问题 In this page listing the redis clients, I counted 8 asynchronous libraries. My understanding is that frameworks like as node.js or tornado only make sense when the asynchronous callback functions are not fighting with each other for I/O, otherwise you might as well go synchronous. But Redis is single-threaded. So they are actually fighting for I/O. Doesn't the single-threaded nature of Redis cancel all the potential benefits of asynchronous callbacks? Why does it make sense to use

异步编程之美

只愿长相守 提交于 2019-12-30 18:45:16
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 在大学毕业后的工作期间,从最开始的编写同步代码,到慢慢为了提高系统性能,把一些任务使用异步的方式来处理,从而提高系统的响应时间,那么就会产生一些新的问题,如何监控在异步线程执行的任务的执行状态,是否出现了错误,出现了错误怎么处理,系统创建大量线程又该如何统一管理,这些种种问题都让使我意识到深入了解异步编程的必要性。 同步的代码, 在很多情况下, CPU其实是在等待中度过的, 比如等待一个网络连接, 等待MySQL服务器的数据返回异步的代码, 就是把这些等待的时间给充分利用起来了, 把网络连接, 访问数据库这种耗时的工作时都在注册一个callback或者event之后切换出来,让CPU先去干别的活(例如响应别的请求), 当网络连接,数据库返回结果时再回来执行刚才的callback中的代码,异步的代码可以大大的提升系统的容量上限, 因为充分利用了空闲的CPU时间, 但是对于单个的请求的性能提升帮助比较有限 (除非你的单个请求大量依赖这种IO操作)。 Java的异步编程其实是一个充分利用计算机CPU资源,不想让主程序阻塞在某个长时间运行的任务上,这类耗时的任务可以是IO操作、远程调用以及高密度计算任务。如果不使用多线程异步编程,我们的系统就会阻塞在耗时的子任务上,会导致极大延长完成主函数任务的时间

Reactor系列(十五)backpressure背压

穿精又带淫゛_ 提交于 2019-12-30 13:59:51
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> #java#reactor#flux#backpressure# 背压 视频讲解 : https://www.bilibili.com/video/av81253248/ FluxMonoTestCase.java package com.example.reactor; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; import reactor.core.publisher.Flux; import java.time.Duration; import java.util.List; @Slf4j public class FluxMonoTestCase extends BaseTestCase { @Test public void pressure() throws InterruptedException { Flux<Long> longFlux = Flux.interval(Duration.ofMillis(1)); longFlux

Reactor系列(十四)buffer缓冲

烈酒焚心 提交于 2019-12-29 14:11:50
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> #java#reactor#flux#buffer# 缓冲 视频讲解: https://www.bilibili.com/video/av81107026/ FluxMonoTestCase.java package com.example.reactor; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import reactor.core.publisher.Flux; import java.util.List; @Slf4j public class FluxMonoTestCase extends BaseTestCase { @Test public void buffer(){ Flux<String> stringFlux = Flux.just("a","b","c","d","e","f","g"); stringFlux.subscribe(x -> System.out.print("->"+x)); System.out.println(); Flux<List<String>> listFlux = stringFlux.buffer(2); listFlux.subscribe(x ->

快速入门Swoole引擎原理的干货

冷暖自知 提交于 2019-12-27 16:12:46
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 过去半年使用PHP和Java两种技术栈完成了一个游戏服务器项目。由于项目中有高频的网络请求,所以PHP技术栈尝试使用Swoole引擎(基于事件的高性能异步并行网络通信引擎)来完成部分游戏业务。 Swoole的安装 安装swoole很简单,由于是国人做的项目,很多issue可以在 官网 文档找到答案。安装分两种: 编译安装。直接去github或者gitee去下载官方的发行版,编译安装后,将so拓展写入php.ini文件。 容器安装。swoole引擎应用广泛,所以hub上有很多可用的容器,选择需要的pull一下即可。 Swoole引擎的优势 常驻内存。传统 PHP框架或者单文件,在处理每个请求之前,都要做一遍加载框架文件、配置的操作,请求完成之后会释放所有资源和内存,无须担心内存泄漏。但是如果请求数量上升,并发很高的时候,快速创建资源,又马上释放,会导致 PHP 程序运行效率急剧下降。而使用 Swoole 则没有这个问题:PHP的代码加载到内存后,拥有更长的生命周期,这样建立的数据库连接和其他大的对象,不被释放。每次请求只需要处理很少的代码,而这些代码只在第一次运行时,被 PHP 解析器编译,驻留内存。以后都是直接载入 OPCODE ,让 Zend 引擎直接运行。另外,之前PHP不能实现的,如数据库连接池