Scheduling asynchronous tasks in PlayFramework 2.5.X (Java)

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 every time we receive a request and update if the day is over. This leads to some problems:

  • The current player must wait a quiet long time until the request is completed
  • it may happen that in one day there is no update (but we want every day, even if nothing has changed)
  • we must change every request we insert.

So, I already found the AKKA documentation and old stackoverflowquestions (for example, How to schedule the daily + onStart () task in Play 2.0.4? ). But the solutions no longer work.

Akka.system().scheduler() 

outdated

 system.scheduler() 

provides compilingerrors ( from the document ), and I don’t know if import is missing or what else. Since I know that you should use @inject from version 2.4, but I cannot find the correct examples of how to use it with a schedule or how to use it after

In fact, all I want to do is call PlayerDBHandler.newDay () every day at the same time.

thanks for the help

+6
source share
1 answer

Without seeing compilation errors, I assume that system is undefined. By deploying an 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 entered, and you can also enter a link to the actor. In addition, you can watch the actor’s referent with system .

After you have adapted this to do what you want, declare SchedulingTask in the 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 the application to enable the module.

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

All Articles