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

2

1

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.

Unwatched answered 11/4, 2012 at 10:54 Comment(2)
Have you looked at the doc ? github.com/playframework/Play20/wiki/JavaAkka ("Scheduling asynchronous tasks" section)Imitate
isnt this the same as what I answered in this question - #10086091Contraindicate
W
3

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

Weisburgh answered 12/4, 2012 at 9:50 Comment(0)
G
0

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"

Gradin answered 24/4, 2017 at 16:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.