[Akka-stream] question about building graph

32 views
Skip to first unread message

Leon Ma

unread,
Nov 25, 2015, 4:09:27 AM11/25/15
to Akka User List
Hi, 

I have some questions regarding below code:

 val myFlow: Flow[A, B, Any] =
    Flow.fromGraph(FlowGraph.create() { implicit b =>
      import akka.stream.scaladsl.FlowGraph.Implicits._

      val broadcast = b.add(Broadcast[A](2))
      val merge = b.add(Merge[B](2))

      val flow1 = Flow[A].map(...)
      val flow2 = b.add(Flow[A].map(...))

      broadcast ~> flow1  ~> merge
      broadcast ~> flow2  ~> merge

      // expose ports
      FlowShape(broadcast.in , merge.out)
    })


Question:

What's the purpose of b.add(...)?  What's the difference if I don't use it, just like flow1?

Thanks
Leon


Akka Team

unread,
Nov 25, 2015, 5:46:25 AM11/25/15
to Akka User List
Hi Leon,

Have you tried not adding a broadcast and try to use it inline?

-Endre

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Akka Team
Typesafe - Reactive apps on the JVM
Blog: letitcrash.com
Twitter: @akkateam
Message has been deleted

Akka Team

unread,
Nov 26, 2015, 4:23:04 AM11/26/15
to Akka User List


On Thu, Nov 26, 2015 at 8:30 AM, Leon Ma <tutu...@gmail.com> wrote:
I don't quite catch the word "inline"

I answered to your original question, why "add" is needed. I wanted to hint you to try not adding a Broadcast and try it out that way :) (to help figure out yourself)

Anyway, the simple answer is that every component used in the graph needs to be added. The only difference is with flows, sinks and sources is that the library can do it automatically for you, since you don't need to refer to that flow in a second expression.

To fully wire a broadcast you need two expressions at least, because you cannot wire both input ports in one expression. Therefore you need a stable name (a val) to be able to refer to it in those two expressions. This is not necessary for a flow, which can be wired in one expression: out ~> flow ~> in. It is actually equivalent to out ~> b.add(flow) ~> in. We considered removing the auto-import feature and requiring explicit addition for everything.

-Endre

 

Let me make it a little complex:

Assuming I have a source, for each element A of the source:

if(A.property1 == 1) goes to flow1 + flow2 + flow3
if(A.property1 ==2) goes to  flow4 + flow5
if(A.property1 == 3) goes to flow6 + flow7 + flow8 + flow9
...
...




It seems what I want is a black box composite flow that encapsulate all above sub flows and output them in orders.

How should I do branching for above cases?



Leon


在 2015年11月25日星期三 UTC-8上午2:46:25,Akka Team写道:

Leon Ma

unread,
Nov 27, 2015, 1:24:20 AM11/27/15
to Akka User List
Ah, got it!

Thanks a lot for the explanation!

Leon


在 2015年11月26日星期四 UTC-8上午1:23:04,Akka Team写道:
Reply all
Reply to author
Forward
0 new messages