Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 访问Source.ActorRef创建的akka流源的基础ActorRef_Scala_Akka_Akka Stream - Fatal编程技术网

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都完成-太棒了!谢谢