Quartz PersistJobDataAfterExecution 执行后不存储

KIC*_*KIC 0 java quartz-scheduler

我使用quartz 2.2.1和mysql als jdbc store,我有一个简单的工作是这样的:

package foo;

import org.quartz.*;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class FooJob2 implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        int count = (int) jobExecutionContext.getJobDetail().getJobDataMap().get("foobar");
        System.out.println("lala neu 3 " + count);
        jobExecutionContext.getJobDetail().getJobDataMap().put("foobar", count++);

    }
}
Run Code Online (Sandbox Code Playgroud)

我像这样安排作业,但是任何时候作业打印 foobar 编号都不会增加。我该怎么做才能使作业持久化 jobdatamap?

public class TestStore {
    public static void main(String[] args) throws SchedulerException, InterruptedException {
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.start();

        JobDataMap data = new JobDataMap();
        data.put("foobar", 12);
        // data.put("foobar", "12");

        // define the job and tie it to our HelloJob class
        JobDetail job = newJob(FooJob2.class)
                .withIdentity("job6", "group1")
                .storeDurably()
                .usingJobData(data)
                .build();

        // Trigger the job to run now, and then repeat every 40 seconds
        Trigger trigger = newTrigger()
                .withIdentity("trigger6", "group1")
                .startNow()
                //.usingJobData(data)
                .withSchedule(simpleSchedule()
                        .withIntervalInSeconds(1)
                        .repeatForever())
                .build();


        // Tell quartz to schedule the job using our trigger
        Set<Trigger> triggers = new HashSet<>();
        triggers.add(trigger);
        try {
            scheduler.scheduleJob(job, trigger);
        } catch (SchedulerException se) {
            System.out.println("update job");
            scheduler.addJob(job, true);
        }


        // Thread.sleep(6000);
        // scheduler.shutdown();
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 7

++count在行中使用。

jobExecutionContext.getJobDetail().getJobDataMap().put("foobar", ++count);
Run Code Online (Sandbox Code Playgroud)