jobDataMap为Quartz中的多个触发器传递参数 [英] jobDataMap to pass parameters for Multiple Triggers in Quartz

查看:136
本文介绍了jobDataMap为Quartz中的多个触发器传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


你好我的代码与多个触发器一起工作,我试图通过jobDataMap.But传递与
相关联的特定参数,当我试图在我的config.groovy中分配映射时to
jobDataMap我得到一个nullpointerexception




  **这是我的地图Config.groovy  - > ** 

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

**这是我的多重触发器的Quartz工作代码 - > **

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;
触发器触发器
def triggerList = []
def jobList = []

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

//这里我创建3个触发器,它可以正常工作
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(触发器)
jobList.add(job)
}

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

for(j in 0..2)
{

//这里我想把每个触发器的相关参数放在触发器列表中
//对于示例1)触发器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]);
printlntorpido
println j
}

// while(true){};
}

public static class TestJob implements Job
{
public void execute(JobExecutionContext context)throws JobExecutionException
{
HashMap< String,串GT;参数= new HashMap();
parameter = context.getMergedJobDataMap()
printlnInside Execute
}
}
}

$ b


如何在上面的for循环中使用jobDataMap(通过查看for循环内的注释可以更清晰
)并在execute方法中访问它们



解决方案

grails expert,但似乎应该使用grails quartz scheduler 插件



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

  @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()
调度程序。 scheduleJob(job,trigger)
}
while(true){}
}
}

公共类HelloJob实现作业{
public void execute(JobExecutionContext context)throws JobExecutionException {
printlnHello Quartz with cron:$ {context.mergedJobDataMap.getString('cron')}
}
}

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


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 ?

解决方案

I'm not grails expert but it seems that grails quartz scheduler plugin should be used.

Below You can find working code:

@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')}"
    }
}

Job name, job group as well trigger name, trigger group must be unique. Other objects can be passed with JobDataMap. Is that clear now?

这篇关于jobDataMap为Quartz中的多个触发器传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆