{"id":318,"date":"2009-12-01T12:08:55","date_gmt":"2009-12-01T06:38:55","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=318"},"modified":"2016-12-19T15:06:04","modified_gmt":"2016-12-19T09:36:04","slug":"rescheduling-a-quartz-job-programmatically","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/rescheduling-a-quartz-job-programmatically\/","title":{"rendered":"Rescheduling a Quartz job programatically"},"content":{"rendered":"<p>In our <a title=\"Grails Development\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">current project on Grails<\/a> we faced a problem\u00a0 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-<\/p>\n<blockquote>\n<div class=\"code\">\n<pre lang=\"groovy\">class MyJob{\r\n    static triggers = {\r\n        simple name: 'mySimpleTrigger', group: 'mySimpleTriggerGroup'\r\n        }\r\n    def execute() {\r\n        println \"${new Date()} -&gt; Job run!\"\r\n    }\r\n}<\/pre>\n<\/div>\n<\/blockquote>\n<p>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.<\/p>\n<blockquote>\n<div class=\"code\">\n<pre lang=\"groovy\">class MyController{\r\n   def quartzScheduler                \/\/ Inject the quartzScheduler bean\r\n\r\n  def reScheduleMyJob={\r\n       def trigger = quartzScheduler.getTrigger(\"mySimpleTrigger\", \"mySimpleTriggerGroup\")\r\n\r\n       trigger.repeatInterval = 5000    \/\/ in millisecods, a long value\r\n       trigger.repeatCount=10           \/\/ Optional int value, if not set it repeats indefinately.\r\n\r\n       Date nextFireTime=quartzScheduler.rescheduleJob(trigger.name, trigger.group, trigger)\r\n\r\n       println \"Next Fire Time : ${nextFireTime}\"\r\n  }\r\n}\r\n<\/pre>\n<\/div>\n<\/blockquote>\n<p>Similarly if you are using Cron expression to schedule the job then use <code>setCronExpression<\/code> method of the trigger instance got from <code>quartzScheduler.getTrigger()<\/code> to reset the cron expression.<\/p>\n<p>It worked for us.Hope it will save your time too.<\/p>\n<p>Helpful links:-<br \/>\nhttp:\/\/old.nabble.com\/Changing-quartz-job-timeout-property-td26512630.html#a26532021<\/p>\n<p>http:\/\/grails.org\/plugin\/quartz<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our current project on Grails we faced a problem\u00a0 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: &#8216;mySimpleTrigger&#8217;, group: &#8216;mySimpleTriggerGroup&#8217; } def [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":59},"categories":[7],"tags":[152,4840,155,148,150,149],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/318"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=318"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/318\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}