当前位置: 首页 > wzjs >正文

成都企业网站建设 四川冠辰科技企业网站推广 知乎

成都企业网站建设 四川冠辰科技,企业网站推广 知乎,线下推广渠道和方式,婚恋网站女孩子都是做美容Reactor 是一个基于响应式编程的库&#xff0c;主要用于构建异步和事件驱动的应用程序。Reactor 提供了丰富的 API&#xff0c;包括创建、转换、过滤、组合等操作符&#xff0c;用于处理异步数据流。以下是一些 Reactor 的主要 API 示例&#xff1a; pom依赖 <dependencyMan…

Reactor 是一个基于响应式编程的库,主要用于构建异步和事件驱动的应用程序。Reactor 提供了丰富的 API,包括创建、转换、过滤、组合等操作符,用于处理异步数据流。以下是一些 Reactor 的主要 API 示例:

pom依赖

   <dependencyManagement><dependencies><dependency><groupId>io.projectreactor</groupId><artifactId>reactor-bom</artifactId><version>2023.0.0</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>io.projectreactor</groupId><artifactId>reactor-core</artifactId></dependency><dependency><groupId>io.projectreactor</groupId><artifactId>reactor-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.7.2</version><scope>test</scope></dependency></dependencies>

22. 使用 Reactor 的 elapsed 方法进行时间测量

elapsed 方法可以用于测量元素发射之间的时间间隔,返回包含时间间隔和元素的元组。

import reactor.core.publisher.Flux;
import java.time.Duration;public class ReactorElapsedExample {public static void main(String[] args) throws InterruptedException {Flux<Integer> source = Flux.just(1, 2, 3, 4, 5).delayElements(Duration.ofSeconds(1));source.elapsed().subscribe(tuple -> {long elapsedTime = tuple.getT1();int value = tuple.getT2();System.out.println("Elapsed Time: " + elapsedTime + "ms, Value: " + value);});Thread.sleep(23333);}
}

23. 使用 Reactor 的 cache 方法进行结果缓存

cache 方法可以用于缓存结果,避免多次计算相同的数据流。

import reactor.core.publisher.Flux;public class ReactorCacheExample {public static void main(String[] args) {Flux<Integer> source = Flux.range(1, 3).log() //日志.cache();source.subscribe(System.out::println); // 输出: 1, 2, 3source.subscribe(System.out::println); // 输出: 1, 2, 3 直接从缓存中取,日志中显示,未调用request、onNext等方法}
}

24. 使用 Reactor 的 reduce 方法进行聚合操作

reduce 方法用于对数据流中的元素进行聚合操作,返回一个包含最终结果的 Mono

import reactor.core.publisher.Flux;public class ReactorReduceExample {public static void main(String[] args) {Flux<Integer> source = Flux.range(1, 5);source.reduce(Integer::sum).subscribe(result -> System.out.println("Sum: " + result)); // 输出: Sum: 15}
}

25. 使用 Reactor 的 interval 方法进行周期性操作

interval 方法可以用于创建一个周期性的数据流,用于执行定时任务。

import reactor.core.publisher.Flux;
import java.time.Duration;public class ReactorIntervalExample {public static void main(String[] args) throws InterruptedException {Flux.interval(Duration.ofSeconds(1)).take(5) // 限制产生的元素数量.subscribe(System.out::println);Thread.sleep(233333);}
}

26. 使用 Reactor 的 onErrorContinue 方法进行错误处理

onErrorContinue 方法允许在发生错误时继续处理数据流,并提供一个处理函数,用于处理错误。

import reactor.core.publisher.Flux;public class ReactorOnErrorContinueExample {public static void main(String[] args) {Flux<Integer> source = Flux.just(1, 2, 0, 4, 5);// 在发生除零错误时继续处理数据流source.map(x -> 10 / x).onErrorContinue((error, value) -> {//10/0触发的异常会在最后打印System.err.println("Error: " + error.getMessage() + ", Value: " + value);}).subscribe(System.out::println); }
}

28. 使用 Reactor 的 materialize 方法进行错误通知

materialize 方法用于将正常元素和错误信息封装为通知对象,使得错误信息也成为数据流的一部分。

import reactor.core.publisher.Flux;public class ReactorMaterializeExample {public static void main(String[] args) {Flux<Integer> source = Flux.just(1, 2, 0, 4, 5);// 将正常元素和错误信息封装为通知对象source.map(x -> 10 / x).materialize().subscribe(System.out::println);}
}

29. 使用 Reactor 的 expand 方法进行递归操作

expand 方法用于对数据流进行递归操作,产生新的元素并加入数据流。

import reactor.core.publisher.Flux;public class ReactorExpandExample {public static void main(String[] args) {Flux<Integer> source = Flux.just(1, 2, 3);// 对数据流进行递归操作,每个元素产生两个新元素source.expand(value -> Flux.just(value * 2, value * 3)).take(22) // 限制产生的元素数量.subscribe(System.out::println);//原始   新元素 ->新元素 ->新元素...//1 2 3   -> 2 3  4 6  6 9 ->4 6  6 9     8 12  12 18    12 18   18 27 -> 8 ...}
}

30. 使用 Reactor 的 checkpoint 方法进行调试

checkpoint 方法用于在操作链中设置断点,以便在调试时更容易定位问题。

import reactor.core.publisher.Flux;public class ReactorCheckpointExample {public static void main(String[] args) {Flux<Integer> source = Flux.range(1, 5);// 在操作链中设置断点source.checkpoint("Initial Source").map(x -> x * 2).checkpoint("Mapped Source").subscribe(System.out::println);}
}
  • 好像没啥用

31. 使用 Reactor 的 groupBy 方法进行分组操作

groupBy 方法用于将数据流中的元素进行分组,返回一个 GroupedFlux

import reactor.core.publisher.Flux;
import reactor.core.publisher.GroupedFlux;public class ReactorGroupByExample {public static void main(String[] args) {Flux<Integer> source = Flux.range(1, 10);// 将数据流中的元素按奇偶分组Flux<GroupedFlux<String, Integer>> groupedFlux = source.groupBy(value -> value % 2 == 0 ? "Even" : "Odd");groupedFlux.subscribe(group -> {String key = group.key();group.subscribe(value -> System.out.println(key + ": " + value));});}
}

32. 使用 Reactor 的 concatMap 方法进行顺序操作

concatMap 方法用于对数据流中的元素进行顺序操作,并保持元素的相对顺序。

import reactor.core.publisher.Flux;public class ReactorConcatMapExample {public static void main(String[] args) {Flux<Integer> source = Flux.range(1, 3);// 对每个元素进行异步操作,保持相对顺序source.concatMap(value -> Flux.just(value * 2).log()).subscribe(System.out::println);}
}

33. 使用 Reactor 的 block 方法获取结果

在某些情况下,可以使用 block 方法来阻塞等待数据流的完成,并获取最终结果。

import reactor.core.publisher.Flux;public class ReactorBlockExample {public static void main(String[] args) {Flux<Integer> source = Flux.range(1, 3);// 阻塞等待数据流的完成,并获取最终结果Integer result = source.reduce((x, y) -> x + y).block();System.out.println("Sum: " + result); // 输出: Sum: 6}
}

35. 使用 Reactor 的 doFinally 方法进行清理操作

doFinally 方法用于在数据流完成时执行清理操作,无论是正常完成还是发生错误。

import reactor.core.publisher.Flux;public class ReactorDoFinallyExample {public static void main(String[] args) {Flux<Integer> source = Flux.range(1, 3);source.doFinally(signalType -> System.out.println("Finally: " + signalType)).subscribe(System.out::println);}
}

36. 使用 Reactor 的 log 方法进行日志记录

log 方法用于在操作链中添加日志记录,以便更好地了解数据流的处理过程。

import reactor.core.publisher.Flux;public class ReactorLogExample {public static void main(String[] args) {Flux<Integer> source = Flux.range(1, 3);source.log().subscribe(System.out::println);}
}

37. 使用 Reactor 的 create 方法创建自定义 Publisher

create 方法用于创建自定义的 FluxMono,通过编程方式发射元素和控制订阅。

import reactor.core.publisher.Flux;public class ReactorCreate2Example {public static void main(String[] args) {Flux<Integer> customFlux = Flux.create(emitter -> {for (int i = 1; i <= 5; i++) {emitter.next(i);}emitter.complete();});customFlux.subscribe(System.out::println);}
}

38. 使用 Reactor 的 sample 方法进行采样操作

sample 方法用于在固定的时间间隔内从数据流中采样元素。

import reactor.core.publisher.Flux;import java.time.Duration;public class ReactorSampleExample {public static void main(String[] args) throws InterruptedException {Flux<Integer> source = Flux.range(1, 10).delayElements(Duration.ofSeconds(1)); // 模拟延迟;// 在2秒钟采样一个元素source.sample(Duration.ofSeconds(2)) //数据源1秒一个,采用2秒一次。会漏掉部分数据.subscribe(System.out::println);// 阻塞主线程,让采样执行完Thread.sleep(233333);}
}

41. 使用 Reactor 的 limitRate 方法进行限流

limitRate 方法用于限制数据流的速率,防止快速生产者导致的资源耗尽。

import reactor.core.publisher.Flux;public class ReactorLimitRateExample {public static void main(String[] args) {Flux<Integer> source = Flux.range(1, 1000).log();// 限制数据流的速率为每秒产生100个元素source.limitRate(100)  //一次预取100个元素; 第一次 request(100),以后request(75) (100*75=75).subscribe(data -> {// 模拟慢速消费者try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(data);},error -> System.err.println("Error: " + error),() -> System.out.println("Done"));}
}

学习打卡day08:响应式编程Reactor API大全(中)


文章转载自:

http://t6al20kQ.ysrcf.cn
http://gdO7Wl5h.ysrcf.cn
http://8YXUlXDu.ysrcf.cn
http://SQTfDQKI.ysrcf.cn
http://t9GXkwuJ.ysrcf.cn
http://YLhN3FHr.ysrcf.cn
http://HuRA137f.ysrcf.cn
http://V7x4Njzn.ysrcf.cn
http://1hUWmg7f.ysrcf.cn
http://PNWmDsmh.ysrcf.cn
http://jerWXney.ysrcf.cn
http://knGPp7gI.ysrcf.cn
http://8OKh1bHl.ysrcf.cn
http://B01Hfxxm.ysrcf.cn
http://hDMo4FbJ.ysrcf.cn
http://Gl6gnmHL.ysrcf.cn
http://GIN8NB56.ysrcf.cn
http://c6ejk515.ysrcf.cn
http://FUBMhHQF.ysrcf.cn
http://PW0EBzmL.ysrcf.cn
http://kn46oGqU.ysrcf.cn
http://FdHJSE7P.ysrcf.cn
http://i1UVM9GI.ysrcf.cn
http://WOHUaR7g.ysrcf.cn
http://HuC5lkmF.ysrcf.cn
http://ck2z77rQ.ysrcf.cn
http://OweL9csv.ysrcf.cn
http://ViVliaoJ.ysrcf.cn
http://BOi20HqA.ysrcf.cn
http://8TLM7Vqy.ysrcf.cn
http://www.dtcms.com/wzjs/631917.html

相关文章:

  • 哈尔滨公司网页制作seo外链建设的方法
  • 网站的建设运营收费是哪些ppt制作手机版
  • 湛江建站程序35互联网站建设怎么样
  • 怎样做免费外贸网站wordpress的asp版
  • 江苏省建设工程管理局网站wordpress主题 大
  • 做网站域名的设置网站制作大概需要多少钱
  • 学校网站建设流程wordpress创建空白网页
  • 建设部网站继续教育专业百度seo排名优化
  • 新浪网站首页wordpress前端投稿插件
  • 佛山自定义网站建设各大网站投稿邮箱
  • 南阳市宛城区建设局网站seo推广的特点
  • 东莞网站设计品牌公司如何建设一个网站
  • 做网站运营好还是SEO好做酒类直供网站行吗
  • php 公司网站做网站被坑
  • 中国建设工程协会网站微信怎么弄自己的公众号
  • 贵州建设厅考试网站安全员网站建设怎么设计更加吸引人
  • 苏州网站建站aso排名优化
  • 成都网站建设贴吧哪个网站平面设计做的好
  • 中国建设企业银行app下载宁波seo营销
  • 餐饮美食网站源码wordpress表格布局插件
  • 中学网站模板微博嵌入wordpress
  • 江西建设银行官方网站小程序商城哪家好排行榜
  • 建网站公司公司在哪里找给公司做网站优化的人
  • 软件开发网站建设维护运营网站要多少费用
  • 重庆微网站建设哪家好wordpress 去掉骄傲的
  • 邯郸网站建设怎么做免费外贸网站制作
  • 做网站编辑的时候没保存怎么欢迎访问陕西省交通建设集团公司网站
  • 永州市建设网站网站能调用一些字体
  • 网站的推广方案的内容有哪些网站管理员是什么意思
  • 科技网站模板老外做牛排的视频网站