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

What's the use and meaning of second type parameter in akka.Source

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

示例代码:-

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

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

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

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
    }
}