2015-01-11 2 views
0

안녕하세요 제 코드는 다중 트리거와 작동하며 내 설정에서 맵을 할당하려고 할 때 각 트리거와 관련된 특정 매개 변수를 jobDataMap.But을 사용하여 전달하려고합니다. jobDataMap에 끝내 내가 루프 위 안에 jobDataMap을 사용하여 어떻게작업 데이터 맵은 석영의 다중 트리거에 대한 매개 변수를 전달합니다.

**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" 
     }   
    } 
} 

은 (는 루프에 대한 내부의 의견을보고 더 많은 분명한 것)하고 그들에게 012 액세스는 NullPointerException를 얻을 실행 메소드 안에?

답변

1

저는 grails 전문가는 아니지만 grails 석영 스케쥴러 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과 함께 전달 될 수 있습니다. 그거 분명해?

+0

안녕하세요, 고맙습니다. :) 이미 여러 개의 트리거가 고정되어 있습니다. 내가 직면 한 주된 문제는 jobDataMap을 사용하여 일정을 잡기 전에 각 트리거에 특정 매개 변수를 전달하는 것입니다. 새 코드를 살펴보십시오. – elyon

+0

당신은 upvoting으로 감사하다고 말할 수 있습니다 :) 트리거를 생성 할 때'usingJobData'를 사용하여 다른 객체를 전달할 수 있습니다 - 제공된 예제를 참조하십시오. – Opal

+0

또는 다음 코드를 사용합니다 :'triggerList [j] .jobDataMap.put ('params', [ 'some', 'list']'. API 참조 : http://quartz-scheduler.org/ apache/2.2.0/org/quartz/JobDataMap.html – Opal

관련 문제