Ask Your Question
1

How can Quartz be utilized in Grails?

asked 2023-06-19 15:45:43 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2023-06-19 16:15:01 +0000

scrum gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-19 15:45:43 +0000

Seen: 16 times

Last updated: Jun 19 '23