Running multiple instances of a quartz job dynamically

14 / Jun / 2010 by Bhagwat Kumar 2 comments

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
//import org.codehaus.groovy.grails.plugins.quartz.GrailsTaskClass
//import org.quartz.Trigger
//import org.quartz.SimpleTrigger
// def quartzScheduler /*inject qartzScheduler bean*/

GrailsTaskClass tc=grailsApplication.taskClasses.find{it.fullName=='myPackage.MySampleJob'}
// 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,
                        GTCP.DEFAULT_TRIGGERS_GROUP,
                        tc.getFullName(),
                        tc.getGroup(),
                        new Date(),
                        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)
*/

trigger.jobDataMap.putAll(['triggerName':triggerName, 'myData':1000]) 

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

1.
2. Quartz Plugin source code(QuartzGrailsPlugin.groovy)

Hope it helps you.

~~~~Bhagwat Kumar~~~~
bhagwat@intelligrape.com
IntelliGrape Software

FOUND THIS USEFUL? SHARE IT

comments (2)

  1. Martin

    Dude, thanks anyways, but this example is buggy!

    1. imports missing:
    import org.codehaus.groovy.grails.plugins.quartz.GrailsTaskClass
    import org.quartz.Trigger
    import org.quartz.SimpleTrigger

    2. tc should be it here:
    GrailsTaskClass tc = grailsApplication.taskClasses.find{it.fullName==”myPackage.MySampleJob”}

    3. typo in triggerName1 should be triggerName (if one wants the trigger name to be part of the execution context)
    trigger.jobDataMap.putAll([‘triggerName':triggerName1, ‘myData':1000])

    Thought it helps. Thanks again for this post anyway!

    Cheers
    M

    Reply
    1. Bhagwat Kumar

      Thank you Martin, for sharing the missing imports and pointing out the typo in variable name. I have updated the blog now with the missing imports and typos.

      Reply

Leave a comment - Martin Cancel reply