【问题标题】:Suitable method for scheduler not found with play 2.1.1在 play 2.1.1 中找不到适合调度程序的方法
【发布时间】:2023-04-04 09:31:01
【问题描述】:

我正在尝试将 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 文档上自动重新分配……
我必须在“tick”之后添加 Akka.system().dispatcher()。

【问题讨论】:

    标签: java playframework playframework-2.1


    【解决方案1】:

    这个错误是不言自明的:

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

    所以,ActorRef 不是 Runnable

    您必须以这种方式调用 schedule (嗯,不完全是这种方式,为您的程序修改它):

    system.scheduler().schedule(Duration.create(50, TimeUnit.MILLISECONDS),
    new Runnable() {
     ...   
    }
    }, system.dispatcher());
    

    【讨论】:

    • 谢谢,事实上,它最后缺少了一个 system.dispatcher(),错误不是很明显……还是谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 2017-03-12
    相关资源
    最近更新 更多