I want to use akka's scheduler in my play 2.0(using java) framework application to send email reminders on a particular date and time.I am new to play 2.0. Please tell me the procedure to use the akka scheduler in play 2.0 framework if anyone knows (in detailed) ? Thanks in advance.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
I'm new as well, and have another question related to Akka in Scala. But while reading i found that this might be helpful to you: http://www.playframework.org/documentation/2.0/JavaAkka and perhaps this as well: https://github.com/playframework/Play20/wiki/JavaAsync
回答2:
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 multiple jobs in this class and call them by passing different messages to the receive 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's 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'll also need to add a line in apllication.config file: play.modules.enabled += "com.abc.xyz.JobModule"