简体   繁体   English

在PlayFramework 2.5.X(Java)中调度异步任务

[英]Scheduling asynchronus tasks in PlayFramework 2.5.X (Java)

We have a Play-Project that uses PlayFramework 2.5.4 and MongoDB. 我们有一个使用PlayFramework 2.5.4和MongoDB的Play-Project。 We want to update our database daily. 我们希望每天更新我们的数据库。 At the moment we check the time everytime we get a Request and update if a day is over. 目前,我们会在每次收到请求时检查时间,并在一天结束时更新。 That leads to some Problems: 这导致了一些问题:

  1. The current player has to wait a quiet long time until the request finishes 当前玩家必须等待很长时间才能完成请求
  2. it can happen that there is one day no update (but we want everyday one, even if nothing changes) 可能会发生有一天没有更新(但我们想要每天一次,即使没有任何变化)
  3. we have to modify every request we insert. 我们必须修改我们插入的每个请求。

So i found already the documentation of AKKA and old stackoverflowquestions (like How to schedule task daily + onStart() in Play 2.0.4? ). 所以我发现了AKKA和旧的stackoverflowquestions的文档(比如如何在Play 2.0.4中安排每日任务+ onStart()? )。 But the solutions don´t work anymore. 但解决方案不再适用。

Akka.system().scheduler()

is deprecated 已弃用

system.scheduler()

gives compilingerrors ( from docu ) and i dont know if an import is missing or what else. 给出compilingerrors( 来自文档 ),我不知道导入是否丢失或者还有什么。 As i know you should use @inject since Version 2.4, but i cant find proper examples on how to use it with schedule or how to use it afterall 我知道你应该从版本2.4开始使用@inject,但是我找不到关于如何将它与时间表一起使用或者如何使用它的适当例子

Actually all i want to do is call PlayerDBHandler.newDay() every day on the same time. 其实我想做的就是每天都在同一时间调用PlayerDBHandler.newDay()。

Thanks for Help 感谢帮助

Without seeing the compilation errors, I'm guessing that system isn't defined. 没有看到编译错误,我猜测system没有定义。 Expanding the example from the documentation, something like this should work. 扩展文档中的示例,这样的事情应该有效。

public class SchedulingTask {

    @Inject
    public SchedulingTask(final ActorSystem system,
                          @Named("update-db-actor") ActorRef updateDbActor) {
        system.scheduler().schedule(
            Duration.create(0, TimeUnit.MILLISECONDS), //Initial delay
            Duration.create(1, TimeUnit.DAYS),     //Frequency
            updateDbActor,
            "update",
            system.dispatcher(),
            null);
    }
}

system is injected, and you can also inject a reference to the actor. system被注入,你也可以注入一个actor的引用。 Alternatively, you can look up the actor ref from system . 或者,您可以从system查找actor ref。

Once you've adapted this to do what you want, declare SchedulingTask in a module. 一旦你适应了你想做的事情,就在模块中声明SchedulingTask

package com.example;
import com.google.inject.AbstractModule;
import play.libs.akka.AkkaGuiceSupport;

public class MyModule extends AbstractModule implements AkkaGuiceSupport {
    @Override
    protected void configure() {
        bindActor(UpdateDbActor.class, "update-db-actor");
        bind(SchedulingTask.class).asEagerSingleton();
    }
}

Finally, update your application conf to enable the module. 最后,更新您的应用程序配置以启用该模块。

play.modules.enabled += "com.example.MyModule"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM