当它们被包装在 future 中时,可以使用阻塞 actor 消息吗?
我当前的应用程序基于akka 1.1。它有多个 ProjectAnalysisActor
,每个负责处理特定项目的分析任务。当此类参与者收到通用开始消息时,分析就会开始。完成一个步骤后,只要定义了一个步骤,它就会向自己发送一条包含下一步的消息。执行代码基本上如下所示,
sealed trait AnalysisEvent {
def run(project: Project): Future[Any]
def nextStep: AnalysisEvent = null
}
case class StartAnalysis() extends AnalysisEvent {
override def run ...
override def nextStep: AnalysisEvent = new FirstStep
}
case class FirstStep() extends AnalysisEvent {
override def run ...
override def nextStep: AnalysisEvent = new SecondStep
}
case class SecondStep() extends AnalysisEvent {
...
}
class ProjectAnalysisActor(project: Project) extends Actor {
def receive = {
case event: AnalysisEvent =>
val future = event.run(project)
future.onComplete { f =>
self ! event.nextStep
}
}
}
如何为每个分析步骤的运行方法实现我的代码存在一些困难。目前,我在每种跑步方法中创造了一个新的未来。在这个未来中,我将所有后续消息发送到不同的子系统中。其中一些是非阻塞的即发即弃消息,但其中一些返回的结果应该在下一个分析步骤开始之前存储。
目前,典型的运行方法如下所示
def run(project: Project): Future[Any] = {
Future {
progressActor ! typicalFireAndForget(project.name)
val calcResult = (calcActor1 !! doCalcMessage(project)).getOrElse(...)
val p: Project = ... // created updated project using calcResult
val result = (storage !! updateProjectInformation(p)).getOrElse(...)
result
}
}
由于应该避免那些阻塞消息,我想知道这是否是正确的方法。在这个用例中使用它们是否有意义,或者我仍然应该避免它?如果是这样,正确的解决方案是什么?
My current application is based on akka 1.1. It has multiple ProjectAnalysisActors
each responsible for handling analysis tasks for a specific project. The analysis is started when such an actor receives a generic start message. After finishing one step it sends itself a message with the next step as long one is defined. The executing code basically looks as follows
sealed trait AnalysisEvent {
def run(project: Project): Future[Any]
def nextStep: AnalysisEvent = null
}
case class StartAnalysis() extends AnalysisEvent {
override def run ...
override def nextStep: AnalysisEvent = new FirstStep
}
case class FirstStep() extends AnalysisEvent {
override def run ...
override def nextStep: AnalysisEvent = new SecondStep
}
case class SecondStep() extends AnalysisEvent {
...
}
class ProjectAnalysisActor(project: Project) extends Actor {
def receive = {
case event: AnalysisEvent =>
val future = event.run(project)
future.onComplete { f =>
self ! event.nextStep
}
}
}
I have some difficulties how to implement my code for the run-methods for each analysis step. At the moment I create a new future within each run-method. Inside this future I send all follow-up messages into the different subsystems. Some of them are non-blocking fire-and-forget messages, but some of them return a result which should be stored before the next analysis step is started.
At the moment a typical run-method looks as follows
def run(project: Project): Future[Any] = {
Future {
progressActor ! typicalFireAndForget(project.name)
val calcResult = (calcActor1 !! doCalcMessage(project)).getOrElse(...)
val p: Project = ... // created updated project using calcResult
val result = (storage !! updateProjectInformation(p)).getOrElse(...)
result
}
}
Since those blocking messages should be avoided, I'm wondering if this is the right way. Does it make sense to use them in this use case or should I still avoid it? If so, what would be a proper solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然,
ProjectAnalysisActor
的唯一目的是链接未来的调用。其次,运行方法似乎也在等待结果以继续计算。因此,我认为您可以尝试重构代码以使用 Future Composition,如下所述:http://akka.io/docs/akka/1.1/scala/futures.html
Apparently the only purpose of the
ProjectAnalysisActor
is to chain future calls. Second, the runs methods seems also to wait on results to continue computations.So I think you can try refactoring your code to use Future Composition, as explained here: http://akka.io/docs/akka/1.1/scala/futures.html