jobDataMap在Quartz中传递Multiple Triggers的参数

时间:2015-01-11 23:57:37

标签: groovy quartz-scheduler

  

您好我的代码使用多个触发器,我试图传递与之关联的特定参数   当我尝试将config.groovy中的地图分配给时,每个触发器都使用jobDataMap.But   jobDataMap我得到一个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"
       }        
    }
}
  

我如何在上面的for循环中使用jobDataMap(它会更多   通过查看for循环中的注释来清除并访问它们   在execute方法中?

1 个答案:

答案 0 :(得分:1)

我不是grails专家,但似乎应该使用grails quartz scheduler 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一起传递。现在清楚了吗?