【问题标题】:How to run tasks from db with Quartz Cron expression in java?如何在 java 中使用 Quartz Cron 表达式从 db 运行任务?
【发布时间】:2018-07-24 06:37:26
【问题描述】:

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

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

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

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() {
    }
}

这是在数据库中创建的计划..

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);
        }
    }
}

【问题讨论】:

  • 你能不能在for循环之外创建调度器并使用相同的实例来调度作业..

标签: java cron quartz-scheduler crontrigger


【解决方案1】:

我像这样更改了我的代码,并且工作正常!

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);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    相关资源
    最近更新 更多