Akka Stream 中的 Via/ViaMat/to/toMat

编程入门 行业动态 更新时间:2024-10-10 08:21:00
本文介绍了Akka Stream 中的 Via/ViaMat/to/toMat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有人能解释清楚这 4 种方法有什么区别吗?什么时候使用更合适?另外一般来说这组方法的名称是什么?是否有更多方法可以完成相同的工作?scaladoc 的链接也有帮助.

-D-

解决方案

所有这些方法都是将两个流合并为一个流所必需的.例如,您可以从 Source 和 Flow 中创建一个 Source,或者您可以创建一个 Sink一个Flow和一个Sink,或者你可以用两个Flow创建一个Flow.>

为此,有两个基本操作,to 和via.前者允许将 Source 或 Flow 连接到 Sink,而后者允许连接 Sourcecode> 或 Flow 到 Flow:

source.to(sink) ->可运行图flow.to(sink) ->下沉source.via(flow) ->来源flow1.via(flow2) ->流动

作为参考,可运行图是一个完全连接的反应流,可以被物化和执行.

*Mat 各种操作的版本允许指定操作中包含的流的物化值应如何组合.您可能知道,每个流都有一个物化值,该值可以在流物化时获得.例如,Source.queue 生成一个队列对象,程序的另一部分可以使用该对象将元素发送到正在运行的流中.

默认情况下,源和流上的 to 和 via 只保留它被调用的流的物化值,忽略其参数的物化值:

source.to(sink) 产生源的 mat.valuesource.via(flow) 产生源的 mat.valueflow.to(sink) 产生流的 mat.valueflow1.via(flow2) 产生 flow1 的 mat.value

然而,有时您需要保留两个具体化的值或以某种方式将它们组合起来.这就是需要 Mat 方法变体的时候.它们允许您指定组合函数,该函数采用两个操作数的物化值并返回组合流的物化值:

source.to(sink) 等价于 source.toMat(sink)(Keep.left)flow1.via(flow2) 相当于 flow1.viaMat(flow2)(Keep.left)

例如,要保留两个物化值,可以使用 Keep.both 方法,或者如果您只需要正确"操作数的 mat.value,则可以使用 Keep.right 方法:

source.toMat(sink)(Keep.both) 产生一个元组(mat.value of source,mat.value of sink)

Can someone explain clearly what are the difference between those 4 methods ? When is it more appropriate to use each one ? Also generally speaking what is the name of this Group of method? Are there more method that does the same job ? A link to the scaladoc could also help.

-D-

解决方案

All these methods are necessary to join two streams into one stream. For example, you can create a Source out of a Source and a Flow, or you can create a Sink out of a Flow and a Sink, or you can create a Flow out of two Flows.

For this, there are two basic operations, to and via. The former allows one to connect either a Source or a Flow to a Sink, while the latter allows to connect a Source or a Flow to a Flow:

source.to(sink) -> runnable graph flow.to(sink) -> sink source.via(flow) -> source flow1.via(flow2) -> flow

For the reference, a runnable graph is a fully connected reactive stream which is ready to be materialized and executed.

*Mat versions of various operations allow one to specify how materialized values of streams included in the operation should be combined. As you may know, each stream has a materialized value which can be obtained when the stream is materialized. For example, Source.queue yields a queue object which can be used by another part of your program to emit elements into the running stream.

By default to and via on sources and flows only keeps the materialized value of the stream it is called on, ignoring the materialized value of its argument:

source.to(sink) yields mat.value of source source.via(flow) yields mat.value of source flow.to(sink) yields mat.value of flow flow1.via(flow2) yields mat.value of flow1

Sometimes, however, you need to keep both materialized values or to combined them somehow. That's when Mat variants of methods are needed. They allow you to specify the combining function which takes materialized values of both operands and returns a materialized value of the combined stream:

source.to(sink) equivalent to source.toMat(sink)(Keep.left) flow1.via(flow2) equivalent to flow1.viaMat(flow2)(Keep.left)

For example, to keep both materialized values, you can use Keep.both method, or if you only need the mat.value of the "right" operand, you can use Keep.right method:

source.toMat(sink)(Keep.both) yields a tuple (mat.value of source, mat.value of sink)

更多推荐

Akka Stream 中的 Via/ViaMat/to/toMat

本文发布于:2023-11-25 10:11:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629340.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Stream   Akka   toMat   ViaMat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!