Grails - Parameter in a Quartz job Trigger

2019-05-10 23:32发布

问题:

I have the following quartz job, set via Quartz-plugin:

class UserMonthlyNotificationJob { 
static triggers = {
        cron name:'dailyTrigger', cronExpression: " ... "
        cron name:'weeklyTrigger', cronExpression: " ... "
        cron name:'monthlyTrigger', cronExpression: " ... "
}

    def execute(){ ... }
}

I would like to be able to set a parameter in the trigger that would be available in the execute block. It seems I can not set any variable in a cron trigger, and a custom trigger require to implement the Quartz Trigger interface, which I do not know how to do.

Any help greatly appreciated.

回答1:

Make your job implement StatefulJob, then you'll have access to JobExecutionContext which has a Trigger instance accessor. If you have your own Trigger class, that will be an instance of it.



回答2:

Thank you very much, it did the trick. This is how I ended up using it:

import org.quartz.StatefulJob
import org.quartz.JobExecutionContext

class UserPeriodicalNotificationJob implements StatefulJob{   

    static triggers = {
        cron name:'dailyTrigger', cronExpression: ConfigHolder.config.userDailyNotificationJob
        cron name:'weeklyTrigger', cronExpression: ConfigHolder.config.userWeeklyNotificationJob
        cron name:'monthlyTrigger', cronExpression: ConfigHolder.config.userMonthlyNotificationJob   
    }

    void execute(JobExecutionContext context){
        def triggerName = context.trigger.key
        try {
            switch (triggerName) {...}
        }
        catch(Exception) {...}
  }
}