gpt4 book ai didi

java - 在 Quartz 中使用 property/xml 文件动态添加脚本作为作业

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:22:18 24 4
gpt4 key购买 nike

Scenario : I want to create a scheduler application which should run shell scripts as per the defined schedule. To keep it simple, I want the user to add script name and execution timings in some external file (properties/xml) which will be used by my application. For now, I am planning to run this application as a background process on Linux server. In future may be we'll make it as a web-app.

到目前为止我尝试过的:

  1. 为此,我遇到了 xmlschedulingdataprocessorplugin,但它要求用户将作业编写为 Java 代码,然后将其添加到 XML 文件中。
  2. 我找到了 some examples for scheduling目前无法正常工作。

请推荐一些有用的 quartz API 来帮助我实现这个目的。

更新:

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

String[] a = {"script1.sh:0/10 * * * * ?", "script2.sh:0/35 * * * * ?"};

for (String config : a) {

String[] attr = config.split(":");
System.out.println("Iterating for : "+attr[0]);

JobKey jobKey = new JobKey(attr[0], attr[0]);

Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity(attr[0], attr[0])
.withSchedule(CronScheduleBuilder.cronSchedule(attr[1]))
.build();

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

scheduler.getContext().put("val", config);
JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity(jobKey).build();

scheduler.start();
scheduler.scheduleJob(job, trigger);
System.out.println("=======================");
}
}
}

我的HelloJob类:

public class HelloJob implements Job {

public void execute(JobExecutionContext context) throws JobExecutionException {
String objectFromContext = null;
Date date = new Date();
try {
SchedulerContext schedulerContext = context.getScheduler().getContext();
objectFromContext = (String) schedulerContext.get("val");

} catch (SchedulerException ex) {
ex.printStackTrace();
}

System.out.println("Triggered "+objectFromContext+" at: "+date);

}
}

输出:

Iterating for : script1.sh
log4j:WARN No appenders could be found for logger (org.quartz.impl.StdSchedulerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
=======================
Iterating for : script2.sh
=======================
Triggered script2.sh:0/35 * * * * ? at: Mon Apr 18 12:21:50 IST 2016
Triggered script2.sh:0/35 * * * * ? at: Mon Apr 18 12:22:00 IST 2016
Triggered script2.sh:0/35 * * * * ? at: Mon Apr 18 12:22:00 IST 2016
Triggered script2.sh:0/35 * * * * ? at: Mon Apr 18 12:22:10 IST 2016
Triggered script2.sh:0/35 * * * * ? at: Mon Apr 18 12:22:20 IST 2016
Triggered script2.sh:0/35 * * * * ? at: Mon Apr 18 12:22:30 IST 2016
Triggered script2.sh:0/35 * * * * ? at: Mon Apr 18 12:22:35 IST 2016
Triggered script2.sh:0/35 * * * * ? at: Mon Apr 18 12:22:40 IST 2016

我错过了什么?我尝试为每次迭代创建新的作业并将脚本名称分配为 JobExecutionContext

最佳答案

以下教程可帮助您安排 shell 脚本。

http://www.mkyong.com/java/how-to-run-a-task-periodically-in-java/

通过使用

Runtime.getRuntime().exec("sh shellscript.sh");

您可以运行 shell 脚本。

关于java - 在 Quartz 中使用 property/xml 文件动态添加脚本作为作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36466651/

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