1. Understanding Triggers

1.1. Scheduling configuration syntax

Currently, plugin supports three types of triggers:

  • simple — executes once per defined interval (ex. “every 10 seconds”);

  • cron — executes job with cron expression (ex. “at 8:00am every Monday through Friday”);

  • custom — your implementation of Trigger interface.

Multiple triggers per job are allowed.

class MyJob {

    static triggers = {
        simple(
                name: 'simpleTrigger',
                startDelay: 10000,
                repeatInterval: 30000,
                repeatCount: 10
        )
        cron(
                name: 'cronTrigger',
                startDelay: 10000,
                cronExpression: '0/6 * 15 * * ?',
                timeZone: TimeZone.getTimeZone('GMT-8') // timeZone is optional
        )
        custom(
            name: 'customTrigger',
            triggerClass: MyTriggerClass,
            myParam: myValue,
            myAnotherParam: myAnotherValue
        )
    }

    void execute() {
        println 'Job run!'
    }
}

With this configuration a job will be executed 11 times with a 30-second interval with the first run in 10 seconds after scheduler startup (simple trigger). It will also be executed each 6 seconds during the 15th hour (15:00:00, 15:00:06, 15:00:12, … — this configured by cron trigger), and also each time your custom trigger will fire.

Three kinds of triggers are supported with the following parameters:

  • simple:

    • name — the name that identifies the trigger

    • startDelay — delay (in milliseconds) between scheduler startup and first job’s execution

    • repeatInterval — timeout (in milliseconds) between consecutive job’s executions

    • repeatCount — trigger will fire job execution (1 + repeatCount) times and stop after that (specify zero here to have a one-shot job or -1 to repeat job executions indefinitely)

  • cron:

    • name — the name that identifies the trigger

    • startDelay — delay (in milliseconds) between scheduler startup and first job’s execution

    • cronExpression — cron expression

  • custom:

    • triggerClass — your class which implements Trigger interface any params needed by your trigger.

It is also possible to adjust properties in a trigger Closure by the Grails configuration since the triggers block is given access to the grailsApplication object.

1.1.1. Dynamic Job Scheduling

Starting from the 0.4.1 version, you can schedule job executions dynamically.

These methods are available:

// creates cron trigger
MyJob.schedule(String cronExpression, Map params)

//  creates simple trigger: repeats job repeatCount+1 times with delay of repeatInterval milliseconds
MyJob.schedule(Long repeatInterval, Integer repeatCount, Map params)

// schedules one job execution to the specific date
MyJob.schedule(Date scheduleDate, Map params)

//schedules job's execution with a custom trigger
MyJob.schedule(Trigger trigger)

// force immediate execution of the job
MyJob.triggerNow(Map params)

// Each method (except the one for custom trigger) takes optional 'params' argument.
// You can use it to pass some data to your job and then access it from the job.
class MyJob {
    void execute(context) {
        println(context.mergedJobDataMap.foo)
    }
}

// now in your controller (or service, or something else):
MyJob.triggerNow([foo: 'It Works!'])