如何在java中使用Quartz Cron表达式从db运行任务?

时间:2018-02-13 19:45:54

标签: java cron quartz-scheduler crontrigger

我正在尝试使用后台计划任务创建文件备份应用程序。用户可以为具有不同计划的备份文件创建大量计划。

当我开发样本时,我得到了一个有趣的结果。

每个任务加载成功但最后一个计划有效。这是我的结果:

plan_1 created...
plan_6 created...
plan_11 created...
plan_16 created...
plan_16 executed!
plan_16 executed!
plan_16 executed!
plan_16 executed!
plan_16 executed!
....

这个课程适合我的计划..

public class BasePlan {
    private String name;
    private String filePath;
    private String schedule;
    private boolean state;

    public BasePlan() {
    }
}

这是在db ..

中创建的计划
public class DummyData {
        public static List<BasePlan> getWaitingPlans() {
            List<BasePlan> planList = new ArrayList<BasePlan>();
            for (int i = 1; i <= 20; i += 5) {
                planList.add(new BasePlan("plan_" + i, "0/" + i + " * * * * ?", false, randomFilePath));
            }
            return planList;
        }
    }

此课程用于备份进度

    public class BackupJobFactory implements Job {
        public void execute(JobExecutionContext context) throws JobExecutionException {
            // this method will back up files in different time.
            try {
                SchedulerContext schedulerContext = context.getScheduler().getContext();
                BasePlan plan = (BasePlan) schedulerContext.get("plan");
                plan.setState(true);
                System.out.println(plan.getName() + " executed!");
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }
    }

这是主要的课程

public class MainApplication {

    public static void main(String[] args) throws SchedulerException, ParseException {
        for (BasePlan plan : DummyData.getWaitingPlans()) {

            System.out.println(plan.getName() + " created...");
            JobBuilder jobBuilder = JobBuilder.newJob(BackupJobFactory.class);
            JobDetail job = jobBuilder.withIdentity(plan.getName(), plan.getName()).build();
            Trigger trigger = TriggerBuilder
                    .newTrigger()
                    .withSchedule(CronScheduleBuilder.cronSchedule(plan.getSchedule()))
                    .build();

            Scheduler scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.getContext().put("plan", plan);
            scheduler.start();
            scheduler.scheduleJob(job, trigger);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我改变了我的代码,工作得很好!

for (BasePlan plan : DummyData.getWaitingPlans()) {

            System.out.println(plan.getName() + " created...");
            JobBuilder jobBuilder = JobBuilder.newJob(BackupJobFactory.class);
            JobDetail job = jobBuilder.withIdentity(plan.getName(), plan.getName()).build();
            job.getJobDataMap().put("basePlan", plan);

            Trigger trigger = TriggerBuilder
                    .newTrigger()
                    .withSchedule(CronScheduleBuilder.cronSchedule(plan.getSchedule()))
                    .build();

            Scheduler scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.start();
            scheduler.scheduleJob(job, trigger);
        }
    }