gpt4 book ai didi

grails - 单个Quartz作业中有多个触发器/Cron表达式

转载 作者:行者123 更新时间:2023-12-02 16:03:25 25 4
gpt4 key购买 nike

I have list of templates and each has different set of parameters , and each template has to execute at specific time.How do i approach this problem in Quartz scheduler


 Template  Parameters list                 Time of execution
T1 ['date','frequency'] 3:30 AM
T2 ['Id'] 10:20 AM
T3 ['customerid','houseNo','Info'] 6:06 PM

and execute() method will perform some operation on parameter list for each template.I want to do this in a single Quartz job. I was trying something like this :


  def list = ["*/2 * * * * ?","*/10 * * * * ?","*/20 * * * * ?"]
String triggerName;
int j=0;
for(cronExpr in list)
{
j++;
triggerName="trigger"+Integer.toString(j)
triggerName = new CronTrigger();
triggerName.setName(triggerName);
triggerName.setGroup(job.getGroup());
triggerName.setJobName(job.getName());
triggerName.setJobGroup(job.getGroup());
triggerName.setCronExpression(cronExpr);
}

I have asked similar question before without any satisfactory answer ,it would be very helpful if someone can provide a better way to approach this problem along with some guide or useful link on quartz scheduling which can walk me through basic and advanced topics so that i have better understanding on how to use multiple triggers or some way to approach the above problem.

最佳答案

在您的情况下,我可能要做的是为一个作业创建多个触发器,以实现所有模板通用的所需业务逻辑。

每个触发器将在其JobDataMap中指定可以与触发器关联的模板参数。触发作业并调用其execute方法后,您可以使用以下代码访问相关的模板参数:

context.getMergedJobDataMap()

有关详细信息,请参见 getMergedJobDataMap JavaDoc。

Java范例:
    public class TemplateJob implements Job {

public void execute(JobExecutionContext context)
throws JobExecutionException
{
JobDataMap dataMap = context.getMergedJobDataMap();

String templateId = dataMap.getString("templateId");

if ("T1".equals(templateId))
{
// template1 params
String t1Date = dataMap.getString("date");
String t1Frequency = dataMap.getString("frequency");

doTemplate1Logic(t1Date, t1Frequency);
}
else if ("T2".equals(templateId))
{
// template2 params
String t2Id = dataMap.get("Id");

doTemplate2Logic(t1Id);
}
else if ("T3".equals(templateId))
{
// template3 params
String t3CustomerId = dataMap.get("customerid");
String t3HouseNo = dataMap.get("houseNo");
String t3Info = dataMap.get("Info");

doTemplate3Logic(t1Id);
}
else
{
throw new JobExecutionException("Unrecognized template ID: " + templateId);
}
}

...
}


public class TestCase
{
public static void main(String[] args)
{
Scheduler scheduler = ....

JobDetail templateJob = JobBuilder.newJob(TemplateJob.class)
.withIdentity("templateJob", "myJobGroup")
.build();

// trigger for Temlate1
Trigger template1Trigger = TriggerBuilder.newTrigger()
.withIdentity("template1Trigger", "myTriggerGroup")
.withSchedule(TriggerBuilder.cronSchedule("*/2 * * * * ?"))
.usingJobData("date", "...")
.usingJobData("frequency", "...")
.forJob("templateJob", "myJobGroup")
.build();
scheduler.scheduleJob(templateJob, template1Trigger);

// trigger for Temlate2
Trigger template2Trigger = TriggerBuilder.newTrigger()
...
scheduler.scheduleJob(templateJob, template2Trigger);

...
}
}

如果各个模板的模板处理逻辑差异很大,则可能应为每个模板实施一个单独的作业。

关于grails - 单个Quartz作业中有多个触发器/Cron表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27827563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com