本文整理汇总了Scala中akka.stream.scaladsl.Broadcast类的典型用法代码示例。如果您正苦于以下问题:Scala Broadcast类的具体用法?Scala Broadcast怎么用?Scala Broadcast使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Broadcast类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: FlowFromGraph
//设置package包名称以及导入依赖的类
package sample.graphDSL
import akka.NotUsed
import akka.actor.ActorSystem
import akka.stream.scaladsl.{Broadcast, Flow, GraphDSL, Merge, Sink, Source}
import akka.stream.{ActorMaterializer, FlowShape, UniformFanInShape, UniformFanOutShape}
object FlowFromGraph {
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem("FlowFromGraph")
implicit val ec = system.dispatcher
implicit val materializer = ActorMaterializer()
val processorFlow1: Flow[Int, Int, NotUsed] = Flow[Int].map(_ * 2)
val processorFlow2: Flow[Int, Int, NotUsed] = Flow[Int].map(_ * 3)
val listOfFlows = List(processorFlow1, processorFlow2)
def compoundFlowFrom[T](indexFlows: Seq[Flow[T, T, NotUsed]]): Flow[T, T, NotUsed] = {
require(indexFlows.nonEmpty, "Cannot create compound flow without any flows to combine")
Flow.fromGraph(GraphDSL.create() { implicit b =>
import akka.stream.scaladsl.GraphDSL.Implicits._
val broadcast: UniformFanOutShape[T, T] = b.add(Broadcast(indexFlows.size))
val merge: UniformFanInShape[T, T] = b.add(Merge(indexFlows.size))
indexFlows.foreach(broadcast ~> _ ~> merge)
FlowShape(broadcast.in, merge.out)
})
}
val compoundFlow = compoundFlowFrom(listOfFlows)
Source(1 to 10)
.via(compoundFlow)
.runWith(Sink.foreach(println(_)))
.onComplete(_ => system.terminate())
}
}
示例2: stringPersister
//设置package包名称以及导入依赖的类
package services
import javax.inject.{Inject, Singleton}
import akka.NotUsed
import akka.stream._
import akka.stream.scaladsl.{Broadcast, Flow, GraphDSL, Sink}
import play.api.Logger
import play.api.libs.concurrent.Execution.Implicits._
import scala.concurrent.Future
import scala.util.{Failure, Success}
def stringPersister(pf: String => Future[Unit]): Flow[String, String, NotUsed] =
Flow.fromGraph(GraphDSL.create() { implicit builder =>
import GraphDSL.Implicits._
val persistenceSink = Sink.foreach[String] { content =>
val f = pf(content)
f.onComplete {
case Success(u) => Logger.debug(s"Persisted content: '$content'")
case Failure(t) => Logger.error(s"Failed to persist content: '$content", t)
}
}
val bcast = builder.add(Broadcast[String](2))
bcast.out(1) ~> persistenceSink
FlowShape(bcast.in, bcast.out(0))
})
}
示例3: GraphBuilder
//设置package包名称以及导入依赖的类
package akka_in_action.streams
import akka.actor.ActorSystem
import akka.stream.{ActorMaterializer, ClosedShape}
import akka.stream.scaladsl.{Balance, Broadcast, Flow, GraphDSL, RunnableGraph, Sink, Source}
import scala.concurrent.Await
import scala.concurrent.duration._
object GraphBuilder extends App {
implicit val system = ActorSystem("actor-system")
implicit val materializer = ActorMaterializer()
val topHeadSink = Sink.head[Int]
val middleSink = Sink.head[Int]
val bottomHeadSink = Sink.seq[Int]
val sharedDoubler = Flow[Int].map(_ * 2)
val results = RunnableGraph.fromGraph(GraphDSL.create(topHeadSink, middleSink, bottomHeadSink)
((_, _, _)) { implicit builder =>
(topHS, midHS, bottomHS) =>
import GraphDSL.Implicits._
val broadcast = builder.add(Broadcast[Int](2))
val source = builder.add(Source(1 to 10))
Source.repeat(2) ~> broadcast.in
broadcast.out(0) ~> sharedDoubler ~> topHS.in
broadcast.out(1) ~> sharedDoubler ~> midHS.in
source ~> bottomHS.in
ClosedShape
}).run()
val r1 = Await.result(results._1, 300 millis)
val r2 = Await.result(results._2, 300 millis)
val r3 = Await.result(results._3, 300 millis)
println(r1, r2, r3)
system.terminate()
}
示例4: WorkingWithGraphsApplication
//设置package包名称以及导入依赖的类
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()
}
示例5: WebSocketHandler
//设置package包名称以及导入依赖的类
package com.ulasakdeniz.hakker.websocket
import akka.actor.ActorRef
import akka.http.scaladsl.model.ws.Message
import akka.stream.FlowShape
import akka.stream.scaladsl.{Broadcast, Flow, GraphDSL, Sink}
class WebSocketHandler {
def apply(transform: PartialFunction[Message, Message]): Flow[Message, Message, _] =
flow(transform)
def apply(transform: PartialFunction[Message, Message],
actorRef: ActorRef,
onCompleteMessage: Any): Flow[Message, Message, _] =
flow(transform, Some(Sink.actorRef(actorRef, onCompleteMessage)))
def apply(transform: PartialFunction[Message, Message],
sink: Sink[Message, _]): Flow[Message, Message, _] =
flow(transform, Some(sink))
def flow(transform: PartialFunction[Message, Message],
sinkOpt: Option[Sink[Message, _]] = None): Flow[Message, Message, _] = {
Flow
.fromGraph(GraphDSL.create() { implicit builder =>
import GraphDSL.Implicits._
val incomingMessage = builder.add(Flow[Message].collect(transform))
val in = sinkOpt
.map(sink => {
val broadcast = builder.add(Broadcast[Message](2))
val sinkShape = builder.add(sink)
broadcast.out(0) ~> sinkShape.in
broadcast.out(1) ~> incomingMessage.in
broadcast.in
})
.getOrElse {
incomingMessage.in
}
FlowShape(in, incomingMessage.out)
})
.via(failureHandler)
}
private def failureHandler: Flow[Message, Message, _] =
Flow[Message].recover[Message] {
case ex: Exception =>
// TODO: handle
throw ex
}
}
示例6: Demo3
//设置package包名称以及导入依赖的类
package lew.bing.akka.stream
import akka.NotUsed
import akka.actor.ActorSystem
import akka.stream.scaladsl.{Broadcast, Flow, GraphDSL, Merge, RunnableGraph, Sink, Source}
import akka.stream.{ActorMaterializer, ClosedShape}
object Demo3 {
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem("QuickStart")
implicit val materializer = ActorMaterializer()
val g = RunnableGraph.fromGraph(GraphDSL.create(){implicit builder:GraphDSL.Builder[NotUsed] =>
import GraphDSL.Implicits._
val in = Source(1 to 10)
val out = Sink.foreach(println)
val bcast = builder.add(Broadcast[Int](2))
val merge = builder.add(Merge[Int](2))
val f1, f2, f3, f4 = (k:Int) => Flow[Int].map(i => {
println(s"??$k : $i")
i + 10
})
in ~> f1(1) ~> bcast ~> f2(2) ~> merge ~> f3(3) ~> out
bcast ~> f4(4) ~> merge
ClosedShape
})
g.run()
Thread.sleep(3000)
System.exit(0)
}
}
示例7: ChatStreamHandler
//设置package包名称以及导入依赖的类
package com.example.routes
import akka.http.scaladsl.model.ws.{Message, TextMessage}
import akka.stream.{FlowShape, Materializer, OverflowStrategy}
import akka.stream.scaladsl.{Broadcast, Flow, GraphDSL, Merge, Sink, Source}
import com.example.chat.{ChatSystemMessages, ChatSystemService}
import com.example.chat.ChatSystemMessages._
case class ChatStreamHandler()(implicit chatSystemService: ChatSystemService, materializer: Materializer) {
def handler(userId: String): Flow[Message, Message, _] =
Flow.fromGraph(GraphDSL.create(Source.actorRef[SystemMessage](5, OverflowStrategy.fail)) {
implicit builder =>
participantSource =>
import GraphDSL.Implicits._
val fromClientFlow = builder.add(Flow[Message].collect {
case TextMessage.Strict(txt) => NewMessage(userId, MessageBody(txt))
})
val merge = builder.add(Merge[ChatSystemMessage](3))
val broadcast = builder.add(Broadcast[ChatSystemMessage](2))
val toClientFlow = builder.add(Flow[SystemMessage].map[Message] {
case SystemMessage(body) => TextMessage.Strict(body)
})
val actorMaterializedSource = builder.materializedValue.map {
actor => chatSystemService.findUser(userId) match {
case Some(_) => JoinedMessage(userId, actor)
case None => RejectMessage(userId, actor)
}
}
val reportLastMessageFlow = builder.add(Flow[ChatSystemMessage].map {
case JoinedMessage(_, userActor) => ReportMessage(chatSystemService.findMessages(20), userActor)
case [email protected]_ => m
})
val registerMessageFlow = builder.add(Flow[ChatSystemMessage].map {
case [email protected](_userId, body) =>
chatSystemService.addMessage(_userId, body.value)
msg
case [email protected]_ => msg
})
def toChatGroupActorSink(userId: String) =
Sink.actorRef[ChatSystemMessages.ChatSystemMessage](chatSystemService.chatGroupActor, LeftMessage(userId))
actorMaterializedSource ~> broadcast
broadcast ~> reportLastMessageFlow ~> merge
broadcast ~> merge
fromClientFlow ~> registerMessageFlow ~> merge ~> toChatGroupActorSink(userId)
participantSource ~> toClientFlow
FlowShape(fromClientFlow.in, toClientFlow.out)
})
}
示例8: Utilities
//设置package包名称以及导入依赖的类
package io.plasmap.util.streams
import akka.NotUsed
import akka.stream._
import akka.stream.scaladsl.{Broadcast, Flow, GraphDSL, Zip}
object Utilities {
def reduceByKey[In, K, Out](
maximumGroupSize: Int,
groupKey: (In) => K,
map: (In) => Out)(reduce: (Out, Out) => Out): Flow[In, (K, Out), NotUsed] = {
Flow[In]
.groupBy[K](maximumGroupSize, groupKey)
.map(e => groupKey(e) -> map(e))
.reduce((l, r) => l._1 -> reduce(l._2, r._2))
.mergeSubstreams
}
def groupAndMapSubFlow[In, K, Out](groupKey: (In) => K,
subFlow: Flow[In, Out, NotUsed],
maximumGroupSize: Int
): Flow[In, Out, NotUsed] = {
Flow[In]
.groupBy[K](maximumGroupSize, groupKey)
.via(subFlow)
.mergeSubstreams
}
private[streams] def subflowWithGroupKey[In, K, Out](groupKey: (In) => K, subFlow: Flow[In, Out, NotUsed]):
Flow[In, (K, Out), NotUsed] = Flow.fromGraph {
GraphDSL.create() { implicit b =>
import GraphDSL.Implicits._
val broadcast = b.add(Broadcast[In](outputPorts = 2, eagerCancel = true))
val repeater = b.add(InfiniteRepeat[K])
val zip = b.add(Zip[K, Out])
broadcast ~> subFlow ~> zip.in1
broadcast ~> Flow[In].map(groupKey) ~> repeater ~> zip.in0
FlowShape(broadcast.in, zip.out)
}
}
def groupAndMapSubflowWithKey[In, K, Out](
groupKey: (In) => K,
subFlow: Flow[In, Out, NotUsed],
maximumGroupSize: Int): Flow[In, (K, Out), NotUsed] = {
val subFlowWithGroupKey: Flow[In, (K, Out), NotUsed] = subflowWithGroupKey(groupKey, subFlow)
groupAndMapSubFlow[In, K, (K, Out)](groupKey, subFlowWithGroupKey, maximumGroupSize)
}
}