Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Quartz is a job scheduling framework that can be utilized in Grails to perform common tasks like data backup, email notifications, and report generation. Here’s how it can be used in Grails:

  1. Add the Quartz plugin to your Grails application by including the following line in your BuildConfig.groovy file:
plugins {
    runtime ":quartz:2.1.6"
}
  1. Define your job by creating a new class that extends the QuartzJobBean, and override the executeInternal() method. This method will contain the logic to execute your job. Here’s an example:
class BackupJob extends QuartzJobBean {
    void executeInternal(JobExecutionContext context) throws JobExecutionException {
        // execute backup job logic here
    }
}
  1. Configure the job in the Quartz properties file by defining the job, trigger, and other scheduling options. Here’s an example:
org.quartz.jobBackupJob.name = BackupJob
org.quartz.jobBackupJob.group = Backup
org.quartz.jobBackupJob.description = Scheduled backup job
org.quartz.jobBackupJob.concurrency = 1
org.quartz.jobBackupJob.requestRecovery = true
org.quartz.triggerBackupJob.type = CronTrigger
org.quartz.triggerBackupJob.cronExpression = 0 0 12 * * ?
  1. Use the Quartz API to schedule the job. Here’s an example in the Grails BootStrap.groovy file:
class BootStrap {
    def init = { servletContext ->
        JobDetail job = newJob(BackupJob.class)
            .withIdentity("jobBackupJob", "Backup")
            .storeDurably(true)
            .build()

        CronTrigger trigger = newTrigger()
            .withIdentity("triggerBackupJob", "Backup")
            .withSchedule(cronSchedule("0 0 12 * * ?"))
            .build()

        scheduler.scheduleJob(job, trigger)
    }
}

This code will create a new instance of the BackupJob class, configure the job with the properties defined in the Quartz properties file, and schedule the job to run every day at noon.