run 和 runWith 之間的主要區別是什么:-
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Keep, Sink, Source}
object RunAndRunWith extends App {
implicit val system: ActorSystem = ActorSystem("Run_RunWith")
implicit val materializer: ActorMaterializer = ActorMaterializer()
Source(1 to 10).toMat(Sink.foreach[Int](println))(Keep.right).run()
Source(1 to 10).runWith(Sink.foreach[Int](println))
}
如何知道使用哪一個?
uj5u.com熱心網友回復:
to(Sink)并toMat(Sink)使用接收器終止源并生成一個RunnableGraph,您可以執行它,run()但它也讓您有機會在運行它之前為整個圖形設定流屬性,或者將它傳遞給將運行它的其他函式/方法(或可能會用它做其他事情而不是執行它)。
如果需要,此表單還可以讓您控制物化值的來源。
由于想要終止并運行帶有接收器的源,而不需要任何附加屬性,因此保持接收器的物化值是如此常見,runWith(Sink)這是一個方便的捷徑。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/535799.html
標籤:斯卡拉阿卡akka流
