We have a Play-Project that uses PlayFramework 2.5.4 and MongoDB. 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:
So i found already the documentation of AKKA and old stackoverflowquestions (like How to schedule task daily + onStart() in Play 2.0.4?). 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. 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
Actually all i want to do is call PlayerDBHandler.newDay() every day on the same time.
Thanks for Help
Without seeing the compilation errors, I'm guessing that system
isn't defined. 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. Alternatively, you can look up the actor ref from system
.
Once you've adapted this to do what you want, declare SchedulingTask
in a module.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With