Future[来源] pipeTo an Actor
Future[Source] pipeTo an Actor
有两个本地演员(未使用远程处理)。示例中的演员已简化:
class ProcessorActor extends Actor {
override def receive: Receive = {
case src:Source[Int, NotUsed] =>
//TODO processing of `src` here
}
}
class FrontendActor extends Actor {
val processor = context.system.actorOf(Props[ProcessorActor])
...
override def receive: Receive = {
case "Hello" =>
val f:Future[Source[Int, NotUsed]] = Future (Source(1 to 100))
f pipeTo processor
}
}
// entry point:
val frontend = system.actorOf(Props[FrontendActor])
frontend ! "Hello"
因此 FrontendActor
发送 Source
到 ProcessorActor
。在上面的例子中它是成功的。
这样的做法可以吗?
是的,这没问题,但并不像您描述的那样有效。 FrontendActor
不发送 Future[Source]
,它只发送 Source
。
来自文档:
pipeTo installs an onComplete-handler on the future to affect the submission of the result to another actor.
换句话说,pipeTo
表示"send the result of this Future
to the actor when it becomes available"。
请注意,即使正在使用远程处理,这也会起作用,因为 Future
在本地解析,不会通过线路发送到远程参与者。
Thus the FrontendActor
sends Source
to ProcessorActor
. In the above example it is works successfully.
Is such approach okay?
不清楚您的顾虑是什么。
在同一个 JVM 上将 Source
从一个演员发送到另一个演员 没问题。因为同一个 JVM 上的参与者间通信,如 documentation 所述,“只是通过引用传递完成”,所以您的示例 1 没有任何异常。本质上,一旦 Future
完成,对 Source
的引用将传递给 ProcessorActor
。 Source
是定义流的一部分的对象;您可以将 Source
从一个演员 本地发送到另一个演员 ,就像您可以发送任何 JVM 对象一样。
(但是,一旦越过单个JVM的边界,就得处理序列化了。)
1 次要的切向观察:FrontendActor
调用 context.system.actorOf(Props[ProcessorActor])
,它创建了一个顶级 actor。通常,顶级 actor 是在主程序中创建的,而不是在 actor 中。
有两个本地演员(未使用远程处理)。示例中的演员已简化:
class ProcessorActor extends Actor {
override def receive: Receive = {
case src:Source[Int, NotUsed] =>
//TODO processing of `src` here
}
}
class FrontendActor extends Actor {
val processor = context.system.actorOf(Props[ProcessorActor])
...
override def receive: Receive = {
case "Hello" =>
val f:Future[Source[Int, NotUsed]] = Future (Source(1 to 100))
f pipeTo processor
}
}
// entry point:
val frontend = system.actorOf(Props[FrontendActor])
frontend ! "Hello"
因此 FrontendActor
发送 Source
到 ProcessorActor
。在上面的例子中它是成功的。
这样的做法可以吗?
是的,这没问题,但并不像您描述的那样有效。 FrontendActor
不发送 Future[Source]
,它只发送 Source
。
来自文档:
pipeTo installs an onComplete-handler on the future to affect the submission of the result to another actor.
换句话说,pipeTo
表示"send the result of this Future
to the actor when it becomes available"。
请注意,即使正在使用远程处理,这也会起作用,因为 Future
在本地解析,不会通过线路发送到远程参与者。
Thus the
FrontendActor
sendsSource
toProcessorActor
. In the above example it is works successfully.Is such approach okay?
不清楚您的顾虑是什么。
在同一个 JVM 上将 Source
从一个演员发送到另一个演员 没问题。因为同一个 JVM 上的参与者间通信,如 documentation 所述,“只是通过引用传递完成”,所以您的示例 1 没有任何异常。本质上,一旦 Future
完成,对 Source
的引用将传递给 ProcessorActor
。 Source
是定义流的一部分的对象;您可以将 Source
从一个演员 本地发送到另一个演员 ,就像您可以发送任何 JVM 对象一样。
(但是,一旦越过单个JVM的边界,就得处理序列化了。)
1 次要的切向观察:FrontendActor
调用 context.system.actorOf(Props[ProcessorActor])
,它创建了一个顶级 actor。通常,顶级 actor 是在主程序中创建的,而不是在 actor 中。