我需要编写一个GraphStage但遇到了一些问题。 我已经将代码浓缩到下面,希望你们能为我解释一下。
下面的示例代码不是我的真实用例,它只是在这里证明我的观点。希望这是我不了解akka流而不是它的局限性。
示例代码使用WrapFlowShape构建一个Graph,并基本上重定向" in"附图流中的图形和" out"图表的流程图。
import akka.actor.ActorSystem
import akka.stream._
import akka.stream.javadsl.RunnableGraph
import akka.stream.scaladsl.{Flow, GraphDSL, Sink, Source}
import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler}
import scala.collection.immutable
import scala.io.StdIn
object WrapFlowSandbox extends App {
case class WrapFlowShape[I, O](
in: Inlet[I],
out: Outlet[O],
flowIn: Inlet[O],
flowOut: Outlet[I]) extends Shape {
val inlets: immutable.Seq[Inlet[_]] = in :: flowIn :: Nil
val outlets: immutable.Seq[Outlet[_]] = out :: flowOut :: Nil
def deepCopy = WrapFlowShape(in.carbonCopy, out.carbonCopy, flowIn.carbonCopy, flowOut.carbonCopy)
}
class WrapFlow[I, O] extends GraphStage[WrapFlowShape[I, O]] {
val in: Inlet[I] = Inlet[I]("WrapFlow.in")
val out: Outlet[O] = Outlet[O]("WrapFlow.out")
val flowIn: Inlet[O] = Inlet[O](s"Select.flowIn")
val flowOut: Outlet[I] = Outlet[I](s"Select.flowOut")
val shape: WrapFlowShape[I, O] = WrapFlowShape(in, out, flowIn, flowOut)
def createLogic(initialAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) {
var inElem: I = _
setHandler(in, new InHandler {
def onPush = {
println("2 in.onPush")
inElem = grab(in)
pull(flowIn)
}
})
setHandler(out, new OutHandler {
def onPull = {
println("1 out.onPull")
pull(in)
}
})
setHandler(flowIn, new InHandler {
def onPush = {
println("4 flowIn.onPush")
val outElem = grab(flowIn)
push(out, outElem)
}
})
setHandler(flowOut, new OutHandler {
def onPull = {
println("3 flowOut.onPull")
push(flowOut, inElem)
}
})
}
}
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
val flow = Flow[Int].map(_ + 1)
RunnableGraph.fromGraph(GraphDSL.create() { implicit b =>
import GraphDSL.Implicits._
val select = b.add(new WrapFlow[Int, Int])
Source.single(1) ~> select.in
select.out ~> Sink.foreach[Int](r => println(s"result = $r"))
select.flowOut ~> flow ~> select.flowIn
ClosedShape
}).run(materializer)
StdIn.readLine
system.terminate
}
我希望看到的输出是:
1 out.onPull
2 in.onPush
3 flowOut.onPull
4 flowIn.onPush
result = 2
但实际输出只是前3行:
1 out.onPull
2 in.onPush
3 flowOut.onPull
InHandler.onPush()for" flowIn"永远不会被称为。
我知道以这种方式编写GraphStage是不合常规的,但我确实需要它。
让我感到困惑的是,我通过在步骤2(pull(flowIn))中拉动它来产生对附加流的需求, 而附加的流程反过来又产生了对流量的需求。在第3步。但在步骤3中通过flowOut推送元素后,元素从未被推送,因此从未执行过步骤4。
为什么会这样?
如果附加流量感知到下游需求并在步骤3生成上游需求,为什么在步骤3推送的元素没有通过附加流?
答案 0 :(得分:1)
不确定我是否遵循处理程序中的逻辑。我根据您对(V1, V2) => V
内容的理解将其修改为以下内容:
GraphDSL.create()
执行它应该产生以下输出:
def createLogic(initialAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) {
var inElem: I = _
setHandler(in, new InHandler {
def onPush = {
println("in.onPush")
inElem = grab(in)
push(flowOut, inElem)
}
})
setHandler(out, new OutHandler {
def onPull = {
println("out.onPull")
pull(flowIn)
}
})
setHandler(flowIn, new InHandler {
def onPush = {
println("flowIn.onPush")
val outElem = grab(flowIn)
push(out, outElem)
}
})
setHandler(flowOut, new OutHandler {
def onPull = {
println("flowOut.onPull")
pull(in)
}
})
}
注意到out.onPull
flowOut.onPull
in.onPush
flowIn.onPush
result = 2
案例类(不是抽象类)中的方法copyFromPorts()
未被覆盖。我相信你需要用以下内容覆盖它:
WrapFlowShape