Springboot 3.x - Reactive programming

news2024/10/6 3:08:28

一、Preliminary Knowledge

  1. Functional Interface
  2. Lambda expression
  3. Stream API
    1. Intermediate operation
      1. filter:Used to filter elements in a stream
      2. map:One-to-one conversion
      3. flatMap:One-to-many conversion
      4. distinct、sorted、peek、limit、skip、takeWhile…
    2. Terminal operation
      1. collect:toList/toMap/groupingBy

二、Reactor Core

1、Reactive Stream

https://www.reactive-streams.org

Java’s Reactive Streams is a standardized API for asynchronously processing data streams, designed to support backpressure mechanisms to ensure that system resources are not over-consumed due to mismatches in the speeds of producers and consumers during asynchronous data processing. The main goal of Reactive Streams is to provide a compatible and unified API that allows different libraries and frameworks to work seamlessly together, especially when dealing with large-scale data streams.

Core Concepts

The Reactive Streams API is primarily composed of the following four core interfaces:

  1. Publisher:A publisher, the source that produces the data stream.

    // The Publisher interface has only one method, subscribe, which is used to subscribe to the data stream.
    public interface Publisher<T> {
        void subscribe(Subscriber<? super T> s);
    }
    
  2. Subscriber:A subscriber consumes the data stream at the terminal.

    public interface Subscriber<T> {
        // Called when a subscriber subscribes, passing a Subscription object.
        void onSubscribe(Subscription s);
        // Called when a new data item is produced.
        void onNext(T t);
        //  Called when an error occurs.
        void onError(Throwable t);
        // Called when the data stream ends.
        void onComplete();
    }
    
  3. Subscription:A subscription manages the relationship between the publisher and the subscriber, including operations for requesting and canceling data.

    public interface Subscription {
        // Requesting the number of data items.
        void request(long n);
        // Canceling the subscription.
        void cancel();
    }
    
  4. Processor<T, R>:A processor is both a publisher and a subscriber, used to process data within the data stream.

    // The Processor interface inherits from both the Subscriber and Publisher interfaces, indicating that it can act as both a consumer and producer of data.
    public interface Processor<T, R> extends Subscriber<T>, Publisher<R> {
    }
    
Backpressure Mechanism and How It Works

Backpressure is an important concept in Reactive Streams, allowing subscribers to control the rate at which they receive data from publishers to prevent publishers from producing data too quickly for subscribers to handle. The backpressure mechanism is implemented through the request method in the Subscription interface, where subscribers can call this method to specify the number of data items they are able to handle.

The key to the backpressure mechanism lies in the Subscription interface, which provides two methods: request(long n) and cancel(). The main workflow of backpressure is as follows:

  1. Subscribers Request Data: When a subscriber subscribes to a publisher, it receives a Subscription object. The subscriber calls the request method of the Subscription to request the number of data items it can handle.
  2. Publishers Respond to Requests: Based on the subscriber’s request, the publisher sends the specified number of data items to the subscriber. If the subscriber does not request new data items, the publisher will not send data.
  3. Dynamically Adjusting the Requested Quantity: Subscribers can dynamically adjust the number of requested data items based on their processing capabilities to avoid data accumulation.
Backpressure Strategies

In practice, different backpressure strategies can be adopted based on specific needs:

  1. Directly Requesting All Data: If the consumer can handle all the data, it can request all the data at once, but this may lead to high memory usage.
  2. Batch Requesting Data: Request data in batches, each time requesting a batch of data items that can be processed.
  3. Requesting Data on Demand: Dynamically adjust the number of requested data items based on the real-time processing capability of the consumer.
Common Implementations of Backpressure Strategies

Different implementations of Reactive Streams provide a variety of built-in backpressure strategies. For example, Project Reactor and RxJava both offer several backpressure strategies:

  • Buffer: Buffer all data items until the consumer processes them.
  • Drop: Discard new data items, retaining old ones.
  • Latest: Only keep the latest data item, discarding old ones.
  • Error: Throw an error when exceeding the buffer limit.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1904125.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Spring Boot的无缝衔接:深入解析与实践

欢迎来到 破晓的历程的 博客 ⛺️不负时光&#xff0c;不负己✈️ &#x1f680;The begin&#x1f697;点点关注&#xff0c;收藏不迷路&#x1f6a9; 引言 在快速迭代的软件开发环境中&#xff0c;无缝衔接是提升开发效率、降低维护成本、增强系统稳定性的关键。Spring Boo…

嵌入式c语言5——位运算符

<<与>>是c语言中两个移位运算符&#xff0c;分别有乘以2与除以2的意义 位运算符还包括&#xff0c;与&&#xff0c;或|&#xff0c;均进行按位操作 同时&#xff0c;还可以进行取反以及异或操作

【C++】cout.self()函数

&#x1f4e2;博客主页&#xff1a;https://blog.csdn.net/2301_779549673 &#x1f4e2;欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; 如有错误敬请指正&#xff01; &#x1f4e2;本文作为 JohnKi 学习笔记&#xff0c;借鉴了部分大佬案例 &#x1f4e2;未来很长&#…

web前端开发——开发环境和基本知识

今天我来针对web前端开发讲解一些开发环境和基本知识 什么是前端 前端通常指的是网站或者Web应用中用户可以直接与之交互的部分&#xff0c;包括网站的结构、设计、内容和功能。它是软件开发中的一个专业术语&#xff0c;特别是指Web开发领域。前端开发涉及的主要技术包括HTML…

C++语言相关的常见面试题目(一)

1. const关键字的作用 答&#xff1a; 省流&#xff1a;&#xff08;1&#xff09;定义变量&#xff0c;主要为了防止修改 (2) 修饰函数参数&#xff1a;防止在函数内被改变 &#xff08;3&#xff09;修饰函数的返回值 &#xff08;4&#xff09;修饰类中的成员函数 2. Sta…

Windows远程桌面实现之十五:投射浏览器摄像头到xdisp_virt以及再次模拟摄像头(一)

by fanxiushu 2024-07-01 转载或引用请注明原始作者。 本文还是围绕xdisp_virt这个软件展开&#xff0c; 再次模拟成摄像头这个比较好理解&#xff0c;早在很久前&#xff0c;其实xdisp_virt项目中就有摄像头功能&#xff0c; 只是当时是分开的&#xff0c;使用起来…

centos docker 安装mysql:8.0.21 天坑记录

docker pull mysql:8.0.21 安装的mysql 8.0.21 版本&#xff0c;当创建表时只要创建表的字段大于10&#xff0c;就会报错 > 2013 - Lost connection to MySQL server during query 当删除一个字段&#xff0c;刚好9个字段时就可以创建成功&#xff0c;打印等于10个&#…

时间处理的未来:Java 8全新日期与时间API完全解析

文章目录 一、改进背景二、本地日期时间三、时区日期时间四、格式化 一、改进背景 Java 8针对时间处理进行了全面的改进&#xff0c;重新设计了所有日期时间、日历及时区相关的 API。并把它们都统一放置在 java.time 包和子包下。 Java5的不足之处&#xff1a; 非线程安全&…

Nginx auth 的权限验证

基本流程 整个流程为&#xff1b;以用户视角访问API开始&#xff0c;进入 Nginx 的 auth 认证模块&#xff0c;调用 SpringBoot 提供的认证服务。根据认证结果调用重定向到对应的 API 接口或者 404 页面。 查看版本保证有 Nginx auth 模块 由于 OpenAI 或者本身自己训练的一套…

实现多数相加,但是传的参不固定

一、情景 一般实现的加法和减法等简单的相加减函数的话。一般都是写好固定传的参数。比如&#xff1a; function add(a,b) {return a b;} 这是固定的传入俩个&#xff0c;如果是三个呢&#xff0c;有人说当然好办&#xff01; 这样写不就行了&#xff01; function add(a…

前端JS特效第22波:jQuery滑动手风琴内容切换特效

jQuery滑动手风琴内容切换特效&#xff0c;先来看看效果&#xff1a; 部分核心的代码如下&#xff1a; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xm…

Day4 用 rustlings 练习 Rust 语言

大家好 今天 完成 2024年自动驾驶OS开发训练营-初阶营第四期-导学 Day4用 rustlings 练习 Rust 语言 欢迎加入国家智能网联汽车创新中心OS开发训练营大家庭。&#x1f680; 导学阶段启动 在正式开营之前&#xff0c;我们特别设置了导学阶段&#xff0c;旨在帮助大家更好地迎接颇…

万和day01代码分析

将了数据库的多表之间的操作&#xff0c;实际应用到JDBC中去。 一共五张表&#xff0c; info存储的是具体的信息&#xff0c;edu job role 和info都是多对一的关系。 采用的是Java FX&#xff0c;界面采用xml去编写。 项目理解一 在JavaFX中&#xff0c;ObservableList 是一个…

SSL/CA 证书及其相关证书文件解析

在当今数字化的时代&#xff0c;网络安全变得至关重要。SSL&#xff08;Secure Socket Layer&#xff09;证书和CA&#xff08;Certificate Authority&#xff09;证书作为保护网络通信安全的重要工具&#xff0c;发挥着关键作用。 一、SSL证书 SSL证书是数字证书的一种&…

汉诺塔与青蛙跳台阶

1.汉诺塔 根据汉诺塔 - 维基百科 介绍 1.1 背景 最早发明这个问题的人是法国数学家爱德华卢卡斯。 传说越南河内某间寺院有三根银棒&#xff0c;上串 64 个金盘。寺院里的僧侣依照一个古老的预言&#xff0c;以上述规则移动这些盘子&#xff1b;预言说当这些盘子移动完毕&am…

使用Charles mock服务端响应数据

背景 服务端未提供接口/服务端接口返回结果有逻辑限制&#xff08;次数限制&#xff09;&#xff0c;不能通过原始接口返回多次模拟预期的返回结果&#xff0c;例如边界值情况 客户端受到接口响应数据的限制&#xff0c;无法继续开发或测试&#xff0c;会极大影响开发测试效率…

C# WPF 3D 数据孪生 系列六

数字孪生应用开发 应用开发中的布局需求 Grid基本使用 WPF 3D绘图 点云 系列五-CSDN博客 WPF UI 3D 多轴 机械臂 stl 模型UI交互-CSDN博客 WPF UI 3D 基本概念 点线三角面 相机对象 材质对象与贴图 3D地球 光源 变形处理 动作交互 辅助交互插件 系列三-CSDN博客 数字孪生 介…

550kg级大载重长航时无人机直升机技术详解

550kg级大载重长航时无人机直升机&#xff0c;作为一种高性能的无人机系统&#xff0c;具备了多项先进的技术特点&#xff0c;以满足高海拔、高寒等复杂环境下的应用需求。这些无人机直升机通常具备高载重、长航时、强适应性、高可靠性和良好的任务拓展性。 设备由无人直升机平…

刷题之移除元素(leetcode)

移除元素 这题简单题&#xff0c;但是前面思路是先找到左边第一个不是val的&#xff0c;和右边第一个不是val的&#xff0c;进行交换&#xff0c;边界条件没有处理好&#xff0c;导致报错&#xff08;水平真菜&#xff09; 也可以直接把left是val的与right进行交换&#xf…

个人视角,社会影响力:自媒体的魅力所在

随着数字化时代的到来&#xff0c;自媒体正成为信息传播领域的一场革命。个人视角与社会影响力的结合&#xff0c;赋予了自媒体独特的魅力。在传统媒体受限制的同时&#xff0c;自媒体为每个人提供了表达自己观点和思想的自由。个体的真实视角使得自媒体在信息传播中发挥着重要…