how to use akka's scheduler in play 2.0 application?

我的梦境 提交于 2019-12-10 11:34:57

问题


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"



来源:https://stackoverflow.com/questions/10104530/how-to-use-akkas-scheduler-in-play-2-0-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!