created factory method

This commit is contained in:
Endre Sándor Varga 2015-07-09 13:36:54 +02:00
parent 662dd035a0
commit 47ea3fde38
13 changed files with 129 additions and 30 deletions

View file

@ -4,10 +4,9 @@
package docs.stream
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Flow
import akka.stream.scaladsl.{ RunnableGraph, Flow, Sink, Source }
import akka.stream.testkit._
import akka.stream.scaladsl.Sink
import akka.stream.scaladsl.Source
import org.reactivestreams.Processor
class ReactiveStreamsDocSpec extends AkkaSpec {
import TwitterStreamQuickstartDocSpec._
@ -78,11 +77,10 @@ class ReactiveStreamsDocSpec extends AkkaSpec {
val storage = impl.storage
//#flow-publisher-subscriber
val (in: Subscriber[Tweet], out: Publisher[Author]) =
authors.runWith(Source.subscriber[Tweet], Sink.publisher[Author])
val processor: Processor[Tweet, Author] = authors.toProcessor.run()
tweets.subscribe(in)
out.subscribe(storage)
tweets.subscribe(processor)
processor.subscribe(storage)
//#flow-publisher-subscriber
assertResult(storage)
@ -135,4 +133,15 @@ class ReactiveStreamsDocSpec extends AkkaSpec {
assertResult(storage)
}
"use a processor" in {
//#use-processor
// An example Processor factory
def createProcessor: Processor[Int, Int] = Flow[Int].toProcessor.run()
val flow: Flow[Int, Int, Unit] = Flow(() => createProcessor)
//#use-processor
}
}

View file

@ -394,7 +394,9 @@ Using an Akka Streams :class:`Flow` we can transform the stream and connect thos
The :class:`Publisher` is used as an input :class:`Source` to the flow and the
:class:`Subscriber` is used as an output :class:`Sink`.
A :class:`Flow` can also be materialized to a :class:`Subscriber`, :class:`Publisher` pair:
A :class:`Flow` can also be also converted to a :class:`RunnableGraph[Processor[In, Out]]` which
materializes to a :class:`Processor` when ``run()`` is called. ``run()`` itself can be called multiple
times, resulting in a new :class:`Processor` instance each time.
.. includecode:: code/docs/stream/ReactiveStreamsDocSpec.scala#flow-publisher-subscriber
@ -424,4 +426,10 @@ by using the Subscriber-:class:`Source`:
.. includecode:: code/docs/stream/ReactiveStreamsDocSpec.scala#sink-subscriber
It is also possible to use re-wrap :class:`Processor` instances as a :class:`Flow` by
passing a factory function that will create the :class:`Processor` instances:
.. includecode:: code/docs/stream/ReactiveStreamsDocSpec.scala#use-processor
Please note that a factory is necessary to achieve reusability of the resulting :class:`Flow`.