How to use akka scheduler in an application for playback 2.0?

I want to use the akka scheduler in the application for the play 2.0 platform (using java) to send reminders by email about a specific date and time. I am new to game 2.0. Please tell me how to use the akka scheduler in the platform play 2.0, if anyone knows (in detail)? Thanks in advance.

+1
source share
2 answers

I am also a newbie, and I have one more question related to Akka in Scala. But when reading, I found that this might be useful for you: http://www.playframework.org/documentation/2.0/JavaAkka and maybe this also: https://github.com/playframework/Play20/wiki/ JavaAsync

+3
source

Module class

import com.google.inject.AbstractModule import play.api.libs.concurrent.AkkaGuiceSupport class JobModule extends AbstractModule with AkkaGuiceSupport { def configure() = { bindActor[JobBucket]("job-bucket-actor") bind(classOf[Scheduler]).asEagerSingleton() } } 

Scheduler class

 import javax.inject._ import akka.actor._ import scala.concurrent.ExecutionContext import scala.concurrent.duration._ class Scheduler @Inject()(val system: ActorSystem, @Named("job-bucket-actor") val jobBucketActor: ActorRef)(implicit ec: ExecutionContext) { system.scheduler.schedule(0.microseconds, 1.day, jobBucketActor, "cleanBucket") } 

JobBucket (you can create several jobs in this class and call them by sending different messages to the receiving method.)

 import javax.inject._ import akka.actor.Actor import org.apache.commons.io.FileUtils import play.api.Logger // You can inject any service class or other class if it required @Singleton class JobBucket extends Actor { def receive = { //You can place n number of cases. case "cleanBucket" => clean() } def clean(): Unit = { //Do whatever you want to do over here. Logger.info("This task has been scheduled...!!!") } } 

You also need to add a line to the apllication.config file: play.modules.enabled + = "com.abc.xyz.JobModule"

0
source

Source: https://habr.com/ru/post/984478/


All Articles