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.
Here is a sample code to dynamically create a new trigger for a job and scheduling it to run immediately…
//import org.codehaus.groovy.grails.plugins.quartz.GrailsTaskClassProperty as GTCP
// def quartzScheduler /*inject qartzScheduler bean*/
// fully packaged name of the Job. There may be better way to find the Grails Job.
String triggerName=’mytrigger’+System.currentTimeMillis() // unique trigger name
Trigger trigger = new SimpleTrigger(triggerName,
null, 0, 1000)
Created a simple trigger using constructor :
SimpleTrigger(String name, String group, String jobName, String jobGroup, Date startTime, Date endTime, int repeatCount, long repeatInterval)
passing map to the trigger that can be accessed inside execute() method of the job using context.mergedJobDataMap
quartzScheduler.scheduleJob(trigger) //execute the job for this trigger.
The above sample code can be used to create any number of triggers that are executed with a new instance of the specified Quartz Job.
Helpful Links :
2. Quartz Plugin source code(QuartzGrailsPlugin.groovy)
Hope it helps you.