我想创建一个Source
,然后像下面这样推送元素:
val src = ... // create the Source here
// and then, do something like this
pushElement(x1, src)
pushElement(x2, src)
请问怎样做是推荐的方法?
谢谢!
我想创建一个Source
,然后像下面这样推送元素:
val src = ... // create the Source here
// and then, do something like this
pushElement(x1, src)
pushElement(x2, src)
请问怎样做是推荐的方法?
谢谢!
可以通过以下三种方式实现:
1. 使用SourceQueue进行后处理
您可以使用Source.queue
将Flow材料化为SourceQueue
:
case class Weather(zipCode : String, temperature : Double, raining : Boolean)
val bufferSize = 100
//if the buffer fills up then this strategy drops the oldest elements
//upon the arrival of a new element.
val overflowStrategy = akka.stream.OverflowStrategy.dropHead
val queue = Source.queue(bufferSize, overflowStrategy)
.filter(!_.raining)
.to(Sink foreach println)
.run() // in order to "keep" the queue Materialized value instead of the Sink's
queue offer Weather("02139", 32.0, true)
2. 使用Actor进行后处理
类似的问题和答案可以在这里找到,要点是将流作为ActorRef实例化并向该引用发送消息:
val ref = Source.actorRef[Weather](Int.MaxValue, fail)
.filter(!_.raining)
.to(Sink foreach println )
.run() // in order to "keep" the ref Materialized value instead of the Sink's
ref ! Weather("02139", 32.0, true)
3. 通过Actor进行预物化
同样地,您可以显式地创建一个包含消息缓冲区的Actor,使用该Actor创建一个Source,并按照这里的回答向该Actor发送消息:
object WeatherForwarder {
def props : Props = Props[WeatherForwarder]
}
//see provided link for example definition
class WeatherForwarder extends Actor {...}
val actorRef = actorSystem actorOf WeatherForwarder.props
//note the stream has not been instatiated yet
actorRef ! Weather("02139", 32.0, true)
//stream already has 1 Weather value to process which is sitting in the
//ActorRef's internal buffer
val stream = Source(ActorPublisher[Weather](actorRef)).runWith{...}
mapMaterializedValue
,它会创建另一个源。他使用 Future 来获取他想要返回的源的队列。 - Guillaume MasséSource
有一个preMaterialize
方法。Source
连接到图的其余部分之前获得Source
物化值。这在“物化值驱动”的源(例如Source.queue
、Source.actorRef
或Source.maybe
)的情况下特别有用。SourceQueue
的示例。在物化之前和之后以及从Flow
内推送元素到队列:import akka.actor.ActorSystem
import akka.stream.scaladsl._
import akka.stream.{ActorMaterializer, OverflowStrategy}
implicit val system = ActorSystem("QuickStart")
implicit val materializer = ActorMaterializer()
val sourceDecl = Source.queue[String](bufferSize = 2, OverflowStrategy.backpressure)
val (sourceMat, source) = sourceDecl.preMaterialize()
// Adding element before actual materialization
sourceMat.offer("pre materialization element")
val flow = Flow[String].map { e =>
if(!e.contains("new")) {
// Adding elements from within the flow
sourceMat.offer("new element generated inside the flow")
}
s"Processing $e"
}
// Actually materializing with `run`
source.via(flow).to(Sink.foreach(println)).run()
// Adding element after materialization
sourceMat.offer("post materialization element")
输出:
Processing pre materialization element
Processing post materialization element
Processing new element generated inside the flow
Processing new element generated inside the flow
在尝试和寻找解决方案后,我发现了这个干净、简单且适用于材料化前后的解决方案。
https://dev59.com/cF0a5IYBdhLWcg3wCE3O#32553913 val (ref: ActorRef, publisher: Publisher[Int]) =
Source.actorRef[Int](bufferSize = 1000, OverflowStrategy.fail)
.toMat(Sink.asPublisher(true))(Keep.both).run()
ref ! 1 //before
val source = Source.fromPublisher(publisher)
ref ! 2 //before
Thread.sleep(1000)
ref ! 3 //before
source.runForeach(println)
ref ! 4 //after
Thread.sleep(1000)
ref ! 5 //after
输出:
1
2
3
4
5
Source.actorRef
功能实现,就像您链接的帖子和这里所示的那样:https://dev59.com/cF0a5IYBdhLWcg3wCE3O。非常感谢) - ale64bit