Rescheduling a Quartz job programatically
In our current project on Grails we faced a problem where we need to change the schedule of quartz job programmatically. The timeout value is read from the database. After doing some googling and digging mailing lists we found the following way-
class MyJob{ static triggers = { simple name: 'mySimpleTrigger', group: 'mySimpleTriggerGroup' } def execute() { println "${new Date()} -> Job run!" } }
Name and group attributes helped us in retrieving the associated trigger instance with this job. Here is the code of the action that reschedules the above job.
class MyController{ def quartzScheduler // Inject the quartzScheduler bean def reScheduleMyJob={ def trigger = quartzScheduler.getTrigger("mySimpleTrigger", "mySimpleTriggerGroup") trigger.repeatInterval = 5000 // in millisecods, a long value trigger.repeatCount=10 // Optional int value, if not set it repeats indefinately. Date nextFireTime=quartzScheduler.rescheduleJob(trigger.name, trigger.group, trigger) println "Next Fire Time : ${nextFireTime}" } }
Similarly if you are using Cron expression to schedule the job then use setCronExpression
method of the trigger instance got from quartzScheduler.getTrigger()
to reset the cron expression.
It worked for us.Hope it will save your time too.
Helpful links:-
http://old.nabble.com/Changing-quartz-job-timeout-property-td26512630.html#a26532021
http://grails.org/plugin/quartz
Hi
I have included it in domain class but it is not running can you please tell me the ways to schedule it step by step
Hi
I am new to this can you help me in where to include this job scheduling in grails whether in domain class or controller. I hav
Great post. I was checking constantly this blog and I’m impressed! Extremely useful information particularly the last phase 🙂 I deal with such info a lot. I was looking for this particular info for a very lengthy time. Thank you and good luck.
Thanks you so much for this post, you saved my day!
Hi, can you help me with this, I have tried everything. I am getting the below error when I tried to use reschedule job:
(java.lang.String,java.lang.String,org.quartz.Trigger).
Usually you get this error when there is a missing import, but I think I have all the import statements needed. Here is the code:
Trigger tg = scheduler.getTrigger(triggerKey(“Trigger Name”, “Group”))
I am getting the error on the reschedule code.
scheduler.rescheduleJob(“Trigger Name”, “Group”, Trigger)
Hi.
I am highly impressed by this blog. This gives a lot of information about Quartz. I have also visited following link i.e. giving similarly good information about Quartz framework and its integration with both JSP, Servlet and also with Spring Framework.