jobDataMap 为 Quartz 中的多个触发器传递参数

jobDataMap to pass parameters for Multiple Triggers in Quartz

Hi my code works with multiple triggers and i am trying to pass specific parameters associated with each trigger using jobDataMap.But when i am trying to assign the map in my config.groovy to jobDataMap i get a nullpointerexception

**This is the Map in my Config.groovy-->**

Query
{
    Map
    {
        time.'0/5 * * * * ?' =  ['T1']
        time.'0/10 * * * * ?' =  ['T2']
        templates.'T1'   =  ['Date','FinshDate','Location']
        templates.'T2'   =  ['TableName']
        parameterValues.'T1'   =  ['2014071600','2014072000','Path']
        parameterValues.'T2'   =  ['AppleData']
    }
}   

**This is my Quartz Job Code for multiple triggers ->**

import org.quartz.*
import org.quartz.Trigger
import static org.quartz.JobBuilder.*;
import static org.quartz.CronScheduleBuilder.*;
import static org.quartz.TriggerBuilder.*;
import org.quartz.impl.StdSchedulerFactory;
import org.codehaus.groovy.grails.commons.GrailsApplication;
public class TrialJob 
{

   public static void main(String[] args)
   {
       String JobName
       String GroupName
       GrailsApplication grailsApplication;
       Trigger trigger
       def triggerList=[]
       def jobList=[]

       def cronList=["0/5 * * * * ?","0/10 * * * * ?","0/15 * * * * ?"]

       // here i am creating 3 triggers which works fine
       for(i in 0..2)
       {

          JobName="trigger"+Integer.toString(i) 
          GroupName = "Group"+Integer.toString(i)
          println cronList[i]
          JobDetail job = JobBuilder.newJob(TestJob.class).withIdentity(JobName,GroupName).build();
         trigger= TriggerBuilder.newTrigger().withIdentity(JobName,GroupName).withSchedule(CronScheduleBuilder.cronSchedule(cronList[i])).build();
         triggerList.add(trigger)
         jobList.add(job)
      }

      Scheduler scheduler = new StdSchedulerFactory().getScheduler();
      scheduler.start();

      for(j in 0..2)
      {

       // here i want to put the associated parameters for each trigger in the trigger list 
       // For Example 1)  trigger 0--> triggerList[0].jobDataMap.put(['Date','FinshDate','Location'],['2014071600','2014072000','Path'])
       // 2) trigger 1--> triggerList[1].jobDataMap.put(['TableName'],['AppleData'])
       scheduler.scheduleJob(jobList[j],triggerList[j]);
       println "torpido"
       println j
      }

   //while(true){};
}    

    public static class TestJob implements Job 
    {  
       public void execute(JobExecutionContext context) throws JobExecutionException 
       {
          HashMap<String, String> parameter  =  new HashMap();
          parameter=context.getMergedJobDataMap()
          println "Inside Execute"
       }        
    }
}

how do i use jobDataMap inside the above for loop (it would be more clear by looking at the comments inside the for loop) and access them inside the execute method ?

我不是 grails 专家,但似乎应该使用 grails quartz 调度程序 plugin

您可以在下面找到工作代码:

@Grab(group='org.quartz-scheduler', module='quartz', version='2.2.1') 

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class CronTriggerExample {
    public static void main( String[] args ) throws Exception {

        def cronExpressions = ['0/5 * * * * ?', '0/10 * * * * ?', '0/20 * * * * ?']

        def triggers = cronExpressions.collect { cron ->
            TriggerBuilder
                .newTrigger()
                .withIdentity("trigger-$cron", "trigger-$cron-group")
                .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                .usingJobData(new JobDataMap(['cron': cron]))
                .build()
        }

        Scheduler scheduler = new StdSchedulerFactory().getScheduler()
        scheduler.start()

        triggers.each { trigger ->
            def job = JobBuilder.newJob(HelloJob).withIdentity("$trigger.key.name-job", "$trigger.key.name-job-group").build()
            scheduler.scheduleJob(job, trigger)
        }
        while(true){}
    }
}

public class HelloJob implements Job {
    public void execute(JobExecutionContext context) throws JobExecutionException {
        println "Hello Quartz with cron: ${context.mergedJobDataMap.getString('cron')}"
    }
}

作业名称、作业组以及触发器名称、触发器组必须是唯一的。其他对象可以用 JobDataMap 传递。现在清楚了吗?