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

Sha*_*vek 9 java quartz-scheduler

场景:我想创建一个调度程序应用程序,它应该按照定义的计划运行shell脚本.为了简单起见,我希望用户在我的应用程序将使用的一些外部文件(properties/xml)中添加脚本名称和执行时间.目前,我计划在Linux服务器上运行此应用程序作为后台进程.将来我们可能会将其作为网络应用程序.

我到现在为止做了什么:

  1. 我遇到xmlschedulingdataprocessorplugin了这个目的,但它要求用户将作业编写为Java代码,然后将其添加到XML文件中.
  2. 我找到了一些目前无效的调度示例.

请建议一些有用的石英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("=======================");
      }
  }
}
Run Code Online (Sandbox Code Playgroud)

我的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);

  }
}
Run Code Online (Sandbox Code Playgroud)

OUTPUT:

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
Run Code Online (Sandbox Code Playgroud)

我错过了什么?我尝试为每次迭代创建新Job,并将脚本名称指定为JobExecutionContext

Ram*_*ava 2

下面的教程帮助您安排 shell 脚本。

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

通过使用

Runtime.getRuntime().exec("sh shellscript.sh");
Run Code Online (Sandbox Code Playgroud)

您可以运行 shell 脚本。