Rescheduling a Quartz job programatically

01 / Dec / 2009 by Bhagwat Kumar 6 comments

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)

       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:-


comments (6)

  1. Evangileen

    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

  2. Evangileen

    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

  3. prescription glasses sports

    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.

  4. Abraham

    Hi, can you help me with this, I have tried everything. I am getting the below error when I tried to use reschedule job:

    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)

  5. Rabindra Singh

    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.


Leave a Reply

Your email address will not be published. Required fields are marked *