Java Play2- Akka for jobs

2019-06-13 23:04发布

问题:

I am trying to create a job in java play2 using akka.

I always get the same error error: cannot find symbol

And it points to system.actorOf() Intellij and Eclipse don't give me an error msg.

But I can not find this method. I used the following imports

import play.libs.Akka;
import akka.actor.ActorSystem;
import akka.actor.ActorRef;
import akka.actor.UntypedActorFactory;
import akka.actor.UntypedActor;
import akka.actor.Props;
import akka.actor.ActorRefFactory;

Maybe the docs are outdated and they have removed the system.actorOf() ?

public class Global extends GlobalSettings {

    ActorRef tickActor = system.actorOf(new Props().withCreator(new UntypedActorFactory() {
          public UntypedActor create() {
            return new UntypedActor() {
              public void onReceive(Object message) {
                if (message.equals("Log")) {
                  controllers.Application.log();
                } else {
                  unhandled(message);
                }
              }
            };
          }
        }));

    @Override
    public void onStart(Application app) {
        Cancellable cancellable = system.scheduler().schedule(Duration.Zero(), Duration.create(10, TimeUnit.SECONDS),
                tickActor, "Log");

    }
}

EDIT:

.

oh... google redirected me to the outdated documentation. It's Akka.System() now..

  • Can anyone give me an example on how to create the tickActor with up2date code?

http://www.playframework.org/documentation/2.0.2/JavaAkka

回答1:

I strongly suggest that you take a look at the Akka documentation about actors and schedulers.

You can also take a look at this question: Play Framework 2.0 schedules an Akka Actor at server launch



回答2:

solved it.

Btw there are some typos in the documentation.

import java.util.concurrent.TimeUnit;

import play.*;
import play.mvc.*;
import play.mvc.Http.RequestHeader;

import static play.mvc.Results.*;
import play.libs.Akka;
import akka.actor.ActorSystem;
import akka.actor.ActorRef;
import akka.actor.UntypedActorFactory;
import akka.actor.UntypedActor;
import akka.actor.Props;
import akka.actor.ActorRefFactory;

import akka.util.*;
public class Global extends GlobalSettings {

    ActorRef tickActor;

    @Override
    public void onStart(Application app) {
        Logger.info("D");
        tickActor = Akka.system().actorOf((new Props().withCreator(new UntypedActorFactory() {
              public UntypedActor create() {
                    return new UntypedActor() {
                      public void onReceive(Object message) {
                        if (message.equals("Log")) {
                                 //Do something
                         // controllers.Application.log();
                        } else {
                          unhandled(message);
                        }
                      }
                    };
                  }
                })));
        Akka.system().scheduler().schedule(
                  Duration.create(0, TimeUnit.MILLISECONDS),
                  Duration.create(30, TimeUnit.MINUTES),
                  tickActor, 
                  "Log"
                );

    }



}