+doc #15239: Stream cookbook

This commit is contained in:
Endre Sándor Varga 2014-12-08 17:29:40 +01:00
parent ef2835d60e
commit 2c01bed1a7
19 changed files with 1629 additions and 0 deletions

View file

@ -0,0 +1,61 @@
package docs.stream.cookbook
import akka.event.Logging
import akka.stream.scaladsl.{ Sink, Source, Flow }
import akka.testkit.{ EventFilter, TestProbe }
class RecipeLoggingElements extends RecipeSpec {
"Simple logging recipe" must {
"work with println" in {
val printProbe = TestProbe()
def println(s: String): Unit = printProbe.ref ! s
val mySource = Source(List("1", "2", "3"))
//#println-debug
val loggedSource = mySource.map { elem => println(elem); elem }
//#println-debug
loggedSource.runWith(Sink.ignore)
printProbe.expectMsgAllOf("1", "2", "3")
}
"work with PushStage" in {
val mySource = Source(List("1", "2", "3"))
//#loggingadapter
import akka.stream.stage._
class LoggingStage[T] extends PushStage[T, T] {
private val log = Logging(system, "loggingName")
override def onPush(elem: T, ctx: Context[T]): Directive = {
log.debug("Element flowing through: {}", elem)
ctx.push(elem)
}
override def onUpstreamFailure(cause: Throwable,
ctx: Context[T]): TerminationDirective = {
log.error(cause, "Upstream failed.")
super.onUpstreamFailure(cause, ctx)
}
override def onUpstreamFinish(ctx: Context[T]): TerminationDirective = {
log.debug("Upstream finished")
super.onUpstreamFinish(ctx)
}
}
val loggedSource = mySource.transform(() => new LoggingStage)
//#loggingadapter
EventFilter.debug(start = "Element flowing").intercept {
loggedSource.runWith(Sink.ignore)
}
}
}
}