从 Mono 列表创建 Flux 的正确方法

2024-03-28

假设我有一个使用 CustomObjects 列表的 API 操作。对于其中的每一个对象,它都会调用一个创建 Mono 的服务方法。如何以惯用且非阻塞的方式从这些 Mono 对象创建 Flux?

我现在想到的就是这个。我更改了方法名称以更好地反映其预期目的。

fun myApiMethod(@RequestBody customObjs: List<CustomObject>): Flux<CustomObject> {

    return Flux.create { sink ->
        customObjs.forEach {

            service.persistAndReturnMonoOfCustomObject(it).map {
                sink.next(it)
            }
        }
        sink.complete()
    }
}

此外,我是否需要订阅通量才能真正使其返回某些内容?


我相信你可以使用concat()反而:

/**
 * Concatenate all sources provided as a vararg, forwarding elements emitted by the
 * sources downstream.
 * <p>
 * Concatenation is achieved by sequentially subscribing to the first source then
 * waiting for it to complete before subscribing to the next, and so on until the
 * last source completes. Any error interrupts the sequence immediately and is
 * forwarded downstream.
 * <p>
 * <img class="marble" src="https://raw.githubusercontent.com/reactor/reactor-core/v3.1.3.RELEASE/src/docs/marble/concat.png" alt="">
 * <p>
 * @param sources The {@link Publisher} of {@link Publisher} to concat
 * @param <T> The type of values in both source and output sequences
 *
 * @return a new {@link Flux} concatenating all source sequences
 */
@SafeVarargs
public static <T> Flux<T> concat(Publisher<? extends T>... sources) {

Or merge():

/**
 * Merge data from {@link Publisher} sequences contained in an array / vararg
 * into an interleaved merged sequence. Unlike {@link #concat(Publisher) concat},
 * sources are subscribed to eagerly.
 * <p>
 * <img class="marble" src="https://raw.githubusercontent.com/reactor/reactor-core/v3.1.3.RELEASE/src/docs/marble/merge.png" alt="">
 * <p>
 * Note that merge is tailored to work with asynchronous sources or finite sources. When dealing with
 * an infinite source that doesn't already publish on a dedicated Scheduler, you must isolate that source
 * in its own Scheduler, as merge would otherwise attempt to drain it before subscribing to
 * another source.
 *
 * @param sources the array of {@link Publisher} sources to merge
 * @param <I> The source type of the data sequence
 *
 * @return a merged {@link Flux}
 */
@SafeVarargs
public static <I> Flux<I> merge(Publisher<? extends I>... sources) {
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 Mono 列表创建 Flux 的正确方法 的相关文章

随机推荐

  • 服务器端 Web 应用程序的 Google Oauth2 的 CORS 问题

    我在SO上提到了这个问题 Google oauth 400 响应 请求的资源上不存在 Access Control Allow Origin 标头 https stackoverflow com questions 36688035 goo
  • 无法在 RavenDB 2.x 中创建新数据库

    Noob RavenDB 问题 刚刚下载了 Raven 2 x 并将其设置为在 IIS 中运行 只是尝试通过 Studio 在 RavenDB 2 x 中创建一个新的数据库 我的配置中有以下设置
  • Java 8 - CrudRepository 类型中的方法 save(S) 不适用于参数(Optional

    我开始使用 spring 框架进行 java 开发 为了获得比 hello world 更复杂的东西 我找到了本教程并尝试遵循 https www toptal com spring beginners guide to mvc with
  • 为什么在 Java 中右移 16 乘以 32 结果是 16 而不是 0? 16>>32 = 16 为什么? [复制]

    这个问题在这里已经有答案了 我在java中使用右移运算符时遇到了一个奇怪的情况 当我右移 16 乘 31 时 结果为 0 但是尝试右移 16 乘 32 时 它本身仍然是 16 有人可以解释一下吗 因为我对此感到疯狂 public class
  • 如何将对话框指令与 Alexa 的 java SDK 一起使用

    我正在尝试使用 java 技能套件创建我自己的 Alexa 技能 并且我想使用对话框界面 我已经使用测试版的技能生成器创建了我的对话模型 但现在我不明白我需要通过我的网络服务返回什么才能委托我的对话 我应该使用哪个类向 Alexa 发送命令
  • WP7 TimePicker 选择 24 小时格式的时间

    有没有办法让WP7的TimePicker允许用户选择24小时格式的时间 如果我将 TimePicker ValueStringFormat 设置为 0 HH mm ss 它会以 24 小时格式显示 但当我单击它时 输入时间仍为 12 小时格
  • 编译器预处理期间的数学运算

    我经常遇到这样的情况 我需要在编译时生成几个常量以用于移位和屏蔽操作 e g define blockbits 8 define blocksize 256 could be generated from 2 blockbits defin
  • 字母数字会包含 _ 和空格吗?

    如果字段定义为字母数字 是否允许使用空格和下划线 我希望他们不是 有人能证实吗 根据定义 字母数字字符仅包含字母 A 到 Z 和数字 0 到 9 空格和下划线通常被视为标点符号 因此不应该允许使用它们 如果某个字段明确指出 字母数字字符 空
  • Perl 中有 inf 常量吗?

    我正在为算法初始化一个无穷大的列表 写作 x 9 9 9感觉不直观 而且我将来可能想使用 BigInt 1 0抛出错误 获得的规范方法是什么inf 您可以使用特殊字符串 inf perl E say inf 1 inf perl E say
  • .htaccess 用于 HTML5 模式的 AngularJS 应用程序的子文件夹

    概述 我有一个 AngularJS 应用程序 它使用 locationProvider html5Mode true 它由 Apache 服务器提供 到目前为止 我使用的源代码是从其他人那里访问的 我只需要重定向到 index html 以
  • 为什么 EnumerateFiles 比计算大小快得多

    对于我的 WPF 项目 我必须计算单个目录 可能有子目录 中的总文件大小 Sample 1 DirectoryInfo di new DirectoryInfo path var totalLength di EnumerateFiles
  • Android 中如何获取当前 Button 文本颜色?

    我不知道如何获取按钮文本的当前颜色 我知道可能是这样 但无法完全弄清楚参数 public static int getTextColor Context context TypedArray attrs int def 基本上我正在尝试这样
  • 渲染问题无法加载LayoutLib:

    渲染问题无法加载LayoutLib com android layoutlib bridge Bridge 详情 org jetbrains android uipreview RenderingException Failed to lo
  • 在 xml 编辑器中在字符串资源的引用和值预览之间切换

    在 Android Studio 中使用 xml 值并引用时 Strings xy例如 我在视频中看到 实际上可以直接在同一编辑器窗口中显示实际值 它是基本上是在之间切换 String xy和 XYContent 但引用仍然存在 我不是指
  • 在服务器上强制 svn:eol-style=native?

    目前 为了保证颠覆性eol style被设定为native对于添加到项目中的每个新文件 我们必须将其添加到 subversion config我们每台开发人员机器上的文件 miscellany enable auto props yes a
  • c# Xamarin UITextField 设置光标位置

    我需要将 UITextField 的光标定位在与另一个先前文本字段完全相同的位置 我将光标在第一个文本字段中的位置设为nint index txtToField GetOffsetFromPosition txtToField Beginn
  • Android 时差与乔达时间

    我有这个简单的代码 使用 Joda time 工作正常 但我有一个问题 比如返回814分钟 通过代码就可以了 但我希望结果少于 60 分钟 而不是 814 分钟 那么 我该如何转换这 814 分钟才能得到我想要的结果呢 同样的情况也发生在几
  • YouTube Live API 流状态和质量回调

    In the Live Control Room of a YouTube Live broadcast I can see a Stream Status view which shows me details of the video
  • 尝试上传到 Amazon S3 时出现“不支持的正文有效负载对象”

    我想将文件从我的前端上传到我的 Amazon S3 AWS 我正在使用 dropzone 因此我转换文件并将其发送到我的后端 在我的后端我的文件是这样的 fieldname file originalname test torrent en
  • 从 Mono 列表创建 Flux 的正确方法

    假设我有一个使用 CustomObjects 列表的 API 操作 对于其中的每一个对象 它都会调用一个创建 Mono 的服务方法 如何以惯用且非阻塞的方式从这些 Mono 对象创建 Flux 我现在想到的就是这个 我更改了方法名称以更好地