0

我正在尝试将 Akka 和调度程序与 Play 一起使用!java中的框架2.1.1。
我已经在 Scala 和 Play 2.1.0 中使用过它,但是导入并没有全部工作。

我目前的进口:

import play.Application;
import play.GlobalSettings;
import play.Logger;
import play.libs.*;
import utils.MongoUtil;
import play.libs.Akka;
import akka.actor.ActorRef;
import akka.actor.Props;

import java.util.*;
import java.util.concurrent.TimeUnit;

import jobs.*;

import models.User;

import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;

import com.mongodb.*;

我的代码:

            ActorRef cron = Akka.system().actorOf(new Props(Cron.class));
            Akka.system().scheduler().schedule(
              Duration.create(0, TimeUnit.SECONDS),
              Duration.create(24, TimeUnit.HOURS),
              cron,
              "tick"
            );

堆栈跟踪:

 error: no suitable method found for schedule(FiniteDuration,FiniteDuration,ActorRef,String)
[error]                 Akka.system().scheduler().schedule(
[error]                                          ^
[error]     method Scheduler.schedule(FiniteDuration,FiniteDuration,Runnable,ExecutionContext) is not applicable
[error]       (actual argument ActorRef cannot be converted to Runnable by method invocation conversion)
[error]     method Scheduler.schedule(FiniteDuration,FiniteDuration,Function0<BoxedUnit>,ExecutionContext) is not applicable
[error]       (actual argument ActorRef cannot be converted to Function0<BoxedUnit> by method invocation conversion)
[error]     method Scheduler.schedule(FiniteDuration,FiniteDuration,ActorRef,Object,ExecutionContext) is not applicable
[error]       (actual and formal argument lists differ in length)

我尝试了很多在互联网上找到的东西,但似乎没有任何效果。

提前致谢 !

编辑:我真的不喜欢你没有在 API 文档的最新版本上自动重新分配……<br> 我不得不在“tick”之后添加 Akka.system().dispatcher()。

4

1 回答 1

1

The error is pretty self-explanatory:

[error] method Scheduler.schedule(FiniteDuration,FiniteDuration,Runnable,ExecutionContext) is not applicable
[error] (actual argument ActorRef cannot be converted to Runnable by method invocation conversion)

So, ActorRef is not a Runnable.

You must call schedule this way (well, not exactly THIS way, modify it for your program):

system.scheduler().schedule(Duration.create(50, TimeUnit.MILLISECONDS),
new Runnable() {
 ...   
}
}, system.dispatcher());
于 2013-04-29T08:30:16.790 回答