已安排2个石英作业,但只有一个执行两次

时间:2019-01-25 19:10:18

标签: java quartz-scheduler scheduling

我有一个自定义Job和一个自定义JobFactory,因此每个作业可以具有不同的参数(此处:登录到不同的文件)。但是,当我用相同的班级安排不同的工作时,仅执行较晚的计划,但执行两次。我什至尝试复制所有代码,将自定义作业扩展两次,以使MyJob.class中的JobBuilder可以有所不同,但这仍然是相同的问题。即使每个工厂都有自己的类和大量的代码重复,它仍然只会执行一项工作。

public class TripJob implements Job {

    private TripParser parser;
    private CsvWriter writer;

    public TripJob() {

    }

    public void setParser(TripParser parser) {
        this.parser = parser;
    }

    public void setWriter(CsvWriter writer) {
        this.writer = writer;
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        if (parser == null || writer == null) throw new JobExecutionException(getClass().getName() + "'s parser and/or writer not set");
        String result = parser.getResult();
        writer.write(LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")) + result);
    }
}

...

public class TripJobFactory extends SimpleJobFactory {

    private final TripParser parser;
    private final CsvWriter writer;

    public TripJobFactory(TripParser parser, CsvWriter writer) {
        this.parser = parser;
        this.writer = writer;
    }

    @Override
    public Job newJob(TriggerFiredBundle bundle, Scheduler Scheduler) throws SchedulerException {
        TripJob job = (TripJob) super.newJob(bundle, Scheduler);
        if (parser == null || writer == null) throw new SchedulerException(getClass().getName() + "'s parser and/or writer not set");
        job.setParser(parser);
        job.setWriter(writer);
        return job;
    }
}

...

public class TripScheduler {

    private final TripJobFactory factory;

    public TripScheduler(TripJobFactory factory) {
        this.factory = factory;
    }

    public void scheduleJob(String schedule) throws SchedulerException {
        var scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.start();

        var jobDetail = JobBuilder.newJob(TripJob.class)
                .build();

        var trigger = TriggerBuilder.newTrigger()
                .startNow()
                //.withSchedule(CronScheduleBuilder.cronSchedule("0 0/5 * * * ?"))
                .withSchedule(CronScheduleBuilder.cronSchedule(schedule))
                .build();

        scheduler.setJobFactory(factory);
        scheduler.scheduleJob(jobDetail, trigger);
    }
}

在我的主班我打电话

    var toParser = new TripParser(toUrl);
    var toWriter = new CsvWriter("logs/to.csv");
    var toFactory = new TripJobFactory(toParser, toWriter);
    var toScheduler = new TripScheduler(toFactory);

    var fromParser = new TripParser(fromUrl);
    var fromWriter = new CsvWriter("logs/from.csv");
    var fromFactory = new TripJobFactory(fromParser, fromWriter);
    var fromScheduler = new TripScheduler(fromFactory);

    try {
        toScheduler.scheduleJob("0 * * * * ?");
        fromScheduler.scheduleJob("30 * * * * ?");
    } catch (SchedulerException e) {
        e.printStackTrace();
    }

输出为

Wrote '19:53:34;24.0;Min.;24.3;km' to: logs/from.csv
Wrote '19:54:04;24.0;Min.;24.3;km' to: logs/from.csv

但应该

Wrote '19:53:34;24.0;Min.;24.3;km' to: logs/from.csv
Wrote '19:54:04;25.0;Min.;25.3;km' to: logs/to.csv

0 个答案:

没有答案