DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Schedule a Job in Play With Akka

Schedule a Job in Play With Akka

Follow along to see this hands-on approach to job scheduling in Scala using the Play framework.

Rahul Kumar user avatar by
Rahul Kumar
·
Oct. 17, 16 · Java Zone · Tutorial
Like (6)
Save
Tweet
8.08K Views

Join the DZone community and get the full member experience.

Join For Free

Greetings to all!

In this blog, we will see how we can schedule a job after some time period in play using Scala. Before we get started, I'm assuming you all have a little knowledge of Akka, Play, and Scala.

So here, we start with an actor where we will write our job to be scheduled as follows:

import javax.inject.{Inject, Singleton}
import akka.actor.Actor
import play.api.Logger
import scala.concurrent.ExecutionContext
@Singleton
class JobSchedulerActor @Inject()(implicit ec: ExecutionContext) extends Actor {
    var c= 0
    override def receive: Receive = {
        case _ =>
        c = c+1
        Logger.info("Job Scheduled "+ c )
        //Here put your code for your job
    }
}

Now, we will define the scheduler for the above job to execute at a certain duration:

class Scheduler @Inject() (val system: ActorSystem, @Named("<strong>JobSchedulerActor</strong>") val j<strong>obSchedulerActor</strong>: ActorRef)(implicit ec: ExecutionContext) {
    var actor = system.scheduler.schedule(
    0.seconds, 30.seconds,JobSchedulerActor, "schedule")
}

Now we need to bind the actor and scheduler:

import actor.SchedulerActor
import com.google.inject.AbstractModule
import play.api.Logger
import play.api.libs.concurrent.AkkaGuiceSupport
import scheduler.Scheduler

class BindModule extends AbstractModule with AkkaGuiceSupport {
    def configure() = {
        Logger.info("\nConfigurring Scheduler.")
        bindActor[<strong>JobSchedulerActor</strong>]("<strong>JobSchedulerActor</strong>")
        bind(classOf[Scheduler]).asEagerSingleton()
    }
}

Now we tell Play to start the actor after the scheduled time. We only need to write the following configuration inside application.conf :

play.modules.enabled += "modules.BindModule"

And with that, your scheduler will start scheduling your job after the specified duration once your server is up.

career Schedule (computer science) Akka (toolkit)

Published at DZone with permission of Rahul Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Best Team Ever
  • Edge Computing: Implementation, Advantages, and Disadvantages
  • Pub/Sub Design Pattern in .NET Distributed Cache
  • Instancio: Test Data Generator for Java (Part 2)

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo