Grails - Parameter in a Quartz job Trigger

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 10:14:10

问题


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) {...}
  }
}


来源:https://stackoverflow.com/questions/7438531/grails-parameter-in-a-quartz-job-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!