Working With Akka Quartz Scheduler

Reading Time: 2 minutes

In this blog, I am going to discuss how you can schedule tasks with the help of akka quartz scheduler. Also, we can use akka scheduler to execute tasks but akka scheduler is not design for the long-term scheduling. Instead of this, we use akka quartz scheduler.

With the help of akka quartz scheduler, we can execute the task on the basis of cron expression. Suppose we want to execute tasks every first day of the month so akka quartz scheduler is a better option than a simple akka scheduler.

Let’s understand the working of akka quartz scheduler with the help of code.

First you have to create the actor like the following.

import akka.actor.Actor
import ScheduleActor.ExecuteTask

class ScheduleJob extends Actor with LazyLogging {

  def receive: PartialFunction[Any, Unit] = {
    case ExecuteTask => logger.info("i am going to execute every first day of the month")
    case _ => logger.info("invalid message")
  }
}

object ScheduleJob {
  
  case object ExecuteTask
}

In the above code, we create an actor with the name ScheduleJob. Also, this actor will perform the task of printing a message on every first day of the month. So let’s see how you can schedule this actor to perform its task on every first day of the month with the help of the code.

import java.util.TimeZone

import ScheduleActor.ExecuteTask
import akka.actor.{Actor, ActorSystem, Props}
import com.typesafe.akka.extension.quartz.QuartzSchedulerExtension

object Driver extends App {

  val system = ActorSystem("SchedulerSystem")
  implicit val executionContext: ExecutionContextExecutor = system.dispatcher
  val scheduler = QuartzSchedulerExtension
  val schedulerActor = system.actorOf(Props(classOf[ScheduleJob]),
    "Schedule Job")
  
  QuartzSchedulerExtension.get(system).createSchedule("monthlyScheduler", None,
    "0 0 0 1 1/1 ? *", None, TimeZone.getTimeZone("Asia/Calcutta"))
 
  QuartzSchedulerExtension.get(system).schedule("monthlyScheduler", schedulerActor,
    ExecuteTask)
}

In the above code, First we have created the ActorSystem and after that, we have created the actorRef of scheduleJob actor. So that we can send messages to ScheduleJob actor. For akka quartz scheduler we need to import the QuartzSchedulerExtension class. So that we can create scheduler and execute that scheduler. After that, we call the method createSchedule of the QuartzSchedulerExtension class. And then we pass the name of the scheduler, cron expression and time zone as the parameters of the method. Always remember you have to pass the valid cron expression otherwise this method will throw a runtime exception. So in the above code, We pass the cron expression of executing the task on every first day of the month at midnight.

After that, we call the method scheduler of QuartzSchedulerExtension which is used to schedule the created scheduler with the actor. And then we pass the name of the scheduler, actor ref of the actor and the message that we want to send to the actor as the parameters of the method.

So whenever we run the application, the message will be printed on every first day of the month at midnight.

That’s all and Thank you for reading this blog.

References

https://index.scala-lang.org/enragedginger/akka-quartz-scheduler/akka-quartz-scheduler/1.8.1-akka-2.5.x?target=_2.13

Knoldus-blog-footer-image