Search code examples
scalaakkaakka-stream

Is it possible to create a Flow with Akka-stream that can switch between 2 different inner Shapes?


I would like to have a complex Flow that I can switch inside it between 2 different Shapes depending on the data that is flowing into the graph. When we return a ClosedShape the graph is static but when we return FlowShape I was wondering if it is possible to create some kind of dynamic flow inside it. I was looking at this question and it seems that they use a Partition which I don't know how to apply or if it actually solves my problem.

I started with this example and I am stuck on the comment in the code.

import akka.actor.ActorSystem
import akka.stream.FlowShape
import akka.stream.scaladsl.{Flow, GraphDSL, Sink, Source}

import scala.concurrent.duration._

object StreamOpenGraphsWithMultipleFlows extends App {

  run()

  def run() = {
    implicit val system = ActorSystem("StreamOpenGraphsWithMultipleFlows")

    val fastSource = Source(1 to 1000).throttle(50, 1 second)
    val slowSource = Source(1 to 1000).throttle(5, 1 second)
    val INC = 5
    val MULTI = 10
    val DIVIDE = 2

    val incrementer = Flow[Int].map { x =>
      val result = x + INC
      print(s" | incrementing $x + $INC -> $result")
      result
    }
    val multiplier = Flow[Int].map { x =>
      val result = x * MULTI
      print(s" | multiplying $x * $MULTI -> $result")
      result
    }
    val divider = Flow[Int].map { x =>
      val result = x / DIVIDE
      print(s" | dividing $x / $DIVIDE -> $result")
      result
    }

    def isMultipleOf(value: Int, multiple: Int): Boolean = (value % multiple) == 0

    // Step 1 - setting up the fundamental for a stream graph
    val complexFlowIncrementer = Flow.fromGraph(
      GraphDSL.create() { implicit builder =>
        import GraphDSL.Implicits._
        // Step 2 - add necessary components of this graph
        val incrementerShape = builder.add(incrementer)
        val multiplierShape = builder.add(multiplier)

        // println(s"builder.materializedValue: ${builder.materializedValue}")

        // Step 3 - tying up the components
        incrementerShape ~> multiplierShape
        // BUT I WOULD LIKE TO DO SOMETHING AS BELOW
        // if (isMultipleOf(value???, 10)) incrementerShape ~> divider
        // else incrementerShape ~> multiplierShape

        // Step 4 - return the shape
        FlowShape(incrementerShape.in, multiplierShape.out)
      }
    )
    // run the graph and materialize it
    val graph = slowSource
      .via(complexFlowIncrementer)
      .to(Sink.foreach(x => println(s" | result: $x")))
    graph.run()
  }
}

Solution

  • This blog post shows code samples how to achieve that, so in your case you'd need sth along those lines:

    val complexFlowIncrementer = Flow.fromGraph(
          GraphDSL.create() { implicit builder =>
            import GraphDSL.Implicits._
            // Step 2 - add necessary components of this graph
            val incrementerShape = builder.add(incrementer)
            val multiplierShape = builder.add(multiplier)
            val dividerShape = builder.add(divider)
            
            //add partition and merge
            val partition = builder.add(Partition[Int](2, if(isMultipleOf(_, 10)) 0 else 1)
            val merge = builder.add(Merge[Int](2))
    
            // println(s"builder.materializedValue: ${builder.materializedValue}")
    
            // Step 3 - tying up the components
            incrementerShape ~> partition
            partition.out(0) ~> dividerShape ~> merge.in(0)
            partition.out(1) ~> multiplierShape ~> merge.in(1)
           
            // Step 4 - return the shape
            FlowShape(incrementerShape.in, merge.out)
          }
        )