akka stream - How do I create a Flow with a different input and output types for use inside of a graph? -
i making custom sink building graph on inside. here broad simplification of code demonstrate question:
def mysink: sink[int, unit] = sink() { implicit builder => val entrance = builder.add(flow[int].buffer(500, overflowstrategy.backpressure)) val tostring = builder.add(flow[int, string, unit].map(_.tostring)) val printsink = builder.add(sink.foreach(elem => println(elem))) builder.addedge(entrance.out, tostring.in) builder.addedge(tostring.out, printsink.in) entrance.in }
the problem having while valid create flow same input/output types single type argument , no value argument like: flow[int]
(which on documentation) not valid supply 2 type parameters , 0 value parameters.
according reference documentation flow object apply
method looking defined as
def apply[i, o]()(block: (builder[unit]) ⇒ (inlet[i], outlet[o])): flow[i, o, unit]
and says
creates flow passing flowgraph.builder given create function.
the create function expected return pair of inlet , outlet correspond created flows input , output ports.
it seems need deal level of graph builders when trying make think simple flow. there easier , more concise way create flow changes type of it's input , output doesn't require messing it's inside ports? if right way approach problem, solution like?
bonus: why easy make flow doesn't change type of input it's output?
if want specify both input , output type of flow, indeed need use apply method found in documentation. using it, though, done pretty same did.
flow[string, message]() { implicit b => import flowgraph.implicits._ val reversestring = b.add(flow[string].map[string] { msg => msg.reverse }) val mapstringtomsg = b.add(flow[string].map[message]( x => textmessage.strict(x))) // connect graph reversestring ~> mapstringtomsg // expose ports (reversestring.inlet, mapstringtomsg.outlet) }
instead of returning inlet, return tuple, inlet , outlet. flow can used (for instance inside builder, or directly runwith) specific source or sink.
Comments
Post a Comment