Introduction to Quartz Scheduler
In modern software development, scheduling jobs to run at specific times is a common requirement. Quartz Scheduler is a powerful and flexible library that provides this functionality. In this blog post, we will walk through the process of setting up and using Quartz Scheduler in a Scala application. We will demonstrate how to create a schedule and assign it to a job using the Quartz Scheduler extension.
Setting Up Your Project
To get started, you need to include the necessary dependencies in your build.sbt file. Add the following dependencies:
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor-typed" % "2.6.18",
"com.typesafe.akka" %% "akka-actor-testkit-typed" % "2.6.18" % Test,
"com.enragedginger" %% "akka-quartz-scheduler" % "1.9.3-akka-2.6.x"
)
Creating the Schedule
Imagine you have a tool that helps you run tasks automatically at specific times. This tool is called a scheduler. In this example, we’re using Quartz Scheduler to set up a task that will run every day at 11:59:59 PM, just one second before midnight, in the UTC time zone.
Example:
QuartzSchedulerExtension
.get(system)
.createSchedule(
name = "JobSchedule",
description = None,
cronExpression = "59 59 23 * * ?",
calendar = None,
timezone = TimeZone.getTimeZone("UTC")
)
- QuartzSchedulerExtension.get(system): We start by getting an instance of the scheduler from our system. Think of it as grabbing our scheduling tool from the toolbox.
- createSchedule: Next, we tell the scheduler to create a new schedule for us.
- name = “JobSchedule”: We give our new schedule a name, “JobSchedule”. This is like labeling our task so we can easily identify it later.
- description = None: We don’t provide a description for this schedule. It’s optional and can be left blank.
- cronExpression = “59 59 23 * * ?”: This is a special string that tells the scheduler exactly when to run the task. In this case, the task is scheduled to run at 11:59:59 PM every day. For more information refer content.
- calendar = None: We don’t use any special calendar rules for this schedule. Again, this is optional.
- timezone = TimeZone.getTimeZone(“UTC”): Finally, we set the time zone to UTC (Coordinated Universal Time), ensuring the task runs at the correct time regardless of where you are in the world.
Scheduling Jobs
By following these steps, you can easily set up a task that sends a specific message to a particular actor in your system. This is useful for triggering actions or processes at scheduled times without manual intervention. Now, we have to schedule the Job to run the task.
- QuartzSchedulerExtension.get(system): We start by getting an instance of the scheduler from our system. Think of it as grabbing our scheduling tool from the toolbox.
- schedule: Next, we tell the scheduler to set up a new task for us.
- name = “JobSchedule“: We give schedule name, “JobSchedule”. The name must be same as when we created the Job while scheduling.
- receiver = userActor: We specify the recipient of the message. In this case, it’s userActor(Actor), which represents the part of our system that will handle the message.
- msg = JobSchedule: Finally, we define the message to be sent. Here,
JobScheduleis the message that will be sent touserActorwhen the task runs.
Example:
QuartzSchedulerExtension
.get(system)
.schedule(
name = "JobSchedule",
receiver = userActor,
msg = JobSchedule
)
Running the Application
To run the application, simply execute the Application object. The job will be scheduled to run at 23:59:59 UTC every day, and you will see the corresponding message printed in the console or task that you want to execute.
sbt run
Conclusion
We demonstrated how to use Quartz Scheduler in a Scala application for creating and scheduling jobs. By following these steps, you can leverage the power of Quartz Scheduler to handle complex scheduling requirements in your applications. Whether you need to run periodic tasks, trigger jobs at specific times, or manage complex schedules, Quartz Scheduler provides the flexibility and reliability you need. Refer sample code.