Ram*_*gil 27 scala akka akka-stream
我正在尝试使用Source.actorRef方法来创建akka.stream.scaladsl.Source对象.形式的东西
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)
...
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何将数据发送到基于ActorRef的Source对象?
我假设向Source发送消息是一种形式
//does not compile
weatherSource ! Weather("90210", 72.0, false)
weatherSource ! Weather("02139", 32.0, true)
Run Code Online (Sandbox Code Playgroud)
但是weatherSource没有!操作员或tell方法.
该文件是不是关于如何使用Source.actorRef太过描述,它只是说,你可以...
提前感谢您的审核和回复.
Noa*_*oah 24
你需要一个Flow:
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)
Run Code Online (Sandbox Code Playgroud)
请记住,这都是实验性的,可能会改变!
由于@Noah指出了akka-streams的实验性质,他的回答可能不适用于1.0版本.我不得不遵循这个例子给出的例子:
implicit val materializer = ActorMaterializer()
val (actorRef: ActorRef, publisher: Publisher[TweetInfo]) = Source.actorRef[TweetInfo](1000, OverflowStrategy.fail).toMat(Sink.publisher)(Keep.both).run()
actorRef ! TweetInfo(...)
val source: Source[TweetInfo, Unit] = Source[TweetInfo](publisher)
Run Code Online (Sandbox Code Playgroud)
小智 7
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 { /* ... */ }
Run Code Online (Sandbox Code Playgroud)