martyn
martyn

Reputation: 146

Akka Stream DSL graph KillSwitch

I am playing with Akka Streams and streaming content from a file using Alpakka. I need to stop the stream after some time so I want to use KillSwitch. But I don't know how to use it because I am using the graph DSL.

My graph looks like this:

val graph = RunnableGraph.fromGraph(GraphDSL.create() { implicit builder: GraphDSL.Builder[NotUsed] =>
  import GraphDSL.Implicits._

  source ~> mainFlow ~> sink

  ClosedShape
})

graph.run()

I found a solution here: How to abruptly stop an akka stream Runnable Graph?

However, I don't know how to apply it if I'm using the graph DSL. Can you give me some advice?

Upvotes: 3

Views: 1276

Answers (1)

Stefano Bonetti
Stefano Bonetti

Reputation: 9023

To surface a materialized value in the GraphDSL, you can pass the stage that materialized to that value to the create method. It is easier explained with an example. In your case:

  val switch = KillSwitches.single[Int]

  val graph: RunnableGraph[UniqueKillSwitch] =
    RunnableGraph.fromGraph(GraphDSL.create(switch) { implicit builder: GraphDSL.Builder[UniqueKillSwitch] => sw =>
    import GraphDSL.Implicits._

    source ~> mainFlow ~> sw ~> sink

    ClosedShape
  })

  val ks = graph.run()
  ks.shutdown()

Upvotes: 5

Related Questions