An Alternative to Akka Quartz: Time & TimeZone(IST and others) based jobs using Akka Scheduler

Table of contents
Reading Time: 2 minutes

In this blog, we will be scheduling jobs based on time using Akka scheduler. The jobs will be scheduled based on IST(Indian Standard Time). It is basically an alternative to Quartz Scheduler to some extent which is also used to schedule the time-based job. We can schedule jobs at any point of time i.e schedule jobs based on IST or any other TimeZone like UTC

Let’s say, If you want your job to be scheduled at 9:00:00 AM IST every day, What would you do?. Scheduling jobs based on time using Akka scheduler is not easy. So what I have done is, I have written an extra function so that our normal Akka scheduler acts as a Quartz Scheduler but only ‘Time Based ’ not Calendar based. With the example I am going to share you will be able to schedule time-based jobs for almost any TimeZone, you just need to mention the timezone. In this example, I am focusing on IST(Indian Standard Time) but you can tweak it.

Example
Send an E-mail every day at 09:00:00 AM every day.

Akka quartz is one way, but If you can play around with normal Akka scheduler why mess around with Akka quartz.

Alright Let’s head over to the code and see How can we accomplish it.

Here is the Schedule Actor Class

import akka.actor.Actor

class ScheduleActor extends Actor {

  import ScheduleActor._

  var count = 1
//I am using var over here because it's a simple example to increment count. However you should prefer immutability wherever possible.
  def receive: PartialFunction[Any, Unit] = {
    case IncrementNumber => count+=1
    println(count)
  }

}

/**
 * Created by deepak on 22/1/17.
 */
object ScheduleActor {

  case object IncrementNumber

}

Here is the implementation for the job.

import java.text.SimpleDateFormat
import java.util.{Date, TimeZone}

import scala.concurrent.duration._

import ScheduleActor.IncrementNumber
import akka.actor.{ActorSystem, Props}

/**
 * Created by deepak on 22/1/17.
 */
object ScheduleJob extends App {

  val system = ActorSystem("SchedulerSystem")
  val schedulerActor = system.actorOf(Props(classOf[ScheduleActor]),
 "Actor")
  implicit val ec = system.dispatcher
  system.scheduler
    .schedule(calculateInitialDelay().milliseconds, 60.seconds)(
      schedulerActor ! IncrementNumber)
//the first argument in the schedule function is the initial delay 
//the second argument in the schedule function is the interval
  def calculateInitialDelay(): Long = {
    val now = new Date()
    val sdf = new SimpleDateFormat("HH:mm:ss")
    sdf.setTimeZone(TimeZone.getTimeZone("IST"))
    val time1 = sdf.format(now)
    val time2 = "00:00:00" //this is where we provide the time(IST)
 for example I want the job scheduled at 9PM IST I would replace 00:00:00
 with 21:00:00 
    val format = new SimpleDateFormat("HH:mm:ss")
    val date1 = format.parse(time1)
    val date2 = format.parse(time2)
    val timeDifference = date2.getTime() - date1.getTime()
    val calculatedTime = if (timeDifference < 0) 
(Constant.DAYHOURS + timeDifference) else timeDifference
    // val modifiedDate = projectDbService.getModifiedDate("sumit")
    calculatedTime
  }
//Calculate initial delay method basically triggers the job at the IST 
time provided above.
}

That’s all, you are all set to schedule time-based jobs without using Quartz Scheduler.

Full code is available on github
If you find any challenge, Do let me know in the comments.
If you enjoyed this post, I’d be very grateful if you’d help it spread.Keep smiling, Keep coding! Cheers!


scheduling jobs based on time using Akka scheduler

Written by 

Deepak is a Software Consultant having experince of more than 5 years . He is very enthusiastic towards his work and is a good team player. He has sound knowledge of different technologies which include Java, C++, C, HTML, CSS, Javascript, C# always keen to learn new technologies.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading