使一个Akka-2实例每n个TimeUnits向自己发送一条消息,而不会溢出邮箱

rec*_*ant 5 scala actor akka

Akka-2实例应保持无限循环,并每隔10分钟检查一次数据.

如何设计循环以便实例调用自身,检查要执行的工作然后休眠一段时间?

此外,我看到一个人不能再查询邮箱大小.只要工作任务(在这种情况下是调度功能)处于活动状态,您如何确保忽略消息?

case class Dispatch()

// Automatical start? The start function has been removed since Akka 2 ?
val dispatcher = system.actorOf(Props[MailDispatcher])

class MailDispatcher extends Actor {

  private val interval = Config.getLong("mail.check.interval")
  context.setReceiveTimeout(Duration(interval, TimeUnit.SECONDS))

  def receive = {
    case ReceiveTimeout => {
      self ! Dispatch          
    }
    case Dispatch => dispatch()
    case e: Exception => Logger.error("unexpected Error: " + e)
  }

  def dispatch() {       
      // trigger mail-dispatch       
  }
}
Run Code Online (Sandbox Code Playgroud)

Vik*_*ang 3

我建议如下:

使用:http ://akka.io/docs/akka/2.0-RC2/scala/actors.html#initial-receive-timeout

然后,当您收到 ReceiveTimeout 消息时,您将轮询工作,并将工作发送到您自己的邮箱。

  • 永远不要在你的 Actor 中执行 Thread.sleep(..) 。出于任何原因。只是不要。 (3认同)