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

FOUND THIS USEFUL? SHARE IT

comments (6)

  1. Evangileen

    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

    Reply
  2. Evangileen

    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

    Reply
  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.

    Reply
  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:
    (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)

    Reply
  5. Rabindra Singh

    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.

    Reply

Leave a Reply to Max Cancel reply

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