akka.stream.ClosedShape Scala Examples
The following examples show how to use akka.stream.ClosedShape.
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: ThrowExceptionStream.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.stream import java.util.concurrent.atomic.AtomicInteger import akka.{Done, NotUsed} import akka.actor.ActorContext import akka.stream.ClosedShape import akka.stream.scaladsl.GraphDSL.Implicits._ import akka.stream.scaladsl._ import scala.concurrent.Future import scala.language.postfixOps case object NotifyWhenDone { def getInstance: NotifyWhenDone.type = this } object ThrowExceptionStream { val limit = 50000 val exceptionAt = limit * 3 / 10 val recordCount = new AtomicInteger(0) } class ThrowExceptionStream extends PerpetualStream[Future[Int]] { import ThrowExceptionStream._ def streamGraph = RunnableGraph.fromGraph(GraphDSL.create(counter) { implicit builder => sink => startSource ~> injectError ~> sink ClosedShape }) val injectError = Flow[Int].map { n => if (n == exceptionAt) throw new NumberFormatException("This is a fake exception") else n } def counter = Flow[Any].map{ _ => recordCount.incrementAndGet(); 1 }.reduce{ _ + _ }.toMat(Sink.head)(Keep.right) override def receive = { case NotifyWhenDone => import context.dispatcher val target = sender() matValue foreach { v => target ! v } } private def startSource(implicit context: ActorContext): Source[Int, NotUsed] = Source(1 to limit) override def shutdown() = { println("Neo Stream Result " + recordCount.get + "\n\n") Future.successful(Done) } }
Example 2
Source File: Deadlocks.scala From fusion-data with Apache License 2.0 | 5 votes |
package example.akkastream.graph import akka.actor.ActorSystem import akka.stream.{ ActorMaterializer, ClosedShape, OverflowStrategy } import akka.stream.scaladsl.{ Broadcast, Concat, Flow, GraphDSL, Merge, MergePreferred, RunnableGraph, Sink, Source, ZipWith } import scala.io.StdIn object Deadlocks extends App { implicit val system = ActorSystem() implicit val mat = ActorMaterializer() val source = Source(1 to 10) concat <~ bcast ClosedShape }) .run() StdIn.readLine() system.terminate() }
Example 3
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 4
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 5
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 6
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 7
Source File: StreamLoader.scala From recogito2 with Apache License 2.0 | 5 votes |
package controllers.admin.authorities import akka.stream.{ActorAttributes, ClosedShape, Materializer, Supervision} import akka.stream.scaladsl._ import akka.util.ByteString import java.io.InputStream import services.entity.EntityRecord import services.entity.builtin.importer.EntityImporter import play.api.Logger import play.api.libs.json.Json import scala.concurrent.{Await, ExecutionContext} import scala.concurrent.duration._ class StreamLoader(implicit materializer: Materializer) { private val BATCH_SIZE = 100 private val decider: Supervision.Decider = { case t: Throwable => t.printStackTrace() Supervision.Stop } def importPlaces(is: InputStream, crosswalk: String => Option[EntityRecord], importer: EntityImporter)(implicit ctx: ExecutionContext) = { val source = StreamConverters.fromInputStream(() => is, 1024) .via(Framing.delimiter(ByteString("\n"), maximumFrameLength = Int.MaxValue, allowTruncation = false)) .map(_.utf8String) val parser = Flow.fromFunction[String, Option[EntityRecord]](crosswalk) .withAttributes(ActorAttributes.supervisionStrategy(decider)) .grouped(BATCH_SIZE) val sink = Sink.foreach[Seq[Option[EntityRecord]]] { records => val toImport = records.flatten Await.result(importer.importRecords(toImport), 60.minutes) } val graph = RunnableGraph.fromGraph(GraphDSL.create() { implicit builder => import GraphDSL.Implicits._ source ~> parser ~> sink ClosedShape }).withAttributes(ActorAttributes.supervisionStrategy(decider)) graph.run() } }
Example 8
Source File: MergeLogic.scala From cloudflow with Apache License 2.0 | 5 votes |
package cloudflow.akkastream.util.scaladsl import scala.collection.immutable import akka.NotUsed import akka.stream._ import akka.stream.ClosedShape import akka.stream.scaladsl._ import cloudflow.akkastream._ import cloudflow.streamlets._ import cloudflow.akkastream.scaladsl._ import akka.kafka.ConsumerMessage._ override def runnableGraph() = { val inlets = inletPorts.map(inlet ⇒ sourceWithCommittableContext[T](inlet)).toList val out = committableSink[T](outlet) RunnableGraph.fromGraph(GraphDSL.create() { implicit builder: GraphDSL.Builder[NotUsed] ⇒ import GraphDSL.Implicits._ val merge = builder.add(Merger.graph(inlets)) merge ~> out ClosedShape }) } }
Example 9
Source File: IllegalStateStream.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.stream import akka.{Done, NotUsed} import akka.actor.ActorContext import akka.stream.ClosedShape import akka.stream.scaladsl.GraphDSL.Implicits._ import akka.stream.scaladsl._ import org.squbs.unicomplex.Initialized import scala.concurrent.Future import scala.language.postfixOps import scala.util.Try class IllegalStateStream extends PerpetualStream[Future[Int]] { def streamGraph = RunnableGraph.fromGraph(GraphDSL.create(endSink) {implicit builder => sink => startSource ~> sink ClosedShape }) // By accessing matValue here, it should generate an IllegalStateException. // Reporting it to the parent would cause the cube and the system to be at state Failed. val t = Try { Option(matValue.toString) } context.parent ! Initialized(t) private def startSource(implicit context: ActorContext): Source[Int, NotUsed] = Source(1 to 10).map(_ * 1) private def endSink(implicit context: ActorContext): Sink[Int, Future[Int]] = { val sink = Sink.fold[Int, Int](0)(_ + _) sink } override def shutdown() = { print("Neo Stream Result " + matValue.value.get.get + "\n\n") Future.successful(Done) } }
Example 10
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 11
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 12
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 13
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 14
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 15
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 16
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 17
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 18
Source File: Test9.scala From incubator-retired-gearpump with Apache License 2.0 | 5 votes |
package org.apache.gearpump.akkastream.example import akka.NotUsed import akka.actor.{Actor, ActorSystem, Props} import akka.stream.{ActorMaterializer, ActorMaterializerSettings, ClosedShape} import akka.stream.scaladsl._ 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 Test9 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("Test9", akkaConf) implicit val materializer: ActorMaterializer = config.getBoolean("gearpump") match { case true => GearpumpMaterializer() case false => ActorMaterializer( ActorMaterializerSettings(system).withAutoFusing(false) ) } implicit val ec = system.dispatcher val sinkActor = system.actorOf(Props(new SinkActor())) val source = Source((1 to 5)) val sink = Sink.actorRef(sinkActor, "COMPLETE") val flowA: Flow[Int, Int, NotUsed] = Flow[Int].map { x => println(s"processing broadcasted element : $x in flowA"); x } val flowB: Flow[Int, Int, NotUsed] = Flow[Int].map { x => println(s"processing broadcasted element : $x in flowB"); x } val graph = RunnableGraph.fromGraph(GraphDSL.create() { implicit b => import GraphDSL.Implicits._ val broadcast = b.add(Broadcast[Int](2)) val merge = b.add(Merge[Int](2)) source ~> broadcast broadcast ~> flowA ~> merge broadcast ~> flowB ~> merge merge ~> sink ClosedShape }) graph.run() Await.result(system.whenTerminated, 60.minutes) } class SinkActor extends Actor { def receive: Receive = { case any: AnyRef => println("Confirm received: " + any) } } // scalastyle:on println }
Example 19
Source File: Test2.scala From incubator-retired-gearpump with Apache License 2.0 | 5 votes |
package org.apache.gearpump.akkastream.example import akka.actor.{Actor, ActorSystem, Props} import akka.stream.scaladsl._ import akka.stream.{ActorMaterializer, ClosedShape} import org.apache.gearpump.akkastream.GearpumpMaterializer import org.apache.gearpump.akkastream.scaladsl.{GearSink, GearSource} import org.apache.gearpump.cluster.main.ArgumentsParser import org.apache.gearpump.util.AkkaApp import scala.concurrent.Await import scala.concurrent.duration._ object Test2 extends AkkaApp with ArgumentsParser { // scalastyle:off println override def main(akkaConf: Config, args: Array[String]): Unit = { val config = parse(args) implicit val system = ActorSystem("Test2", akkaConf) val gearpumpMaterializer = GearpumpMaterializer() val echo = system.actorOf(Props(new Echo())) val source = GearSource.bridge[String, String] val sink = GearSink.bridge[String, String] val flow = Flow[String].filter(_.startsWith("red")).map("I want to order item: " + _) val (entry, exit) = flow.runWith(source, sink)(gearpumpMaterializer) val actorMaterializer = ActorMaterializer() val externalSource = Source( List("red hat", "yellow sweater", "blue jack", "red apple", "green plant", "blue sky") ) val externalSink = Sink.actorRef(echo, "COMPLETE") RunnableGraph.fromGraph( GraphDSL.create() { implicit b => import GraphDSL.Implicits._ externalSource ~> Sink.fromSubscriber(entry) Source.fromPublisher(exit) ~> externalSink ClosedShape } ).run()(actorMaterializer) Await.result(system.whenTerminated, 60.minutes) } class Echo extends Actor { def receive: Receive = { case any: AnyRef => println("Confirm received: " + any) } } // scalastyle:on println }
Example 20
Source File: Test11.scala From incubator-retired-gearpump with Apache License 2.0 | 5 votes |
package org.apache.gearpump.akkastream.example import akka.NotUsed import akka.stream.ClosedShape import org.apache.gearpump.akkastream.GearpumpMaterializer import org.apache.gearpump.cluster.main.ArgumentsParser import org.apache.gearpump.util.AkkaApp import scala.concurrent.Await import scala.concurrent.duration._ object Test11 extends AkkaApp with ArgumentsParser { // scalastyle:off println override def main(akkaConf: Config, args: Array[String]): Unit = { import akka.actor.ActorSystem import akka.stream.scaladsl._ implicit val system = ActorSystem("Test11", akkaConfig) implicit val materializer = GearpumpMaterializer() // implicit val materializer = // ActorMaterializer(ActorMaterializerSettings(system).withAutoFusing(false)) implicit val ec = system.dispatcher val g = RunnableGraph.fromGraph(GraphDSL.create() { implicit builder: GraphDSL.Builder[NotUsed] => import GraphDSL.Implicits._ val in = Source(1 to 10) val output: (Any) => Unit = any => { val s = s"**** $any" println(s) } val out = Sink.foreach(output) val broadcast = builder.add(Broadcast[Int](2)) val merge = builder.add(Merge[Int](2)) val f1, f2, f3, f4 = Flow[Int].map(_ + 10) in ~> f1 ~> broadcast ~> f2 ~> merge ~> f3 ~> out broadcast ~> f4 ~> merge ClosedShape }) g.run() Await.result(system.whenTerminated, 60.minutes) } // scalastyle:on println }
Example 21
Source File: Test10.scala From incubator-retired-gearpump with Apache License 2.0 | 5 votes |
package org.apache.gearpump.akkastream.example import akka.NotUsed import akka.stream.{ClosedShape, ThrottleMode} import org.apache.gearpump.akkastream.GearpumpMaterializer import org.apache.gearpump.cluster.main.ArgumentsParser import org.apache.gearpump.util.AkkaApp import scala.concurrent.Await import scala.concurrent.duration._ object Test10 extends AkkaApp with ArgumentsParser { // scalastyle:off println override def main(akkaConf: Config, args: Array[String]): Unit = { import akka.actor.ActorSystem import akka.stream.scaladsl._ implicit val system = ActorSystem("Test10", akkaConfig) implicit val materializer = GearpumpMaterializer() implicit val ec = system.dispatcher // Conflate[A] - (2 inputs, 1 output) concatenates two streams // (first consumes one, then the second one) def stream(x: String) = Stream.continually(x) val sourceA = Source(stream("A")) val sourceB = Source(stream("B")) val throttler: Flow[String, String, NotUsed] = Flow[String].throttle(1, 1.second, 1, ThrottleMode.Shaping) val conflateFlow: Flow[String, String, NotUsed] = Flow[String].conflate((x: String, y: String) => x: String) ((acc: String, x: String) => s"$acc::$x") val printFlow: Flow[(String, String), String, NotUsed] = Flow[(String, String)].map { x => println(s" lengths are : ${x._1.length} and ${x._2.length} ; ${x._1} zip ${x._2}") x.toString } val graph = RunnableGraph.fromGraph(GraphDSL.create() { implicit b => import GraphDSL.Implicits._ val zipping = b.add(Zip[String, String]()) sourceA ~> throttler ~> zipping.in0 sourceB ~> conflateFlow ~> zipping.in1 zipping.out ~> printFlow ~> Sink.ignore ClosedShape }) graph.run() Await.result(system.whenTerminated, 60.minutes) } // scalastyle:on println }
Example 22
Source File: Test5.scala From incubator-retired-gearpump with Apache License 2.0 | 5 votes |
package org.apache.gearpump.akkastream.example import akka.actor.{Actor, ActorSystem, Props} import akka.stream.ClosedShape import akka.stream.scaladsl._ import org.apache.gearpump.akkastream.GearpumpMaterializer import org.apache.gearpump.cluster.main.ArgumentsParser import org.apache.gearpump.util.AkkaApp import scala.concurrent.Await import scala.concurrent.duration._ object Test5 extends AkkaApp with ArgumentsParser { // scalastyle:off println override def main(akkaConf: Config, args: Array[String]): Unit = { implicit val system = ActorSystem("Test5", akkaConf) implicit val materializer = GearpumpMaterializer() val echo = system.actorOf(Props(new Echo())) val source = Source(List(("male", "24"), ("female", "23"))) val sink = Sink.actorRef(echo, "COMPLETE") RunnableGraph.fromGraph( GraphDSL.create() { implicit b => import GraphDSL.Implicits._ val unzip = b.add(Unzip[String, String]()) val sink1 = Sink.actorRef(echo, "COMPLETE") val sink2 = Sink.actorRef(echo, "COMPLETE") source ~> unzip.in unzip.out0 ~> sink1 unzip.out1 ~> sink1 ClosedShape } ).run() Await.result(system.whenTerminated, 60.minutes) } class Echo extends Actor { def receive: Receive = { case any: AnyRef => println("Confirm received: " + any) } } // scalastyle:on println }
Example 23
Source File: Test12.scala From incubator-retired-gearpump with Apache License 2.0 | 5 votes |
package org.apache.gearpump.akkastream.example import akka.stream.{ClosedShape, UniformFanInShape} import org.apache.gearpump.akkastream.GearpumpMaterializer import org.apache.gearpump.cluster.main.ArgumentsParser import org.apache.gearpump.util.AkkaApp import scala.concurrent.{Await, Future} object Test12 extends AkkaApp with ArgumentsParser{ // scalastyle:off println override def main(akkaConf: Config, args: Array[String]): Unit = { import akka.actor.ActorSystem import akka.stream.scaladsl._ import scala.concurrent.duration._ implicit val system = ActorSystem("Test12", akkaConfig) // implicit val materializer = ActorMaterializer( // ActorMaterializerSettings(system).withAutoFusing(false) // ) implicit val materializer = GearpumpMaterializer() implicit val ec = system.dispatcher val pickMaxOfThree = GraphDSL.create() { implicit b => import GraphDSL.Implicits._ 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._ // Importing the partial shape will return its shape (inlets & outlets) val pm3 = b.add(pickMaxOfThree) Source.single(1) ~> pm3.in(0) Source.single(2) ~> pm3.in(1) Source.single(3) ~> pm3.in(2) pm3.out ~> sink.in ClosedShape }) val max: Future[Int] = g.run() max.map(x => println(s"maximum of three numbers : $x")) Await.result(system.whenTerminated, 60.minutes) } // scalastyle:on println }
Example 24
Source File: FlowTest.scala From intro-to-akka-streams with Apache License 2.0 | 5 votes |
package com.github.dnvriend.streams.graph import akka.stream.ClosedShape import akka.stream.scaladsl._ import akka.{ Done, NotUsed } import com.github.dnvriend.streams.TestSpec import scala.concurrent.Future class FlowTest extends TestSpec { val ignoreSink: Sink[Int, Future[Done]] = Sink.ignore val resultSink: Sink[Int, Future[Int]] = Sink.head[Int] val foldSink: Sink[AnyRef, Future[Long]] = Sink.fold(0L) { (c, _) ⇒ c + 1 } val in: Source[Int, NotUsed] = Source(1 to 1) "SimpleFlow" should "receive single scalar number" in { val g = RunnableGraph.fromGraph( GraphDSL.create(resultSink) { implicit builder: GraphDSL.Builder[Future[Int]] ⇒ out ⇒ import GraphDSL.Implicits._ val bcast = builder.add(Broadcast[Int](2)) val merge = builder.add(Merge[Int](2)) val f1 = Flow[Int].map(_ + 10).log("f1") val f2 = Flow[Int].map(_ + 20).log("f2") val f3 = Flow[Int].map(_ + 30).log("f3") val f4 = Flow[Int].map(_ + 40).log("f4") in ~> f1 ~> bcast ~> f2 ~> merge ~> f3 ~> out bcast ~> f4 ~> merge ClosedShape } ) g.run().futureValue shouldBe 61 } "Graphs" should "return materialized values when a component has been added to it" in { val xx: RunnableGraph[NotUsed] = RunnableGraph.fromGraph(GraphDSL.create() { implicit b ⇒ import GraphDSL.Implicits._ val src = Source(1 to 5) val snk = Sink.ignore src ~> snk ClosedShape }) val snk = Sink.ignore val xy: RunnableGraph[Future[Done]] = RunnableGraph.fromGraph(GraphDSL.create(snk) { implicit b ⇒ (snk) ⇒ import GraphDSL.Implicits._ val src = Source(1 to 5) src ~> snk ClosedShape }) val snk2 = Sink.ignore val xz: RunnableGraph[(Future[Done], Future[Done])] = RunnableGraph.fromGraph(GraphDSL.create(snk, snk2)(Keep.both) { implicit b ⇒ (s, s2) ⇒ import GraphDSL.Implicits._ val src = Source(1 to 5) val bcast = b.add(Broadcast[Int](2, false)) src ~> bcast bcast ~> s bcast ~> s2 ClosedShape }) val snk3 = Sink.ignore val zz: RunnableGraph[(Future[Done], Future[Done], Future[Done])] = RunnableGraph.fromGraph(GraphDSL.create(snk, snk2, snk3)((_, _, _)) { implicit b ⇒ (s, s2, s3) ⇒ import GraphDSL.Implicits._ val src = Source(1 to 5) val bcast = b.add(Broadcast[Int](3, false)) src ~> bcast bcast ~> s bcast ~> s2 bcast ~> s3 ClosedShape }) // and so on, and so forth... } }