grails.project.dependency.resolution = { ... plugins { ... compile ':quartz:1.0.2' } }You may want to run your Grails application using grails run-app on command line for Grails to download the dependencies.
After installing the plugin, it is easy to create a job using the create-job script:
grails create-job asia.grails.sample.ArchiveTablesThis will create the file PROJECT_ROOT/grails-app/jobs/asia/grails/sample/ArchiveTablesJob.groovy with these contents:
package asia.grails.sampleThe code inside the execute method are called at the intervals defined in the triggers section.class ArchiveTablesJob { static triggers = { simple repeatInterval: 5000l // execute job once in 5 seconds } def execute() { // execute job } }
Many of the things that you can do inside a controller or service can be done inside a Job. In you have a service like this:
class PersonService { def listTallPeople() { return Person.executeQuery("from Person where cmHeight >= 182") } }
You can use the service inside a Job, and so much more:
class UpdatePeopleJob { def personService static triggers = { simple repeatInterval: 5000l // execute job once in 5 seconds } def execute() { println "Hi, Archive Job now running" def tallPeople = personService.listTallPeople() tallPeople.each { tallPerson -> tallPerson.tallFlag = true tallPerson.save() } println "Bye, Archive Job is done now" } }Since a Job is a Groovy class, you can use Groovy language features inside.
This example will execute immediately when a Grails application starts. And then it will be repeatedly invoked every 5 seconds.
static triggers = { simple repeatInterval: 5000l // execute job once in 5 seconds } }
This example will execute 60 seconds after a Grails application starts. And then it will be repeatedly invoked every 15 seconds.
static triggers = { simple startDelay:60000, repeatInterval: 15000l // execute job once in 5 seconds }The problem with simple triggers is you can't control the exact time a job will run. For example, you can create a job to run hourly (repeatInterval: 3600000l) but you can't set it to run at every 15 minute mark of every hour. The timing all depends on what time the first run started.
cronExpression: "s m h D M W Y" | | | | | | `- Year [optional] | | | | | `- Day of Week | | | | `- Month | | | `- Day of Month | | `- Hour | `- Minute `- SecondSpecific values, list or wild-cards are allowed. But understanding cron expressions are best learned through examples:
static triggers = { cron name: 'myTrigger', cronExpression: "0 0/5 * * * ?" }
static triggers = { cron name: 'myTrigger', cronExpression: "0 0 * * * ?" }
static triggers = { cron name: 'myTrigger', cronExpression: "0 0 0/2 * * ?" }
static triggers = { cron name: 'myTrigger', cronExpression: "20 0/10 * * * ?" }
static triggers = { cron name: 'myTrigger', cronExpression: "0 0/30 8-12 * * ?" }
static triggers = { cron name: 'myTrigger', cronExpression: "0 15 12 ? * TUE" }
static triggers = { cron name: 'myTrigger', cronExpression: "0 30 1 ? * MON-SUN" }
PROJECT_ROOT/grails-app/jobs/asia/grails/sample/SampleJob.groovy
package asia.grails.sample import asia.grails.sample.triggers.MyCustomTrigger class SampleJob { static triggers = { custom name:'customTrigger', triggerClass:MyCustomTrigger } def execute() { println "${new Date()} Hello World" } }
PROJECT_ROOT/src/groovy/asia/grails/sample/triggers/MyCustomTrigger.groovy
package asia.grails.sample.triggers import org.quartz.impl.triggers.CalendarIntervalTriggerImpl class MyCustomTrigger extends CalendarIntervalTriggerImpl{ public MyCustomTrigger() { repeatInterval = 1 } @Override public Date computeFirstFireTime(Calendar calendar) { calendar.add(Calendar.SECOND, 5) return calendar.getTime() } @Override public Date getNextFireTime() { Calendar calendar = Calendar.getInstance() calendar.add(Calendar.SECOND, 5) return calendar.getTime() } }CalendarIntervalTriggerImpl is a class that indirectly implements the Trigger interface.