Grails - stop or reschedule a job

旧巷老猫 提交于 2020-02-02 07:54:25

问题


I am dynamically scheduling jobs, at this way:

JobClass.schedule(Long interval, Integer repeatCount, Map params )

Later I want to stop the job from running, and then restart them again according to the users actions.

How could I stop this trigger?

The only way that did actually stop it was JobClass.removeJob(), but I wasn;t able to start it again later, so I need something else.

Thanks!


回答1:


you can use Scheduler class having methods unscheduleJob which just delete all the triggers bind with the job. For scheduler class object:

Inject in service

def jobManagerService 

use the code to unschedule the job

jobManagerService.getQuartzScheduler().unscheduleJob(TriggerKey triggerkey)

to start the job scheduling: just create a new trigger for the same job and schedule it.

Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(triggerName, triggerGroupName)
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds())
                .forJob(JobKey.jobKey(jobName, groupName))
                .build()

        jobManagerService.getQuartzScheduler().scheduleJob(trigger);


来源:https://stackoverflow.com/questions/20370429/grails-stop-or-reschedule-a-job

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