akka.stream.scaladsl.GraphDSL Scala Examples
The following examples show how to use akka.stream.scaladsl.GraphDSL.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: Graph2.scala From fusion-data with Apache License 2.0 | 5 votes |
package example.akkastream.basic import akka.actor.ActorSystem import akka.stream.{ ActorMaterializer, ClosedShape } import akka.stream.scaladsl.{ Broadcast, Flow, GraphDSL, RunnableGraph, Sink, Source } import scala.io.StdIn object Graph2 extends App { implicit val system = ActorSystem() implicit val mat = ActorMaterializer() import system.dispatcher val topHeadSink = Sink.head[Int] val bottomHeadSink = Sink.head[Int] val sharedDoubler = Flow[Int].map(_ * 2) val g = RunnableGraph.fromGraph(GraphDSL.create(topHeadSink, bottomHeadSink)((_, _)) { implicit builder => (topHS, bottomHS) => import GraphDSL.Implicits._ val broadcast = builder.add(Broadcast[Int](2)) Source.single(1) ~> broadcast.in broadcast ~> sharedDoubler ~> topHS.in broadcast ~> sharedDoubler ~> bottomHS.in ClosedShape }) val (topF, bottomF) = g.run() topF.foreach(v => println(s"top is $v")) bottomF.foreach(v => println(s"bottom is $v")) StdIn.readLine() system.terminate() }
Example 2
Source File: PersistentBufferCommitOrderSpec.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.pattern.stream import akka.actor.ActorSystem import akka.stream.scaladsl.{GraphDSL, RunnableGraph} import akka.stream.{ActorMaterializer, ClosedShape} import com.typesafe.config.ConfigFactory import org.scalatest.concurrent.Eventually import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers} import org.squbs.testkit.Timeouts._ import scala.concurrent.Await class PersistentBufferCommitOrderSpec extends FlatSpec with Matchers with BeforeAndAfterAll with Eventually { implicit val system = ActorSystem("PersistentBufferCommitOrderSpec", PersistentBufferSpec.testConfig) implicit val mat = ActorMaterializer() implicit val serializer = QueueSerializer[Int]() import StreamSpecUtil._ override def afterAll = { Await.ready(system.terminate(), awaitMax) } it should "fail when an out of order commit is attempted and commit-order-policy = strict" in { val util = new StreamSpecUtil[Int, Event[Int]] import util._ val buffer = PersistentBufferAtLeastOnce[Int](ConfigFactory.parseString("commit-order-policy = strict").withFallback(config)) val commit = buffer.commit[Int] val streamGraph = RunnableGraph.fromGraph(GraphDSL.create(flowCounter) { implicit builder => sink => import GraphDSL.Implicits._ in ~> buffer.async ~> filterARandomElement ~> commit ~> sink ClosedShape }) val sinkF = streamGraph.run() Await.result(sinkF.failed, awaitMax) shouldBe an[CommitOrderException] clean() } it should "not fail when an out of order commit is attempted and commit-order-policy = lenient" in { val util = new StreamSpecUtil[Int, Event[Int]] import util._ val buffer = PersistentBufferAtLeastOnce[Int](ConfigFactory.parseString("commit-order-policy = lenient").withFallback(config)) val commit = buffer.commit[Int] val streamGraph = RunnableGraph.fromGraph(GraphDSL.create(flowCounter) { implicit builder => sink => import GraphDSL.Implicits._ in ~> buffer.async ~> filterARandomElement ~> commit ~> sink ClosedShape }) val countFuture = streamGraph.run() val count = Await.result(countFuture, awaitMax) count shouldBe elementCount - 1 eventually { buffer.queue shouldBe 'closed } clean() } }
Example 3
Source File: TriggerMerge.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.stream import akka.stream._ import akka.stream.scaladsl.{GraphDSL, Source} import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} final class TriggerMerge[T](eagerComplete: Boolean = false) extends GraphStage[FanInShape2[T, TriggerEvent, T]] { override val shape: FanInShape2[T, TriggerEvent, T] = new FanInShape2[T, TriggerEvent, T]("TriggerMerge") val in: Inlet[T] = shape.in0 val trigger: Inlet[TriggerEvent] = shape.in1 override def initialAttributes = Attributes.name("TriggerMerge") override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) { var flowEnabled, requestPending, triggerCompleted, willShutDown = false setHandler(in, new InHandler { override def onPush(): Unit = { push(out, grab(in)) if (willShutDown) completeStage() } override def onUpstreamFinish(): Unit = { if (!isAvailable(in)) completeStage() willShutDown = true } }) setHandler(trigger, new InHandler { override def onPush(): Unit = { val triggerEvent = grab(trigger) if (triggerEvent.value != flowEnabled) { if (triggerEvent.value && requestPending) { requestPending = false tryPull(in) } flowEnabled = triggerEvent.value } if (triggerCompleted) cancelTrigger() if (!hasBeenPulled(trigger)) tryPull(trigger) } override def onUpstreamFinish(): Unit = { if (!isAvailable(trigger)) cancelTrigger() triggerCompleted = true } def cancelTrigger(): Unit = if (eagerComplete) completeStage() else cancel(trigger) }) setHandler(out, new OutHandler { override def onPull(): Unit = { if (willShutDown) completeStage() else { if (flowEnabled) tryPull(in) else { requestPending = true if (!hasBeenPulled(trigger)) tryPull(trigger) } } } }) } def out: Outlet[T] = shape.out override def toString = "TriggerMerge" } class Trigger[T, M1, M2](eagerComplete: Boolean = false) { import GraphDSL.Implicits._ private[stream] val source = (in: Graph[SourceShape[T], M1], trigger: Graph[SourceShape[TriggerEvent], M2]) => Source.fromGraph( GraphDSL.create(in, trigger)((_, _)) { implicit builder => (sIn, sTrigger) => val merge = builder.add(new TriggerMerge[T](eagerComplete)) sIn ~> merge.in0 sTrigger ~> merge.in1 SourceShape(merge.out) }) // for Java def source(in: javadsl.Source[T, M1], trigger: javadsl.Source[TriggerEvent, M2]): javadsl.Source[T, akka.japi.Pair[M1, M2]] = { source(in.asScala, trigger.asScala).mapMaterializedValue { case (m1, m2) => akka.japi.Pair(m1, m2) }.asJava } }
Example 4
Source File: ProperShutdownStream.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.stream import java.util.concurrent.atomic.AtomicLong import akka.Done import akka.actor.ActorRef import akka.stream.ClosedShape import akka.stream.ThrottleMode.Shaping import akka.stream.scaladsl.{Flow, GraphDSL, Keep, RunnableGraph, Sink, Source} import scala.concurrent.Future import scala.concurrent.duration._ import scala.language.postfixOps object ProperShutdownStream { val genCount = new AtomicLong(0L) } class ProperShutdownStream extends PerpetualStream[(ActorRef, Future[Long])] { import ProperShutdownStream._ import org.squbs.unicomplex.Timeouts._ override def stopTimeout = awaitMax def generator = Iterator.iterate(0){ p => if (p == Int.MaxValue) 0 else p + 1 } map { v => genCount.incrementAndGet() v } val managedSource = LifecycleManaged().source(Source fromIterator generator _) val throttle = Flow[Int].throttle(5000, 1 second, 1000, Shaping) val counter = Flow[Int].map { _ => 1L }.reduce { _ + _ }.toMat(Sink.head)(Keep.right) override def streamGraph = RunnableGraph.fromGraph(GraphDSL.create(managedSource, counter)((a, b) => (a._2, b)) { implicit builder => (source, sink) => import GraphDSL.Implicits._ source ~> throttle ~> sink ClosedShape }) override def receive = { case NotifyWhenDone => val (_, fCount) = matValue // Send back the future directly here, don't map the future. The map will likely happen after ActorSystem // shutdown so we cannot use context.dispatcher as execution context for the map as it won't be there when // the map is supposed to happen. sender() ! fCount } override def shutdown() = { super.shutdown() import context.dispatcher val (actorRef, fCount) = matValue val fStopped = gracefulStop(actorRef, awaitMax) for { _ <- fCount; _ <- fStopped } yield Done } }
Example 5
Source File: KillSwitchStream.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.stream import java.util.concurrent.atomic.AtomicLong import akka.stream.ClosedShape import akka.stream.ThrottleMode.Shaping import akka.stream.scaladsl.{Flow, GraphDSL, Keep, RunnableGraph, Sink, Source} import scala.concurrent.Future import scala.concurrent.duration._ import scala.language.postfixOps object KillSwitchStream { val genCount = new AtomicLong(0L) } class KillSwitchStream extends PerpetualStream[Future[Long]] { import KillSwitchStream._ import org.squbs.unicomplex.Timeouts._ override def stopTimeout = awaitMax def generator = Iterator.iterate(0){ p => if (p == Int.MaxValue) 0 else p + 1 } map { v => genCount.incrementAndGet() v } val source = Source.fromIterator(generator _) val throttle = Flow[Int].throttle(5000, 1 second, 1000, Shaping) val counter = Flow[Int].map { _ => 1L }.reduce { _ + _ }.toMat(Sink.head)(Keep.right) override def streamGraph = RunnableGraph.fromGraph(GraphDSL.create(counter) { implicit builder => sink => import GraphDSL.Implicits._ source ~> killSwitch.flow[Int] ~> throttle ~> sink ClosedShape }) override def receive = { case NotifyWhenDone => // Send back the future directly here, don't map the future. The map will likely happen after ActorSystem // shutdown so we cannot use context.dispatcher as execution context for the map as it won't be there when // the map is supposed to happen. sender() ! matValue } }
Example 6
Source File: KillSwitchWithChildActorStream.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.stream import java.util.concurrent.atomic.AtomicLong import akka.actor.{Actor, Props} import akka.stream.ClosedShape import akka.stream.ThrottleMode.Shaping import akka.stream.scaladsl.{Flow, GraphDSL, Keep, RunnableGraph, Sink, Source} import scala.concurrent.Future import scala.concurrent.duration._ import scala.language.postfixOps object KillSwitchWithChildActorStream { val genCount = new AtomicLong(0L) } class DummyChildActor extends Actor { def receive = PartialFunction.empty } class KillSwitchWithChildActorStream extends PerpetualStream[Future[Long]] { import KillSwitchWithChildActorStream._ import org.squbs.unicomplex.Timeouts._ val dummyChildActor = context.actorOf(Props[DummyChildActor]) override def stopTimeout = awaitMax def generator = Iterator.iterate(0){ p => if (p == Int.MaxValue) 0 else p + 1 } map { v => genCount.incrementAndGet() v } val source = Source.fromIterator(generator _) val throttle = Flow[Int].throttle(5000, 1 second, 1000, Shaping) val counter = Flow[Int].map { _ => 1L }.reduce { _ + _ }.toMat(Sink.head)(Keep.right) override def streamGraph = RunnableGraph.fromGraph(GraphDSL.create(counter) { implicit builder => sink => import GraphDSL.Implicits._ source ~> killSwitch.flow[Int] ~> throttle ~> sink ClosedShape }) override def receive = { case NotifyWhenDone => // Send back the future directly here, don't map the future. The map will likely happen after ActorSystem // shutdown so we cannot use context.dispatcher as execution context for the map as it won't be there when // the map is supposed to happen. sender() ! matValue } override def shutdown() = { val f = super.shutdown() defaultMidActorStop(Seq(dummyChildActor)) f } }
Example 7
Source File: KillSwitchMatStream.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.stream import java.util.concurrent.atomic.AtomicLong import akka.stream.{ClosedShape, KillSwitch, KillSwitches} import akka.stream.ThrottleMode.Shaping import akka.stream.scaladsl.{Flow, GraphDSL, Keep, RunnableGraph, Sink, Source} import scala.concurrent.Future import scala.concurrent.duration._ import scala.language.postfixOps object KillSwitchMatStream { val genCount = new AtomicLong(0L) } class KillSwitchMatStream extends PerpetualStream[(KillSwitch, Future[Long])] { import KillSwitchMatStream._ import org.squbs.unicomplex.Timeouts._ override def stopTimeout = awaitMax def generator = Iterator.iterate(0){ p => if (p == Int.MaxValue) 0 else p + 1 } map { v => genCount.incrementAndGet() v } val source = Source.fromIterator(generator _) val throttle = Flow[Int].throttle(5000, 1 second, 1000, Shaping) val counter = Flow[Int].map { _ => 1L }.reduce { _ + _ }.toMat(Sink.head)(Keep.right) override def streamGraph = RunnableGraph.fromGraph(GraphDSL.create(KillSwitches.single[Int], counter)((_, _)) { implicit builder => (kill, sink) => import GraphDSL.Implicits._ source ~> kill ~> throttle ~> sink ClosedShape }) override def receive = { case NotifyWhenDone => // Send back the future directly here, don't map the future. The map will likely happen after ActorSystem // shutdown so we cannot use context.dispatcher as execution context for the map as it won't be there when // the map is supposed to happen. sender() ! matValue._2 } }
Example 8
Source File: UseCaseSupport.scala From akka-ddd-cqrs-es-example with MIT License | 5 votes |
package com.github.j5ik2o.bank.useCase import akka.{ Done, NotUsed } import akka.stream.{ FlowShape, QueueOfferResult } import akka.stream.scaladsl.{ Flow, GraphDSL, Sink, SourceQueueWithComplete, Unzip, Zip } import scala.concurrent.{ ExecutionContext, Future, Promise } object UseCaseSupport { implicit class FlowOps[A, B](val self: Flow[A, B, NotUsed]) extends AnyVal { def zipPromise: Flow[(A, Promise[B]), (B, Promise[B]), NotUsed] = Flow .fromGraph(GraphDSL.create() { implicit b => import GraphDSL.Implicits._ val unzip = b.add(Unzip[A, Promise[B]]) val zip = b.add(Zip[B, Promise[B]]) unzip.out0 ~> self ~> zip.in0 unzip.out1 ~> zip.in1 FlowShape(unzip.in, zip.out) }) } } trait UseCaseSupport { protected def offerToQueue[A, B]( sourceQueue: SourceQueueWithComplete[(A, Promise[B])] )(request: A, promise: Promise[B])(implicit ec: ExecutionContext): Future[B] = { sourceQueue.offer((request, promise)).flatMap { case QueueOfferResult.Enqueued => promise.future case QueueOfferResult.Failure(t) => Future.failed(new Exception("Failed to offer request", t)) case QueueOfferResult.Dropped => Future.failed( new Exception( s"Failed to enqueue resolve request, the queue buffer was full, please check the bank.interface.buffer-size setting" ) ) case QueueOfferResult.QueueClosed => Future.failed(new Exception("Failed to enqueue request batch write, the queue was closed")) } } protected def completePromiseSink[T]: Sink[(T, Promise[T]), Future[Done]] = Sink.foreach { case (response, promise) => promise.success(response) } }
Example 9
Source File: BufferProblem.scala From fusion-data with Apache License 2.0 | 5 votes |
package example.akkastream.buffer import akka.actor.ActorSystem import akka.stream.scaladsl.{ GraphDSL, RunnableGraph, Sink, Source, ZipWith } import akka.stream.{ ActorMaterializer, Attributes, ClosedShape } import scala.concurrent.duration._ import scala.io.StdIn object BufferProblem extends App { implicit val system = ActorSystem() implicit val mat = ActorMaterializer() case class Tick() val g = RunnableGraph.fromGraph(GraphDSL.create() { implicit b => import akka.stream.scaladsl.GraphDSL.Implicits._ // this is the asynchronous stage in this graph val zipper = b.add(ZipWith[Tick, Int, Int]((tick, count) => count).async.addAttributes(Attributes.inputBuffer(1, 1))) // 用默认缓冲区设置时将只打印 1 // val zipper = b.add(ZipWith[Tick, Int, Int]((tick, count) => count).async) Source.tick(initialDelay = 3.second, interval = 3.second, Tick()) ~> zipper.in0 Source .tick(initialDelay = 1.second, interval = 1.second, "message!") .conflateWithSeed(seed = (_) => 1)((count, _) => count + 1) ~> zipper.in1 zipper.out ~> Sink.foreach(println) ClosedShape }) g.run() StdIn.readLine() system.terminate() }
Example 10
Source File: PartialGraph.scala From fusion-data with Apache License 2.0 | 5 votes |
package example.akkastream.basic import akka.actor.ActorSystem import akka.stream.scaladsl.{ GraphDSL, RunnableGraph, Sink, Source, ZipWith } import akka.stream.{ ActorMaterializer, ClosedShape, UniformFanInShape } import scala.concurrent.Await import scala.concurrent.duration._ import scala.io.StdIn object PartialGraph extends App { implicit val system = ActorSystem() implicit val mat = ActorMaterializer() val pickMaxOfThree = GraphDSL.create() { implicit b => import GraphDSL.Implicits._ // ZipWith 最后一个泛型是输出参数类型。 val zip1 = b.add(ZipWith[Int, Int, Int](math.max)) val zip2 = b.add(ZipWith[Int, Int, Int](math.max)) zip1.out ~> zip2.in0 UniformFanInShape(zip2.out, zip1.in0, zip1.in1, zip2.in1) } val resultSink = Sink.head[Int] val g = RunnableGraph.fromGraph(GraphDSL.create(resultSink) { implicit b => sink => import GraphDSL.Implicits._ val pm3 = b.add(pickMaxOfThree) Source.single(4) ~> pm3.in(0) Source.single(2) ~> pm3.in(1) Source.single(3) ~> pm3.in(2) pm3.out ~> sink.in ClosedShape }) val result = Await.result(g.run, 300.millis) println(s"result: $result") StdIn.readLine() system.terminate() }
Example 11
Source File: BroadcastBufferCommitOrderSpec.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.pattern.stream import akka.actor.ActorSystem import akka.stream.{ClosedShape, ActorMaterializer} import akka.stream.scaladsl.{GraphDSL, RunnableGraph} import com.typesafe.config.ConfigFactory import org.scalatest.concurrent.Eventually import org.scalatest.{BeforeAndAfterAll, Matchers, FlatSpec} import org.squbs.testkit.Timeouts._ import scala.concurrent.Await class BroadcastBufferCommitOrderSpec extends FlatSpec with Matchers with BeforeAndAfterAll with Eventually { implicit val system = ActorSystem("BroadcastBufferCommitOrderSpec", PersistentBufferSpec.testConfig) implicit val mat = ActorMaterializer() implicit val serializer = QueueSerializer[Int]() import StreamSpecUtil._ override def afterAll = { Await.ready(system.terminate(), awaitMax) } it should "fail when an out of order commit is attempted and commit-order-policy = strict" in { val util = new StreamSpecUtil[Int, Event[Int]](2) import util._ val buffer = BroadcastBufferAtLeastOnce[Int](ConfigFactory.parseString("commit-order-policy = strict").withFallback(config)) val streamGraph = RunnableGraph.fromGraph(GraphDSL.create(flowCounter) { implicit builder => sink => import GraphDSL.Implicits._ val commit = buffer.commit[Int] val bcBuffer = builder.add(buffer.async) val mr = builder.add(merge) in ~> bcBuffer ~> filterARandomElement ~> commit ~> mr ~> sink bcBuffer ~> commit ~> mr ClosedShape }) val sinkF = streamGraph.run() Await.result(sinkF.failed, awaitMax) shouldBe an[CommitOrderException] clean() } it should "not fail when an out of order commit is attempted and commit-order-policy = lenient" in { val util = new StreamSpecUtil[Int, Event[Int]](2) import util._ val buffer = BroadcastBufferAtLeastOnce[Int](ConfigFactory.parseString("commit-order-policy = lenient").withFallback(config)) val streamGraph = RunnableGraph.fromGraph(GraphDSL.create(flowCounter) { implicit builder => sink => import GraphDSL.Implicits._ val commit = buffer.commit[Int] val bcBuffer = builder.add(buffer.async) val mr = builder.add(merge) in ~> bcBuffer ~> filterARandomElement ~> commit ~> mr ~> sink bcBuffer ~> commit ~> mr ClosedShape }) val countFuture = streamGraph.run() val count = Await.result(countFuture, awaitMax) eventually { buffer.queue shouldBe 'closed } count shouldBe (elementCount * outputPorts - 1) println(s"Total records processed $count") clean() } }
Example 12
Source File: PartialGraph2.scala From fusion-data with Apache License 2.0 | 5 votes |
package example.akkastream.basic import akka.NotUsed import akka.actor.ActorSystem import akka.stream.scaladsl.{ Broadcast, Flow, GraphDSL, Sink, Source, Zip } import akka.stream.{ ActorMaterializer, FlowShape, SourceShape } import scala.io.StdIn object PartialGraph2 extends App { implicit val system = ActorSystem() implicit val mat = ActorMaterializer() import system.dispatcher val pairs: Source[(Int, Int), NotUsed] = Source.fromGraph(GraphDSL.create() { implicit b => import GraphDSL.Implicits._ // prepare graph elements val zip = b.add(Zip[Int, Int]()) def ints = Source.fromIterator(() => Iterator.from(1)) // connect the graph ints.filter(_ % 2 != 0) ~> zip.in0 ints.filter(_ % 2 == 0) ~> zip.in1 // expose port SourceShape(zip.out) }) val firstPair = pairs.runWith(Sink.head) firstPair.foreach(println) val pairUpWithToString = Flow.fromGraph(GraphDSL.create() { implicit b => import GraphDSL.Implicits._ val broadcast = b.add(Broadcast[Int](2)) val zip = b.add(Zip[Int, String]()) broadcast.out(0) ~> zip.in0 broadcast.out(1).map(_.toString) ~> zip.in1 FlowShape(broadcast.in, zip.out) }) Source(List(1)).via(pairUpWithToString).runWith(Sink.head).foreach(println) StdIn.readLine() system.terminate() }
Example 13
Source File: GraphComponent.scala From fusion-data with Apache License 2.0 | 5 votes |
package example.akkastream.basic import akka.actor.ActorSystem import akka.stream.FanInShape.{ Init, Name } import akka.stream._ import akka.stream.scaladsl.{ Balance, Flow, GraphDSL, Merge, MergePreferred, RunnableGraph, Sink, Source } import scala.collection.immutable import scala.io.StdIn case class PriorityWorkerPoolShape[In, Out](jobsIn: Inlet[In], priorityJobsIn: Inlet[In], resultsOut: Outlet[Out]) extends Shape { override def inlets: immutable.Seq[Inlet[_]] = jobsIn :: priorityJobsIn :: Nil override def outlets: immutable.Seq[Outlet[_]] = resultsOut :: Nil override def deepCopy(): Shape = PriorityWorkerPoolShape(jobsIn.carbonCopy(), priorityJobsIn.carbonCopy(), resultsOut.carbonCopy()) } case class PriorityWorkerPoolShape2[In, Out](_init: Init[Out] = Name("PriorityWorkerPoolShape2")) extends FanInShape[Out](_init) { override protected def construct(init: Init[Out]): FanInShape[Out] = PriorityWorkerPoolShape2(init) val jobsIn: Inlet[In] = newInlet[In]("jobsIn") val priorityJobsIn: Inlet[In] = newInlet[In]("priorityJobsIn") // Outlet[Out] 使用名字 "out" 将被自动创建 } object PriorityWorkerPool { def apply[In, Out](worker: Flow[In, Out, Any], workerCount: Int) = GraphDSL.create() { implicit b => import GraphDSL.Implicits._ val priorityMerge = b.add(MergePreferred[In](1)) val balance = b.add(Balance[In](workerCount)) val resultsMerge = b.add(Merge[Out](workerCount)) for (i <- 0 until workerCount) balance.out(i) ~> worker ~> resultsMerge.in(i) // 在合并优先和普通作业后发送到平衡器 priorityMerge ~> balance PriorityWorkerPoolShape(priorityMerge.in(0), priorityMerge.preferred, resultsMerge.out) } } object GraphComponent extends App { implicit val system = ActorSystem() implicit val mat = ActorMaterializer() import system.dispatcher val worker1 = Flow[String].map("step 1 " + _) val worker2 = Flow[String].map("step 2 " + _) val g = RunnableGraph.fromGraph(GraphDSL.create() { implicit b => import GraphDSL.Implicits._ val priorityPool1 = b.add(PriorityWorkerPool(worker1, 4)) val priorityPool2 = b.add(PriorityWorkerPool(worker2, 2)) Source(1 to 10).map("job: " + _) ~> priorityPool1.jobsIn Source(1 to 10).map("priority job: " + _) ~> priorityPool1.priorityJobsIn priorityPool1.resultsOut ~> priorityPool2.jobsIn Source(1 to 10).map("one-step, priority " + _) ~> priorityPool2.priorityJobsIn priorityPool2.resultsOut ~> Sink.foreach(println) ClosedShape }) g.run() StdIn.readLine() system.terminate() }
Example 14
Source File: Graph1.scala From fusion-data with Apache License 2.0 | 5 votes |
package example.akkastream.basic import akka.NotUsed import akka.actor.ActorSystem import akka.stream.{ ActorMaterializer, ClosedShape } import akka.stream.scaladsl.{ Broadcast, Flow, GraphDSL, Merge, RunnableGraph, Sink, Source } import scala.collection.immutable import scala.io.StdIn object Graph1 extends App { implicit val system = ActorSystem() implicit val mat = ActorMaterializer() val graph = g(1 to 2) graph.run() StdIn.readLine() system.terminate() def g(data: immutable.Iterable[Int]) = RunnableGraph.fromGraph(GraphDSL.create() { implicit b: GraphDSL.Builder[NotUsed] => import GraphDSL.Implicits._ val in = Source(data) val out = Sink.foreach(println) val bcast = b.add(Broadcast[Int](2)) val merge = b.add(Merge[Int](2)) val f1, f2, f3, f4 = Flow[Int].map(_ + 10) in ~> f1 ~> bcast ~> f2 ~> merge ~> f3 ~> out bcast ~> f4 ~> merge ClosedShape }) }
Example 15
Source File: PartialGraph.scala From fusion-data with Apache License 2.0 | 5 votes |
package example.akkastream.graph import akka.actor.ActorSystem import akka.stream.scaladsl.{ Balance, Broadcast, Flow, GraphDSL, Keep, Merge, RunnableGraph, Sink, Source } import akka.stream.{ ActorMaterializer, FlowShape, SourceShape } import scala.concurrent.Future import scala.io.StdIn object PartialGraph extends App { implicit val system = ActorSystem() implicit val mat = ActorMaterializer() import system.dispatcher def partial = GraphDSL .create() { implicit b => import GraphDSL.Implicits._ val B = b.add(Broadcast[Int](2)) val C = b.add(Merge[Int](2)) val D = Flow[Int].map(_ + 1) val E = b.add(Balance[Int](2)) val F = b.add(Merge[Int](2)) C <~ F B ~> C ~> F B ~> D ~> E ~> F FlowShape(B.in, E.out(1)) } .named("partial") // 转换partial从FlowShape到Flow,可访问流DSL(比如:.filter() 函数) val flow = Flow.fromGraph(partial) val source = Source.fromGraph(GraphDSL.create() { implicit b => import GraphDSL.Implicits._ val merge = b.add(Merge[Int](2)) Source.single(0) ~> merge Source(List(2, 3, 4)) ~> merge SourceShape(merge.out) }) val sink: Sink[Int, Future[Int]] = Flow[Int].map(_ * 2).drop(10).named("nestedFlow").toMat(Sink.head)(Keep.right) val closed: RunnableGraph[Future[Int]] = source.via(flow.filter(_ > 1)).toMat(sink)(Keep.right) closed.run().foreach(println) StdIn.readLine() system.terminate() }
Example 16
Source File: VoiceUDPFlow.scala From AckCord with MIT License | 5 votes |
package ackcord.voice import java.net.InetSocketAddress import java.nio.ByteOrder import scala.concurrent.{Future, Promise} import ackcord.data.{RawSnowflake, UserId} import ackcord.util.UdpConnectedFlow import akka.NotUsed import akka.actor.typed.ActorSystem import akka.stream.scaladsl.{BidiFlow, Concat, Flow, GraphDSL, Keep, Source} import akka.stream.{BidiShape, OverflowStrategy} import akka.util.ByteString object VoiceUDPFlow { val silence = ByteString(0xF8, 0xFF, 0xFE) val SampleRate = 48000 val FrameSize = 960 val FrameTime = 20 def flow[Mat]( remoteAddress: InetSocketAddress, ssrc: Int, serverId: RawSnowflake, userId: UserId, secretKeys: Source[Option[ByteString], Mat] )(implicit system: ActorSystem[Nothing]): Flow[ByteString, AudioAPIMessage.ReceivedData, (Mat, Future[FoundIP])] = NaclBidiFlow .bidiFlow(ssrc, serverId, userId, secretKeys) .atopMat(voiceBidi(ssrc).reversed)(Keep.both) .async .join(Flow[ByteString].buffer(32, OverflowStrategy.backpressure).via(UdpConnectedFlow.flow(remoteAddress))) def voiceBidi(ssrc: Int): BidiFlow[ByteString, ByteString, ByteString, ByteString, Future[FoundIP]] = { implicit val byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN val ipDiscoveryPacket = { val byteBuilder = ByteString.createBuilder byteBuilder.sizeHint(74) byteBuilder.putShort(0x1).putShort(70).putInt(ssrc) byteBuilder.putBytes(new Array[Byte](66)) byteBuilder.result() } val valvePromise = Promise[Unit] val valve = Source.future(valvePromise.future).drop(1).asInstanceOf[Source[ByteString, NotUsed]] val ipDiscoveryFlow = Flow[ByteString] .viaMat(new IPDiscoveryFlow(() => valvePromise.success(())))(Keep.right) BidiFlow .fromGraph(GraphDSL.create(ipDiscoveryFlow) { implicit b => ipDiscovery => import GraphDSL.Implicits._ val voiceIn = b.add(Flow[ByteString]) val ipDiscoverySource = b.add(Source.single(ipDiscoveryPacket) ++ valve) val ipDiscoveryAndThenVoiceData = b.add(Concat[ByteString]()) ipDiscoverySource ~> ipDiscoveryAndThenVoiceData voiceIn ~> ipDiscoveryAndThenVoiceData BidiShape( ipDiscovery.in, ipDiscovery.out, voiceIn.in, ipDiscoveryAndThenVoiceData.out ) }) } case class FoundIP(address: String, port: Int) }
Example 17
Source File: CmdHelper.scala From AckCord with MIT License | 5 votes |
package ackcord.oldcommands import ackcord.CacheSnapshot import ackcord.data.raw.RawMessage import ackcord.data.{Message, User} import ackcord.requests.{CreateMessage, Request, Requests} import ackcord.syntax._ import akka.NotUsed import akka.stream.FlowShape import akka.stream.scaladsl.{Broadcast, Flow, GraphDSL} object CmdHelper { def isValidCommand[F[_]](needMention: Boolean, msg: Message)( implicit c: CacheSnapshot ): Option[List[String]] = { if (needMention) { val botUser = c.botUser //We do a quick check first before parsing the message val quickCheck = if (msg.mentions.contains(botUser.id)) Some(msg.content.split(" ").toList) else None quickCheck.flatMap { args => MessageParser .parseEither(args, MessageParser[User]) .toOption .flatMap { case (remaining, user) if user.id == botUser.id => Some(remaining) case (_, _) => None } } } else Some(msg.content.split(" ").toList) } }
Example 18
Source File: LogProgress.scala From apache-spark-test with Apache License 2.0 | 5 votes |
package com.github.dnvriend import akka.NotUsed import akka.event.LoggingAdapter import akka.stream.FlowShape import akka.stream.scaladsl.{ Broadcast, Flow, GraphDSL, Sink } import scala.compat.Platform import scala.collection.immutable._ object LogProgress { def flow[A](each: Long = 1000)(implicit log: LoggingAdapter = null): Flow[A, A, NotUsed] = Flow.fromGraph[A, A, NotUsed](GraphDSL.create() { implicit b => import GraphDSL.Implicits._ val logFlow = Flow[A].statefulMapConcat { () => var last = Platform.currentTime var num = 0L (x: A) => num += 1 if (num % each == 0) { val duration = Platform.currentTime - last val logOpt = Option(log) Option(log).foreach(_.info("[{} ms / {}]: {}", duration, each, num)) if (logOpt.isEmpty) println(s"[$duration ms / $each]: $num") last = Platform.currentTime } Iterable(x) } val bcast = b.add(Broadcast[A](2, eagerCancel = false)) bcast ~> logFlow ~> Sink.ignore FlowShape.of(bcast.in, bcast.out(1)) }) }
Example 19
Source File: SearchController.scala From izanami with Apache License 2.0 | 5 votes |
package controllers import akka.actor.ActorSystem import akka.stream.scaladsl.{GraphDSL, Interleave, Sink, Source} import akka.stream.{ActorMaterializer, SourceShape} import controllers.actions.SecuredAuthContext import domains.abtesting.ExperimentService import domains.config.ConfigService import domains.feature.FeatureService import domains.script.GlobalScriptService import domains.configuration.GlobalContext import play.api.libs.json.{JsArray, JsValue, Json} import play.api.mvc.{AbstractController, ActionBuilder, AnyContent, ControllerComponents} import store.Query import zio.{Runtime, ZIO} import libs.http.HttpContext class SearchController(AuthAction: ActionBuilder[SecuredAuthContext, AnyContent], cc: ControllerComponents)( implicit system: ActorSystem, R: HttpContext[GlobalContext] ) extends AbstractController(cc) { import libs.http._ def search(pattern: String, features: Boolean, configs: Boolean, experiments: Boolean, scripts: Boolean) = AuthAction.asyncTask[GlobalContext] { ctx => val query: Query = Query.oneOf(ctx.authorizedPatterns).and(pattern.split(",").toList) for { featuresRes <- if (features) FeatureService .findByQuery(query, 1, 10) .map(_.results.map(value => Json.obj("type" -> "features", "id" -> Json.toJson(value.id)))) .map(value => Source(value.toList)) else ZIO.succeed(Source.empty[JsValue]) configsRes <- if (configs) ConfigService .findByQuery(query, 1, 10) .map( _.results.map(value => Json.obj("type" -> "configurations", "id" -> Json.toJson(value.id))) ) .map(value => Source(value.toList)) else ZIO.succeed(Source.empty[JsValue]) experimentsRes <- if (experiments) ExperimentService .findByQuery(query, 1, 10) .map( _.results.map(value => Json.obj("type" -> "experiments", "id" -> Json.toJson(value.id))) ) .map(value => Source(value.toList)) else ZIO.succeed(Source.empty[JsValue]) scriptsRes <- if (scripts) GlobalScriptService .findByQuery(query, 1, 10) .map(_.results.map(value => Json.obj("type" -> "scripts", "id" -> Json.toJson(value.id)))) .map(value => Source(value.toList)) else ZIO.succeed(Source.empty[JsValue]) res <- ZIO.fromFuture { implicit ec => val all = Source.fromGraph(GraphDSL.create() { implicit builder => import GraphDSL.Implicits._ val interleave = builder.add(Interleave[JsValue](4, 1)) featuresRes ~> interleave.in(0) configsRes ~> interleave.in(1) experimentsRes ~> interleave.in(2) scriptsRes ~> interleave.in(3) SourceShape(interleave.out) }) all.take(10).runWith(Sink.seq) map { jsons => Ok(JsArray(jsons)) } } } yield res } }
Example 20
Source File: ProcessingKafkaApplication.scala From Akka-Cookbook with MIT License | 5 votes |
package com.packt.chapter8 import akka.actor.ActorSystem import akka.kafka.scaladsl.{Consumer, Producer} import akka.kafka.{ConsumerSettings, ProducerSettings, Subscriptions} import akka.stream.{ActorMaterializer, ClosedShape} import akka.stream.scaladsl.{Flow, GraphDSL, RunnableGraph, Sink, Source} import org.apache.kafka.clients.consumer.{ConsumerConfig, ConsumerRecord} import org.apache.kafka.clients.producer.ProducerRecord import org.apache.kafka.common.TopicPartition import org.apache.kafka.common.serialization.{ByteArrayDeserializer, ByteArraySerializer, StringDeserializer, StringSerializer} import scala.concurrent.duration._ object ProcessingKafkaApplication extends App { implicit val actorSystem = ActorSystem("SimpleStream") implicit val actorMaterializer = ActorMaterializer() val bootstrapServers = "localhost:9092" val kafkaTopic = "akka_streams_topic" val partition = 0 val subscription = Subscriptions.assignment(new TopicPartition(kafkaTopic, partition)) val consumerSettings = ConsumerSettings(actorSystem, new ByteArrayDeserializer, new StringDeserializer) .withBootstrapServers(bootstrapServers) .withGroupId("akka_streams_group") .withProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest") val producerSettings = ProducerSettings(actorSystem, new ByteArraySerializer, new StringSerializer) .withBootstrapServers(bootstrapServers) val runnableGraph = RunnableGraph.fromGraph(GraphDSL.create() { implicit builder => import GraphDSL.Implicits._ val tickSource = Source.tick(0 seconds, 5 seconds, "Hello from Akka Streams using Kafka!") val kafkaSource = Consumer.plainSource(consumerSettings, subscription) val kafkaSink = Producer.plainSink(producerSettings) val printlnSink = Sink.foreach(println) val mapToProducerRecord = Flow[String].map(elem => new ProducerRecord[Array[Byte], String](kafkaTopic, elem)) val mapFromConsumerRecord = Flow[ConsumerRecord[Array[Byte], String]].map(record => record.value()) tickSource ~> mapToProducerRecord ~> kafkaSink kafkaSource ~> mapFromConsumerRecord ~> printlnSink ClosedShape }) runnableGraph.run() }
Example 21
Source File: IndefiniteStreamParquetSink.scala From parquet4s with MIT License | 5 votes |
package com.github.mjakubowski84.parquet4s import akka.stream.FlowShape import akka.stream.scaladsl.{Broadcast, Flow, GraphDSL, Keep, Sink, ZipWith} import com.github.mjakubowski84.parquet4s.ParquetWriter.ParquetWriterFactory import org.apache.hadoop.fs.Path import org.slf4j.{Logger, LoggerFactory} import scala.concurrent.duration.FiniteDuration private[parquet4s] object IndefiniteStreamParquetSink extends IOOps { protected val logger: Logger = LoggerFactory.getLogger(this.getClass) def apply[In, ToWrite: ParquetWriterFactory, Mat](path: Path, maxChunkSize: Int, chunkWriteTimeWindow: FiniteDuration, buildChunkPath: ChunkPathBuilder[In] = ChunkPathBuilder.default, preWriteTransformation: In => ToWrite = identity[In] _, postWriteSink: Sink[Seq[In], Mat] = Sink.ignore, options: ParquetWriter.Options = ParquetWriter.Options() ): Sink[In, Mat] = { validateWritePath(path, options) val internalFlow = Flow.fromGraph(GraphDSL.create() { implicit b => import GraphDSL.Implicits._ val inChunkFlow = b.add(Flow[In].groupedWithin(maxChunkSize, chunkWriteTimeWindow)) val broadcastChunks = b.add(Broadcast[Seq[In]](outputPorts = 2)) val writeFlow = Flow[Seq[In]].map { chunk => val toWrite = chunk.map(preWriteTransformation) val chunkPath = buildChunkPath(path, chunk) if (logger.isDebugEnabled()) logger.debug(s"Writing ${toWrite.size} records to $chunkPath") ParquetWriter.writeAndClose(chunkPath.toString, toWrite, options) } val zip = b.add(ZipWith[Seq[In], Unit, Seq[In]]((chunk, _) => chunk)) inChunkFlow ~> broadcastChunks ~> writeFlow ~> zip.in1 broadcastChunks ~> zip.in0 FlowShape(inChunkFlow.in, zip.out) }) internalFlow.toMat(postWriteSink)(Keep.right) } }
Example 22
Source File: ParametrizedFlow.scala From akka_streams_tutorial with MIT License | 5 votes |
package sample.stream_shared_state import akka.Done import akka.actor.{ActorSystem, Cancellable} import akka.stream.scaladsl.{Flow, GraphDSL, Keep, Sink, Source, SourceQueueWithComplete, Zip} import akka.stream.{FlowShape, OverflowStrategy} import scala.collection.immutable import scala.concurrent.Future import scala.concurrent.duration._ import scala.util.{Failure, Success} object ParametrizedFlow extends App { val service = ParameterizedFlowService Thread.sleep(5000) service.update(1.0) Thread.sleep(2000) service.update(1.5) Thread.sleep(2000) service.cancel() Thread.sleep(2000) println(service.result()) } object ParameterizedFlowService { implicit val system = ActorSystem("ParameterizedFlowService") implicit val executionContext = system.dispatcher def update(element: Double): Unit = flow._1._2.offer(element) def cancel(): Boolean = flow._1._1.cancel() def result(): Future[Seq[Double]] = flow._2 val fun = (flowValue: Int, paramValue: Double) => flowValue * paramValue val flow: ((Cancellable, SourceQueueWithComplete[Double]), Future[immutable.Seq[Double]]) = Source.tick(0.seconds, 500.millis, 10) .viaMat(createParamFlow(1, OverflowStrategy.dropBuffer, 0.5)(fun))(Keep.both) .wireTap(x => println(x)) .toMat(Sink.seq)(Keep.both) .run() val done: Future[Done] = flow._1._2.watchCompletion() terminateWhen(done) private def createParamFlow[A, P, O](bufferSize: Int, overflowStrategy: OverflowStrategy, initialParam: P)(fun: (A, P) => O) = Flow.fromGraph(GraphDSL.create(Source.queue[P](bufferSize, overflowStrategy)) { implicit builder => queue => import GraphDSL.Implicits._ val zip = builder.add(Zip[A, P]()) //Interesting use of the extrapolate operator //based on https://doc.akka.io/docs/akka/current/stream/stream-rate.html#understanding-extrapolate-and-expand val extra = builder.add(Flow[P].extrapolate(Iterator.continually(_), Some(initialParam))) val map = builder.add(Flow[(A, P)].map(r => fun(r._1, r._2))) queue ~> extra ~> zip.in1 zip.out ~> map FlowShape(zip.in0, map.out) }) private def terminateWhen(done: Future[_]) = { done.onComplete { case Success(_) => println("Flow Success. About to terminate...") system.terminate() case Failure(e) => println(s"Flow Failure: $e. About to terminate...") system.terminate() } } }
Example 23
Source File: WebSocketClient.scala From akka_streams_tutorial with MIT License | 5 votes |
package sample.stream_actor import akka.actor.{ActorRef, ActorSystem} import akka.http.scaladsl.Http import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.model.ws._ import akka.stream.scaladsl.{Flow, GraphDSL, Keep, Sink, Source} import akka.stream.{FlowShape, SourceShape} import sample.stream_actor.WindTurbineSimulator._ import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future} import scala.util.{Failure, Success} object WebSocketClient { def apply(id: String, endpoint: String, windTurbineSimulator: ActorRef) (implicit system: ActorSystem, executionContext: ExecutionContext) = { new WebSocketClient(id, endpoint, windTurbineSimulator)(system, executionContext) } } class WebSocketClient(id: String, endpoint: String, windTurbineSimulator: ActorRef) (implicit system: ActorSystem, executionContext: ExecutionContext) { val webSocketFlow: Flow[Message, Message, Future[WebSocketUpgradeResponse]] = { val websocketUri = s"$endpoint/measurements/$id" Http().webSocketClientFlow(WebSocketRequest(websocketUri)) } val outgoing = GraphDSL.create() { implicit builder => val data = WindTurbineData(id) val flow = builder.add { Source.tick(1.second, 100.millis,()) //valve for the WindTurbineData frequency .map(_ => TextMessage(data.getNext)) } SourceShape(flow.out) } val incoming = GraphDSL.create() { implicit builder => val flow = builder.add { Flow[Message] .collect { case TextMessage.Strict(text) => Future.successful(text) case TextMessage.Streamed(textStream) => textStream.runFold("")(_ + _) .flatMap(Future.successful) } .mapAsync(1)(identity) .map(each => println(s"Client received msg: $each")) } FlowShape(flow.in, flow.out) } val (upgradeResponse, closed) = Source.fromGraph(outgoing) .viaMat(webSocketFlow)(Keep.right) // keep the materialized Future[WebSocketUpgradeResponse] .via(incoming) .toMat(Sink.ignore)(Keep.both) // also keep the Future[Done] .run() val connected = upgradeResponse.map { upgrade => upgrade.response.status match { case StatusCodes.SwitchingProtocols => windTurbineSimulator ! Upgraded case statusCode => windTurbineSimulator ! FailedUpgrade(statusCode) } } connected.onComplete { case Success(_) => windTurbineSimulator ! Connected case Failure(ex) => windTurbineSimulator ! ConnectionFailure(ex) } closed.map { _ => windTurbineSimulator ! Terminated } closed.onComplete { case Success(_) => windTurbineSimulator ! Connected case Failure(ex) => windTurbineSimulator ! ConnectionFailure(ex) } }
Example 24
Source File: AckBidiFlow.scala From reactive-activemq with Apache License 2.0 | 5 votes |
package akka.stream.integration package activemq import akka.NotUsed import akka.stream._ import akka.stream.scaladsl.{ BidiFlow, Flow, GraphDSL, Unzip, Zip } object AckBidiFlow { def apply[R, S, T]( bufferSize: Int = 10, overflowStrategy: OverflowStrategy = OverflowStrategy.backpressure ): BidiFlow[(R, S), S, T, (R, T), NotUsed] = { BidiFlow.fromGraph(GraphDSL.create() { implicit b => import GraphDSL.Implicits._ val unzip = b.add(Unzip[R, S]) val zip = b.add(Zip[R, T]) if (bufferSize > 0) { val buffer = b.add(Flow[R].buffer(bufferSize, overflowStrategy)) unzip.out0 ~> buffer ~> zip.in0 } else unzip.out0 ~> zip.in0 BidiShape(unzip.in, unzip.out1, zip.in1, zip.out) }) } }
Example 25
Source File: Flows.scala From BusFloatingData with Apache License 2.0 | 5 votes |
package de.nierbeck.floating.data.server import akka.actor.{ActorRef, Props} import akka.http.scaladsl.model.ws.{Message, TextMessage} import akka.stream.FlowShape import akka.stream.scaladsl.{Flow, GraphDSL, Merge, Source} import de.nierbeck.floating.data.domain.Vehicle import GraphDSL.Implicits._ import de.nierbeck.floating.data.server._ import de.nierbeck.floating.data.server.actors.websocket._ object Flows { def graphFlowWithStats(router: ActorRef): Flow[Message, Message, _] = { Flow.fromGraph(GraphDSL.create() { implicit builder => // create an actor source val source = Source.actorPublisher[String](VehiclePublisher.props(router)) // Graph elements we'll use val merge = builder.add(Merge[String](2)) val filter = builder.add(Flow[String].filter(_ => false)) // get BBox from request and send it to route, return nothing ... val mapMsgToString = builder.add(Flow[Message].map[String] { case TextMessage.Strict(msg) => { println(s"received message: $msg") if (msg.contains("close")) { router ! msg } else if (msg.contains("spark")) { router ! SPARK } else if (msg.contains("flink")) { router ! FLINK } else { val bbox = toBoundingBox(msg) println(s"transformedt to bbox: $bbox") router ! bbox } "" } }) //outgoing message ... val mapStringToMsg = builder.add(Flow[String].map[Message](x => TextMessage.Strict(x))) //add source to flow val vehiclesSource = builder.add(source) // connect the graph mapMsgToString ~> filter ~> merge // this part of the merge will never provide msgs vehiclesSource ~> merge ~> mapStringToMsg // expose ports FlowShape(mapMsgToString.in, mapStringToMsg.out) }) } }
Example 26
Source File: Balancer.scala From Learn-Scala-Programming with MIT License | 5 votes |
package ch13 import akka.NotUsed import akka.stream.FlowShape import akka.stream.scaladsl.{Balance, Flow, GraphDSL, Merge} object Balancer { def apply[In, Out](subFlow: Flow[In, Out, Any], count: Int): Flow[In, Out, NotUsed] = { Flow.fromGraph(createGraph(subFlow, count)) } import akka.stream.scaladsl.GraphDSL import GraphDSL.Implicits._ def createGraph[Out, In](subFlow: Flow[In, Out, Any], count: Int) = { val balanceBlock = Balance[In](count, waitForAllDownstreams = false) val mergeBlock = Merge[Out](count, eagerComplete = false) GraphDSL.create() { implicit builder ⇒ val balancer = builder.add(balanceBlock) val merge = builder.add(mergeBlock) for (_ ← 1 to count) balancer ~> subFlow ~> merge FlowShape(balancer.in, merge.out) } } }
Example 27
Source File: Test15.scala From incubator-retired-gearpump with Apache License 2.0 | 5 votes |
package org.apache.gearpump.akkastream.example import akka.actor.ActorSystem import akka.stream._ import akka.stream.scaladsl.{Balance, Broadcast, Flow, GraphDSL, Merge, RunnableGraph, Sink, Source} import org.apache.gearpump.akkastream.GearpumpMaterializer import org.apache.gearpump.cluster.main.{ArgumentsParser, CLIOption} import org.apache.gearpump.util.AkkaApp import scala.concurrent.Await import scala.concurrent.duration._ object Test15 extends AkkaApp with ArgumentsParser { // scalastyle:off println override val options: Array[(String, CLIOption[Any])] = Array( "gearpump" -> CLIOption[Boolean]("<boolean>", required = false, defaultValue = Some(false)) ) override def main(akkaConf: Config, args: Array[String]): Unit = { val config = parse(args) implicit val system = ActorSystem("Test15", akkaConf) implicit val materializer: ActorMaterializer = config.getBoolean("gearpump") match { case true => GearpumpMaterializer() case false => ActorMaterializer( ActorMaterializerSettings(system).withAutoFusing(false) ) } import akka.stream.scaladsl.GraphDSL.Implicits._ RunnableGraph.fromGraph(GraphDSL.create() { implicit builder => val A = builder.add(Source.single(0)).out val B = builder.add(Broadcast[Int](2)) val C = builder.add(Merge[Int](2).named("C")) val D = builder.add(Flow[Int].map(_ + 1).named("D")) val E = builder.add(Balance[Int](2).named("E")) val F = builder.add(Merge[Int](2).named("F")) val G = builder.add(Sink.foreach(println).named("G")).in C <~ F A ~> B ~> C ~> F B ~> D ~> E ~> F E ~> G ClosedShape }).run() Await.result(system.whenTerminated, 60.minutes) } // scalastyle:on println }
Example 28
Source File: WorkingWithGraphsApplication.scala From Akka-Cookbook with MIT License | 5 votes |
package com.packt.chapter8 import akka.actor.ActorSystem import akka.stream._ import akka.stream.scaladsl.{Balance, Broadcast, Flow, GraphDSL, Merge, RunnableGraph, Sink, Source} import scala.concurrent.duration._ import scala.util.Random object WorkingWithGraphsApplication extends App { implicit val actorSystem = ActorSystem("WorkingWithGraphs") implicit val actorMaterializer = ActorMaterializer() trait MobileMsg { def id = Random.nextInt(1000) def toGenMsg(origin: String) = GenericMsg(id, origin) } class AndroidMsg extends MobileMsg class IosMsg extends MobileMsg case class GenericMsg(id: Int, origin: String) val graph = RunnableGraph.fromGraph(GraphDSL.create() { implicit builder => import GraphDSL.Implicits._ //Sources val androidNotification = Source.tick(2 seconds, 500 millis, new AndroidMsg) val iOSNotification = Source.tick(700 millis, 600 millis, new IosMsg) //Flow val groupAndroid = Flow[AndroidMsg].map(_.toGenMsg("ANDROID")).groupedWithin(5, 5 seconds).async val groupIos = Flow[IosMsg].map(_.toGenMsg("IOS")).groupedWithin(5, 5 seconds).async def counter = Flow[Seq[GenericMsg]].via(new StatefulCounterFlow()) def mapper = Flow[Seq[GenericMsg]].mapConcat(_.toList) //Junctions val aBroadcast = builder.add(Broadcast[Seq[GenericMsg]](2)) val iBroadcast = builder.add(Broadcast[Seq[GenericMsg]](2)) val balancer = builder.add(Balance[Seq[GenericMsg]](2)) val notitificationMerge = builder.add(Merge[Seq[GenericMsg]](2)) val genericNotitificationMerge = builder.add(Merge[GenericMsg](2)) def counterSink(s: String) = Sink.foreach[Int](x => println(s"$s: [$x]")) //Graph androidNotification ~> groupAndroid ~> aBroadcast ~> counter ~> counterSink("Android") aBroadcast ~> notitificationMerge iBroadcast ~> notitificationMerge iOSNotification ~> groupIos ~> iBroadcast ~> counter ~> counterSink("Ios") notitificationMerge ~> balancer ~> mapper.async ~> genericNotitificationMerge balancer ~> mapper.async ~> genericNotitificationMerge genericNotitificationMerge ~> Sink.foreach(println) ClosedShape }) graph.run() }
Example 29
Source File: StaticTime.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.ledger.client.services.testing.time import java.time.Instant import java.util.concurrent.atomic.AtomicReference import akka.stream.scaladsl.{Broadcast, Flow, GraphDSL, RunnableGraph, Sink} import akka.stream.{ClosedShape, KillSwitches, Materializer, UniqueKillSwitch} import com.daml.api.util.{TimeProvider, TimestampConversion} import com.daml.api.util.TimestampConversion._ import com.daml.grpc.adapter.ExecutionSequencerFactory import com.daml.grpc.adapter.client.akka.ClientAdapter import com.daml.dec.DirectExecutionContext import com.daml.ledger.api.v1.testing.time_service.{GetTimeRequest, SetTimeRequest} import com.daml.ledger.api.v1.testing.time_service.TimeServiceGrpc.{TimeService, TimeServiceStub} import com.daml.ledger.client.LedgerClient import scala.concurrent.{ExecutionContext, Future} class StaticTime( timeService: TimeService, clock: AtomicReference[Instant], killSwitch: UniqueKillSwitch, ledgerId: String) extends TimeProvider with AutoCloseable { def getCurrentTime: Instant = clock.get def timeRequest(instant: Instant) = SetTimeRequest( ledgerId, Some(TimestampConversion.fromInstant(getCurrentTime)), Some(TimestampConversion.fromInstant(instant))) def setTime(instant: Instant)(implicit ec: ExecutionContext): Future[Unit] = { timeService.setTime(timeRequest(instant)).map { _ => val _ = StaticTime.advanceClock(clock, instant) } } override def close(): Unit = killSwitch.shutdown() } object StaticTime { def advanceClock(clock: AtomicReference[Instant], instant: Instant): Instant = { clock.updateAndGet { case current if instant isAfter current => instant case current => current } } def updatedVia(timeService: TimeServiceStub, ledgerId: String, token: Option[String] = None)( implicit m: Materializer, esf: ExecutionSequencerFactory): Future[StaticTime] = { val clockRef = new AtomicReference[Instant](Instant.EPOCH) val killSwitchExternal = KillSwitches.single[Instant] val sinkExternal = Sink.head[Instant] RunnableGraph .fromGraph { GraphDSL.create(killSwitchExternal, sinkExternal) { case (killSwitch, futureOfFirstElem) => // We serve this in a future which completes when the first element has passed through. // Thus we make sure that the object we serve already received time data from the ledger. futureOfFirstElem.map(_ => new StaticTime(timeService, clockRef, killSwitch, ledgerId))( DirectExecutionContext) } { implicit b => (killSwitch, sinkHead) => import GraphDSL.Implicits._ val instantSource = b.add( ClientAdapter .serverStreaming( GetTimeRequest(ledgerId), LedgerClient.stub(timeService, token).getTime) .map(r => toInstant(r.getCurrentTime))) val updateClock = b.add(Flow[Instant].map { i => advanceClock(clockRef, i) i }) val broadcastTimes = b.add(Broadcast[Instant](2)) val ignore = b.add(Sink.ignore) // format: OFF instantSource ~> killSwitch ~> updateClock ~> broadcastTimes.in broadcastTimes.out(0) ~> sinkHead broadcastTimes.out(1) ~> ignore // format: ON ClosedShape } } .run() } }
Example 30
Source File: PipeliningParallelizing.scala From Akka-Cookbook with MIT License | 5 votes |
package com.packt.chapter8 import akka.NotUsed import akka.actor.ActorSystem import akka.stream.{ActorMaterializer, FlowShape} import akka.stream.scaladsl.{Balance, Flow, GraphDSL, Merge, Sink, Source} import scala.util.Random trait PipeliningParallelizing extends App { implicit val actorSystem = ActorSystem("PipeliningParallelizing") implicit val actorMaterializer = ActorMaterializer() case class Wash(id: Int) case class Dry(id: Int) case class Done(id: Int) val tasks = (1 to 5).map(Wash) def washStage = Flow[Wash].map(wash => { val sleepTime = Random.nextInt(3) * 1000 println(s"Washing ${wash.id}. It will take $sleepTime milliseconds.") Thread.sleep(sleepTime) Dry(wash.id) }) def dryStage = Flow[Dry].map(dry => { val sleepTime = Random.nextInt(3) * 1000 println(s"Drying ${dry.id}. It will take $sleepTime milliseconds.") Thread.sleep(sleepTime) Done(dry.id) }) val parallelStage = Flow.fromGraph(GraphDSL.create() { implicit builder => import GraphDSL.Implicits._ val dispatchLaundry = builder.add(Balance[Wash](3)) val mergeLaundry = builder.add(Merge[Done](3)) dispatchLaundry.out(0) ~> washStage.async ~> dryStage.async ~> mergeLaundry.in(0) dispatchLaundry.out(1) ~> washStage.async ~> dryStage.async ~> mergeLaundry.in(1) dispatchLaundry.out(2) ~> washStage.async ~> dryStage.async ~> mergeLaundry.in(2) FlowShape(dispatchLaundry.in, mergeLaundry.out) }) def runGraph(testingFlow: Flow[Wash, Done, NotUsed]) = Source(tasks).via(testingFlow).to(Sink.foreach(println)).run() }
Example 31
Source File: AnotherServiceImpl.scala From lagom with Apache License 2.0 | 5 votes |
package docs.scaladsl.mb import akka.Done import akka.NotUsed import akka.stream.FlowShape import akka.stream.scaladsl.Flow import akka.stream.scaladsl.GraphDSL import akka.stream.scaladsl.GraphDSL.Implicits._ import akka.stream.scaladsl.Merge import akka.stream.scaladsl.Partition import com.lightbend.lagom.scaladsl.api.ServiceCall import com.lightbend.lagom.scaladsl.api.broker.Message //#inject-service class AnotherServiceImpl(helloService: HelloService) extends AnotherService { //#inject-service //#subscribe-to-topic helloService .greetingsTopic() .subscribe // <-- you get back a Subscriber instance .atLeastOnce( Flow.fromFunction(doSomethingWithTheMessage) ) //#subscribe-to-topic var lastObservedMessage: String = _ private def doSomethingWithTheMessage(greetingMessage: GreetingMessage): Done = { lastObservedMessage = greetingMessage.message Done } import scala.concurrent.ExecutionContext.Implicits.global override def foo: ServiceCall[NotUsed, String] = ServiceCall { req => scala.concurrent.Future.successful(lastObservedMessage) } def subscribeWithMetadata = { //#subscribe-to-topic-with-metadata import com.lightbend.lagom.scaladsl.api.broker.Message import com.lightbend.lagom.scaladsl.broker.kafka.KafkaMetadataKeys helloService .greetingsTopic() .subscribe .withMetadata .atLeastOnce( Flow[Message[GreetingMessage]].map { msg => val greetingMessage = msg.payload val messageKey = msg.messageKeyAsString val kafkaHeaders = msg.get(KafkaMetadataKeys.Headers) println(s"Message: $greetingMessage Key: $messageKey Headers: $kafkaHeaders") Done } ) //#subscribe-to-topic-with-metadata } def skipMessages = { //#subscribe-to-topic-skip-messages helloService .greetingsTopic() .subscribe .atLeastOnce( Flow[GreetingMessage].map { case msg @ GreetingMessage("Kia ora") => doSomethingWithTheMessage(msg) case _ => Done // Skip all messages where the message is not "Kia ora". } ) //#subscribe-to-topic-skip-messages } }
Example 32
Source File: SourceAugmenter.scala From HAT2.0 with GNU Affero General Public License v3.0 | 5 votes |
package org.hatdex.hat.utils import akka.NotUsed import akka.stream.scaladsl.{ GraphDSL, Source } import akka.stream.stage.GraphStage import akka.stream.{ FanInShape2, SourceShape } class SourceAugmenter { def augment[T, U](source: Source[T, NotUsed], extrasSource: Source[U, NotUsed], augmentFunction: (T, U) ⇒ Either[T, U]): Source[T, NotUsed] = { augmentSource(new AugmentWith(augmentFunction), source, extrasSource) { (_, _) => NotUsed } } private def augmentSource[T, U, MatIn0, MatIn1, Mat]( combinator: GraphStage[FanInShape2[T, U, T]], s0: Source[T, MatIn0], s1: Source[U, MatIn1])(combineMat: (MatIn0, MatIn1) => Mat): Source[T, Mat] = Source.fromGraph(GraphDSL.create(s0, s1)(combineMat) { implicit builder => (s0, s1) => import GraphDSL.Implicits._ val merge = builder.add(combinator) s0 ~> merge.in0 s1 ~> merge.in1 SourceShape(merge.out) }) }
Example 33
Source File: SourceMergeSorter.scala From HAT2.0 with GNU Affero General Public License v3.0 | 5 votes |
package org.hatdex.hat.utils import akka.NotUsed import akka.stream.scaladsl.{ GraphDSL, MergeSorted, Source } import akka.stream.stage.GraphStage import akka.stream.{ FanInShape2, SourceShape } import scala.annotation.tailrec class SourceMergeSorter { def mergeWithSorter[A](originSources: Seq[Source[A, NotUsed]])(implicit ordering: Ordering[A]): Source[A, NotUsed] = merge(originSources, sorter[A]) private def merge[A](originSources: Seq[Source[A, NotUsed]], f: (Source[A, NotUsed], Source[A, NotUsed]) => Source[A, NotUsed]): Source[A, NotUsed] = originSources match { case Nil => Source.empty[A] case sources => @tailrec def reducePairs(sources: Seq[Source[A, NotUsed]]): Source[A, NotUsed] = sources match { case Seq(s) => s case _ => reducePairs(sources.grouped(2).map { case Seq(a) => a case Seq(a, b) => f(a, b) }.toSeq) } reducePairs(sources) } private def sorter[A](s1: Source[A, NotUsed], s2: Source[A, NotUsed])(implicit ord: Ordering[A]): Source[A, NotUsed] = combineSources(new MergeSorted[A], s1, s2) { (_, _) => NotUsed } private def combineSources[A, MatIn0, MatIn1, Mat]( combinator: GraphStage[FanInShape2[A, A, A]], s0: Source[A, MatIn0], s1: Source[A, MatIn1])(combineMat: (MatIn0, MatIn1) => Mat): Source[A, Mat] = Source.fromGraph(GraphDSL.create(s0, s1)(combineMat) { implicit builder => (s0, s1) => import GraphDSL.Implicits._ val merge = builder.add(combinator) s0 ~> merge.in0 s1 ~> merge.in1 SourceShape(merge.out) }) }
Example 34
Source File: MapInitAndLastTests.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.util.streams.test import akka.stream._ import akka.stream.scaladsl.{GraphDSL, RunnableGraph, Source} import akka.stream.testkit.scaladsl.{TestSink, TestSource} import akka.stream.testkit.TestPublisher.{Probe => SrcProbe} import akka.stream.testkit.TestSubscriber.{Probe => SnkProbe} import cmwell.util.stream.MapInitAndLast import scala.concurrent.duration.DurationInt class MapInitAndLastTests extends StreamSpec { def generateGraph[In](): (SrcProbe[In],SnkProbe[(In,Boolean)]) = { val src = TestSource.probe[In] val snk = TestSink.probe[(In,Boolean)] RunnableGraph.fromGraph(GraphDSL.create(src, snk)((a, b) => (a, b)) { implicit b => { (s1, s2) => { import GraphDSL.Implicits._ val mial = b.add(new MapInitAndLast[In, (In,Boolean)](_ -> false, _ -> true)) s1 ~> mial ~> s2 ClosedShape } } }).run() } describe("MapInitAndLast Stage"){ it("should buffer a single element"){ val (src,snk) = generateGraph[Int]() snk.request(99) src.sendNext(1) snk.expectNoMessage(300.millis) src.sendComplete() snk.expectNext((1,true)) snk.expectComplete() } it("should treat last element differently") { val (src,snk) = generateGraph[Int]() snk.request(99) src.sendNext(1) snk.expectNoMessage(300.millis) src.sendNext(2) snk.expectNext((1,false)) src.sendNext(3) snk.expectNext((2,false)) src.sendComplete() snk.expectNext((3,true)) snk.expectComplete() } it("should propagate back-pressure"){ val (src,snk) = generateGraph[Int]() snk.ensureSubscription() src.sendNext(1) snk.expectNoMessage(300.millis) src.sendNext(1) snk.expectNoMessage(300.millis) src.sendComplete() snk.expectNoMessage(300.millis) snk.request(1) snk.expectNext((1,false)) snk.request(1) snk.expectNext((1,true)) snk.expectComplete() } } }
Example 35
Source File: ConcurrentFlow.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.dc.stream.akkautils import akka.NotUsed import akka.stream.{FlowShape, Graph} import akka.stream.scaladsl.{Balance, Flow, GraphDSL, Merge} object ConcurrentFlow { def apply[I, O](parallelism: Int)(flow: Graph[FlowShape[I, O], NotUsed]): Graph[FlowShape[I, O], NotUsed] = GraphDSL.create() { implicit builder => import GraphDSL.Implicits._ val balancer = builder.add(Balance[I](parallelism)) val merger = builder.add(Merge[O](parallelism)) for (i <- 0 until parallelism) { balancer.out(i) ~> flow.async ~> merger.in(i) } FlowShape(balancer.in, merger.out) } }
Example 36
Source File: RefsEnricher.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.bg.imp import akka.NotUsed import akka.stream.FlowShape import akka.stream.contrib.PartitionWith import akka.stream.scaladsl.{Flow, GraphDSL, Merge, Partition} import cmwell.bg.BGMetrics import cmwell.common.formats.BGMessage import cmwell.common._ import cmwell.zstore.ZStore import com.typesafe.scalalogging.LazyLogging import scala.concurrent.ExecutionContext object RefsEnricher extends LazyLogging { def toSingle(bgm: BGMetrics, irwReadConcurrency: Int, zStore: ZStore) (implicit ec: ExecutionContext): Flow[BGMessage[Command], BGMessage[SingleCommand], NotUsed] = { Flow.fromGraph(GraphDSL.create() { implicit b => import GraphDSL.Implicits._ // CommandRef goes left, all rest go right // update metrics for each type of command val commandsPartitioner = b.add(PartitionWith[BGMessage[Command], BGMessage[CommandRef], BGMessage[Command]] { case bgm @ BGMessage(_, CommandRef(_)) => Left(bgm.asInstanceOf[BGMessage[CommandRef]]) case bgm => Right(bgm) }) val commandRefsFetcher = Flow[BGMessage[CommandRef]].mapAsync(irwReadConcurrency) { case bgMessage @ BGMessage(_, CommandRef(ref)) => { zStore.get(ref).map { payload => bgMessage.copy(message = CommandSerializer.decode(payload)) } } } val singleCommandsMerge = b.add(Merge[BGMessage[Command]](2)) commandsPartitioner.out0 ~> commandRefsFetcher ~> singleCommandsMerge.in(0) commandsPartitioner.out1 ~> singleCommandsMerge.in(1) FlowShape(commandsPartitioner.in,singleCommandsMerge.out.map { bgMessage => { // cast to SingleCommand while updating metrics bgMessage.message match { case wc: WriteCommand => bgm.writeCommandsCounter += 1 bgm.infotonCommandWeightHist += wc.infoton.weight case oc: OverwriteCommand => bgm.overrideCommandCounter += 1 bgm.infotonCommandWeightHist += oc.infoton.weight case _: UpdatePathCommand => bgm.updatePathCommandsCounter += 1 case _: DeletePathCommand => bgm.deletePathCommandsCounter += 1 case _: DeleteAttributesCommand => bgm.deleteAttributesCommandsCounter += 1 case unknown => logger.error(s"unknown command [$unknown]") } bgm.commandMeter.mark() bgMessage.copy(message = bgMessage.message.asInstanceOf[SingleCommand]) } }.outlet) }) } }
Example 37
Source File: PromptFlow.scala From akka-http-scala-js-websocket-chat with MIT License | 5 votes |
package example.akkawschat.cli import akka.stream._ import akka.stream.scaladsl.{ Flow, Source, GraphDSL } import akka.stream.stage.{ InHandler, GraphStageLogic, GraphStage } import scala.concurrent.ExecutionContext object Prompt { ⇒ collectedString = collectedString.dropRight(1) prompt() case x ⇒ //println(s"Got ${x.toInt}") collectedString += x print(x) pull(characterInput) } } }) setHandler(outputLinesIn, new InHandler { def onPush(): Unit = { print(s"$RESTORE$ERASE_LINE${grab(outputLinesIn)}\n$SAVE$promptLine") pull(outputLinesIn) } }) setHandler(readLinesOut, eagerTerminateOutput) override def preStart(): Unit = { pull(outputLinesIn) print(SAVE) // to make sure we don't jump back to former SAVE position in the terminal prompt() } def promptLine = s"$RESTORE$ERASE_LINE$SAVE> $collectedString" def prompt(): Unit = { print(promptLine) pull(characterInput) } } }