{"id":1086,"date":"2010-06-14T21:17:09","date_gmt":"2010-06-14T15:47:09","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1086"},"modified":"2016-12-19T15:16:58","modified_gmt":"2016-12-19T09:46:58","slug":"running-multiple-instances-of-a-quartz-job-dynamically","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/running-multiple-instances-of-a-quartz-job-dynamically\/","title":{"rendered":"Running multiple instances of a quartz job dynamically"},"content":{"rendered":"<p>I have been facing a problem of running configurable number of instances of the same job to consume the traffic that was varying over time. In order to dynamically create an instance of a job and run it immediately, I googled and found some interesting facts like each trigger is fired with a new instance of the job. We can create any no. of trigger for a job.<\/p>\n<p>Here is a sample code to dynamically create a new trigger for a job and scheduling it to run immediately&#8230;<br \/>\n[groovy]<br \/>\n\/\/import org.codehaus.groovy.grails.plugins.quartz.GrailsTaskClassProperty as GTCP<br \/>\n\/\/import org.codehaus.groovy.grails.plugins.quartz.GrailsTaskClass<br \/>\n\/\/import org.quartz.Trigger<br \/>\n\/\/import org.quartz.SimpleTrigger<br \/>\n\/\/ def quartzScheduler \/*inject qartzScheduler bean*\/<\/p>\n<p>GrailsTaskClass tc=grailsApplication.taskClasses.find{it.fullName==&#8217;myPackage.MySampleJob&#8217;}<br \/>\n\/\/ fully packaged name of the Job. There may be better way to find the Grails Job.<\/p>\n<p>String triggerName=&#8217;mytrigger&#8217;+System.currentTimeMillis() \/\/ unique trigger name<\/p>\n<p>Trigger trigger = new SimpleTrigger(triggerName,<br \/>\n                        GTCP.DEFAULT_TRIGGERS_GROUP,<br \/>\n                        tc.getFullName(),<br \/>\n                        tc.getGroup(),<br \/>\n                        new Date(),<br \/>\n                        null, 0, 1000)<br \/>\n\/*<br \/>\nCreated a simple trigger using constructor :<\/p>\n<p>SimpleTrigger(String name, String group, String jobName, String jobGroup, Date startTime, Date endTime, int repeatCount, long repeatInterval)<br \/>\n*\/<\/p>\n<p>trigger.jobDataMap.putAll([&#8216;triggerName&#8217;:triggerName, &#8216;myData&#8217;:1000]) <\/p>\n<p>\/*<br \/>\npassing map to the trigger that can be accessed inside execute() method of the job using context.mergedJobDataMap<br \/>\n*\/<\/p>\n<p>quartzScheduler.scheduleJob(trigger) \/\/execute the job for this trigger.<br \/>\n[\/groovy]<br \/>\nThe above sample code can be used to create any number of triggers that are executed with a new instance of the specified Quartz Job.<\/p>\n<p>Helpful Links :<\/p>\n<p>1.<br \/>\n2. Quartz Plugin source code(QuartzGrailsPlugin.groovy)<\/p>\n<p>Hope it helps you.<\/p>\n<p>~~~~Bhagwat Kumar~~~~<br \/>\nbhagwat@intelligrape.com<br \/>\nIntelliGrape Software<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been facing a problem of running configurable number of instances of the same job to consume the traffic that was varying over time. In order to dynamically create an instance of a job and run it immediately, I googled and found some interesting facts like each trigger is fired with a new instance [&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":42},"categories":[7],"tags":[279,148,280],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1086"}],"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=1086"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1086\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}