Akka Streams:无参数 GraphDSL.create() 与 GraphDSL.create(sink)
Akka Streams: No-arg GraphDSL.create() vs GraphDSL.create(sink)
doc 有以下示例(仅显示与我的问题相关的内容):
val resultSink = Sink.head[Int]
val g = RunnableGraph.fromGraph(GraphDSL.create(resultSink) { implicit b => sink =>
import GraphDSL.Implicits._
// importing the partial graph will return its shape (inlets & outlets)
val pm3 = b.add(pickMaxOfThree)
Source.single(1) ~> pm3.in(0)
Source.single(2) ~> pm3.in(1)
Source.single(3) ~> pm3.in(2)
pm3.out ~> sink.in
ClosedShape
})
我很好奇为什么接收器必须作为参数传递给GraphDSL.create
所以我稍微修改了这个例子
val resultSink = Sink.head[Int]
val g = RunnableGraph.fromGraph(GraphDSL.create() { implicit b =>
import GraphDSL.Implicits._
// importing the partial graph will return its shape (inlets & outlets)
val pm3 = b.add(pickMaxOfThree)
val s = b.add(resultSink).in
Source.single(1) ~> pm3.in(0)
Source.single(2) ~> pm3.in(1)
Source.single(3) ~> pm3.in(2)
pm3.out ~> s
ClosedShape
})
但是,这会将 g.run()
的 return 类型从 Future[Int]
更改为 akka.NotUsed
。为什么?
我想我自己找到了答案。累积到文档:
using builder.add(...), an operation that will make a copy of the
blueprint that is passed to it and return the inlets and outlets of
the resulting copy so that they can be wired up. Another alternative
is to pass existing graphs—of any shape—into the factory method that
produces a new graph. The difference between these approaches is that
importing using builder.add(...) ignores the materialized value of the
imported graph while importing via the factory method allows its
inclusion
g.run
returns 图形的物化值,因此 return 类型发生变化。
doc 有以下示例(仅显示与我的问题相关的内容):
val resultSink = Sink.head[Int]
val g = RunnableGraph.fromGraph(GraphDSL.create(resultSink) { implicit b => sink =>
import GraphDSL.Implicits._
// importing the partial graph will return its shape (inlets & outlets)
val pm3 = b.add(pickMaxOfThree)
Source.single(1) ~> pm3.in(0)
Source.single(2) ~> pm3.in(1)
Source.single(3) ~> pm3.in(2)
pm3.out ~> sink.in
ClosedShape
})
我很好奇为什么接收器必须作为参数传递给GraphDSL.create
所以我稍微修改了这个例子
val resultSink = Sink.head[Int]
val g = RunnableGraph.fromGraph(GraphDSL.create() { implicit b =>
import GraphDSL.Implicits._
// importing the partial graph will return its shape (inlets & outlets)
val pm3 = b.add(pickMaxOfThree)
val s = b.add(resultSink).in
Source.single(1) ~> pm3.in(0)
Source.single(2) ~> pm3.in(1)
Source.single(3) ~> pm3.in(2)
pm3.out ~> s
ClosedShape
})
但是,这会将 g.run()
的 return 类型从 Future[Int]
更改为 akka.NotUsed
。为什么?
我想我自己找到了答案。累积到文档:
using builder.add(...), an operation that will make a copy of the blueprint that is passed to it and return the inlets and outlets of the resulting copy so that they can be wired up. Another alternative is to pass existing graphs—of any shape—into the factory method that produces a new graph. The difference between these approaches is that importing using builder.add(...) ignores the materialized value of the imported graph while importing via the factory method allows its inclusion
g.run
returns 图形的物化值,因此 return 类型发生变化。