gpt4 book ai didi

scala - akka.Source中第二类参数的用途和意义

转载 作者:行者123 更新时间:2023-12-04 09:40:15 25 4
gpt4 key购买 nike

akka.Source中第二类参数的用途和意义是什么?

示例代码:-

  def stream: ServiceCall[Source[String, NotUsed], Source[String, NotUsed]]

根据到目前为止我看到的尽可能多的代码,默认情况下第二个类型参数设置为 akka.NotUsed。但我不知道它有什么意义。

最佳答案

第二个参数是物化值,即当您运行源代码时,它是run 方法返回给您的值。 Akka 流中的所有流形状都有它们,即 sources、sinks、flows、bidiflows 等。对于 sinks,很明显,如果你折叠流,你最终会得到一个值,即值(value)是通过物化值(value)给你的,这是结果的 future :

def fold[U, T](zero: U)(f: (U, T) ⇒ U): Sink[T, Future[U]]

虽然对于消息来源来说,它不太明显,但这里有一个例子:

def actorRef[T](bufferSize: Int, overflowStrategy: OverflowStrategy): Source[T, ActorRef]

这是一个 Source,当您运行它时,它会具体化为一个 ActorRef。您发送给参与者的每条消息都将从源发出。如果你想在 Lagom 中使用它,你可以这样做:

def stream = ServiceCall { _ =>
Source.actorRef[String](16, OverflowStrategy.dropHead)
.mapMaterializedValue { actor =>
// send messages here, or maybe pass the actor to somewhere else
actor ! "Hello world"
// And return NotUsed so that it now materializes to `NotUsed`, as required by Lagom
NotUsed
}
}

关于scala - akka.Source中第二类参数的用途和意义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46499925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com