Scala 访问Source.ActorRef创建的akka流源的基础ActorRef
我正在尝试使用该方法创建一个对象。某种形式的Scala 访问Source.ActorRef创建的akka流源的基础ActorRef,scala,akka,akka-stream,Scala,Akka,Akka Stream,我正在尝试使用该方法创建一个对象。某种形式的 import akka.stream.OverflowStrategy.fail import akka.stream.scaladsl.Source case class Weather(zip : String, temp : Double, raining : Boolean) val weatherSource = Source.actorRef[Weather](Int.MaxValue, fail) val sunnySource
import akka.stream.OverflowStrategy.fail
import akka.stream.scaladsl.Source
case class Weather(zip : String, temp : Double, raining : Boolean)
val weatherSource = Source.actorRef[Weather](Int.MaxValue, fail)
val sunnySource = weatherSource.filter(!_.raining)
...
//does not compile
weatherSource ! Weather("90210", 72.0, false)
weatherSource ! Weather("02139", 32.0, true)
我的问题是:如何向基于ActorRef的源对象发送数据
我认为向消息来源发送消息是一种形式
import akka.stream.OverflowStrategy.fail
import akka.stream.scaladsl.Source
case class Weather(zip : String, temp : Double, raining : Boolean)
val weatherSource = Source.actorRef[Weather](Int.MaxValue, fail)
val sunnySource = weatherSource.filter(!_.raining)
...
//does not compile
weatherSource ! Weather("90210", 72.0, false)
weatherSource ! Weather("02139", 32.0, true)
但是weatherSource
没有代码>操作员或告诉方法
对于如何使用Source.actorRef,它并没有太多描述,它只是说您可以
提前感谢您的审阅和回复。您需要一个流程:
import akka.stream.OverflowStrategy.fail
import akka.stream.scaladsl.Source
import akka.stream.scaladsl.{Sink, Flow}
case class Weather(zip : String, temp : Double, raining : Boolean)
val weatherSource = Source.actorRef[Weather](Int.MaxValue, fail)
val sunnySource = weatherSource.filter(!_.raining)
val ref = Flow[Weather]
.to(Sink.ignore)
.runWith(sunnySource)
ref ! Weather("02139", 32.0, true)
记住,这些都是实验性的,可能会改变 正如@Noah指出的akka streams的实验性质,他的答案可能不适用于1.0版本。我必须遵循以下示例:
ActorRef
的实例与所有“物化值”一样,只有在整个流物化后,或者换句话说,在运行RunnableGraph时,才能访问
// RunnableGraph[ActorRef] means that you get ActorRef when you run the graph
val rg1: RunnableGraph[ActorRef] = sunnySource.to(Sink.foreach(println))
// You get ActorRef instance as a materialized value
val actorRef1: ActorRef = rg1.run()
// Or even more correct way: to materialize both ActorRef and future to completion
// of the stream, so that we know when we are done:
// RunnableGraph[(ActorRef, Future[Done])] means that you get tuple
// (ActorRef, Future[Done]) when you run the graph
val rg2: RunnableGraph[(ActorRef, Future[Done])] =
sunnySource.toMat(Sink.foreach(println))(Keep.both)
// You get both ActorRef and Future[Done] instances as materialized values
val (actorRef2, future) = rg2.run()
actorRef2 ! Weather("90210", 72.0, false)
actorRef2 ! Weather("02139", 32.0, true)
actorRef2 ! akka.actor.Status.Success("Done!") // Complete the stream
future onComplete { /* ... */ }
在M5中,似乎Source.actorRef不存在。你知道它移动到哪里了吗?看起来他们基本上改变了这一点,将道具
传递给源代码。更新的文档在这里1.0-RC3是最新的版本,并且源代码。actorRef
仍然住在那里的同一个地方:源代码(道具)
在M5中,现在是源代码。actorPublisher
在RC3中是另一个东西:它用于创建一个由自定义actorPublisher
implementation.hmm支持的源代码,我在尝试获取底层的ActorRef
时遇到了类似的问题,但在创建Sink
之前,我需要ref。这个Flow
能被扔掉,而另一个Flow
能被创建吗?让ActorRef和future都完成-太棒了!谢谢