Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

playframework 2.0 - Play Framework 2.0 schedules an Akka Actor at server launch

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"
    );
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"
    );
  }  

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...