Akka Scala演员预定的消息似乎没有触发 [英] Akka Scala actor scheduled message does not appear to fire

查看:53
本文介绍了Akka Scala演员预定的消息似乎没有触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试Akka演员的过程中,我编写了一个用例,该用例无法按预期工作。我定义了一个演员 MyActor ,它会收到一条开始 消息。此消息启动了调度程序,该调度程序应在每个中继消息请求 给它自己 1秒。代码如下:

In experimenting with Akka actors, I cooked up a use case which doesn't quite work as expected. I've defined an actor, MyActor which receives a "start" message. This message kicks off a scheduler which should relays a message "request" to it self every 1 second. Here's the code:

import akka.actor._
import scala.concurrent.Await
import scala.concurrent.duration._
import akka.pattern.gracefulStop
import scala.concurrent.ExecutionContext.Implicits.global

object ScheduledActors {

  def main(args: Array[String]) {

    implicit val system = ActorSystem("ScheduledActors")
    val admin = system.actorOf(Props[MyActor], name = "my-actor")

    admin ! "start"

    val adminFuture = gracefulStop(admin, 2 minutes)
    Await.result(adminFuture, 2 minutes)
  }

class MyActor extends Actor {

  var cancellableOption: Option[Cancellable] = None

  def receive = {

    case "start" =>
      println("start")

      cancellableOption =
        Some(context.system.scheduler.schedule(0 seconds, 1 second) { self ! "request" })

    case "request" =>
      println("request")

    case "stop" =>
      println("stop")

      cancellableOption match {
        case None => // no-op
        case Some(v) => v.cancel()
      }

      context.stop(self)
    }
  }
}

我尝试了几种变体。利用 schedule 方法变量和显式的 ActorRef message 。例如:

I've experimented with a handful of variants. Leveraging the schedule method variant with an explicit ActorRef and message. E.g.:

schedule(initialDelay: Duration, frequency: Duration, receiver: ActorRef, message: Any): Cancellable

在每种情况下,请求 消息似乎从不已被演员收到。我已经使用 println 语句作为健全性检查进行了验证,因此可以肯定称为该块。但是交货是行不通的。

In each case, the "request" message appears to never have been received by the actor. The block is most certainly called as I've verified with println statements as a sanity check. But delivery is a no-go.

这使我考虑到 self 引用在实际在此上下文中实际执行时可能不会提供相同的上下文(à la ForkJoinPool调用)。 Akka文档确实警告不要在调度调用的上下文中使用不稳定的引用。但是,自己作为接收者被明确引用为合理的选择

This lead me to consider the self reference might not provide the same context when actually executed in this context (à la ForkJoinPool invocation). The Akka documentation does warn against using unstable references in the context of a schedule invocation. However self as a receiver is explicitly cited as a reasonable alternative.

目前我有点茫然。任何帮助是极大的赞赏。

I'm at a bit of a loss here at this point. Any help is greatly appreciated.

推荐答案

这是因为演员已被终止。运行时,您应该会看到记录的无效字母,例如:

It's because the actor is already terminated. When running, you should see dead letters logged, e.g.:

[INFO] [04/08/2014 17:05:03.903] [ScheduledActors-akka.actor.default -dispatcher-2] [akka:// ScheduledActors / user / my-actor]从Actor [akka:// ScheduledActors / user / my-actor#-1824493491]发送给Actor [akka:/的消息[java.lang.String] / ScheduledActors / user / my-actor#-1824493491]未交付。 [1]遇到死信。可以使用配置设置'akka.log-dead-letters'和'akka.log-dead-letters-during-shutdown'来关闭或调整此日志记录。

[INFO] [04/08/2014 17:05:03.903] [ScheduledActors-akka.actor.default-dispatcher-2] [akka://ScheduledActors/user/my-actor] Message [java.lang.String] from Actor[akka://ScheduledActors/user/my-actor#-1824493491] to Actor[akka://ScheduledActors/user/my-actor#-1824493491] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.

GracefulStop不知道您已完成的计划,并且由于邮箱为空,因此停止了参与者。
如果将main方法更改为以下内容,则应该看到以下消息:

GracefulStop does not know about the scheduling you have done and stops the actor because the mailbox is empty. If you would change the main method to the following, you should see the messages:

implicit val system = ActorSystem("ScheduledActors")
val admin = system.actorOf(Props[MyActor], name = "my-actor")

admin ! "start"

Thread.sleep(5000)
admin ! "stop"

这篇关于Akka Scala演员预定的消息似乎没有触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆