Play Framework 2.0 在服务器启动时安排 Akka Actor [英] Play Framework 2.0 schedules an Akka Actor at server launch

查看:33
本文介绍了Play Framework 2.0 在服务器启动时安排 Akka Actor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Akka actor,它验证随机数据并根据该数据的显示时间对其进行一些更改并更新它.目前我正在做的是在控制器中使用此代码:

I have an Akka actor that validates random data and makes some changes to it based on that data's show time and updates it. Currently what I'm doing is using this code inside a controller:

static ActorRef instance = Akka.system().actorOf(new Props(ValidateAndChangeIt.class));
static {
    Akka.system().scheduler().schedule(
        Duration.Zero(),
        Duration.create(5, TimeUnit.MINUTES),
        instance, "VALIDATE"
    );
}

在控制器中使用它的问题在于,有人必须访问由该控制器处理的页面才能启动actor,如果这没有发生,一切都会暂停.

The problem with using this inside a controller is that someone has to access a page processed by that controller for the actor to start, and if this doesn't happen, everything stays paused.

有没有办法在服务器启动时做到这一点?如果演员产生异常,我实际上不知道它的行为.它会停止未来的时间表还是继续?如果没有,有没有办法让演员重新安排,以防发生任何崩溃或错误?

Is there a way to do this at server start? I actually don't know how it behaves if the actor generates an exception. Does it stop future schedules or does it continue? In case it doesn't, is there any way of making the actor re-schedule in case any crash or error?

推荐答案

要在服务器启动时运行代码,请查看 全局对象:将代码从控制器移动到 onStart() 方法:

For running your code at server startup, take a look at the Global object: move the code from your controller to the onStart() method:

public class Global extends GlobalSettings {

  @Override
  public void onStart(Application app) {
    ActorRef instance = Akka.system().actorOf(new Props(ValidateAndChangeIt.class));
    Akka.system().scheduler().schedule(
        Duration.Zero(),
        Duration.create(5, TimeUnit.MINUTES),
        instance, "VALIDATE"
    );
  }  

}

这篇关于Play Framework 2.0 在服务器启动时安排 Akka Actor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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