Quartz PersistJobDataAfterExecution在执行后不存储

时间:2015-01-23 08:49:28

标签: 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++);

    }
}

我安排这样的工作,但任何时候工作打印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();
    }
}

1 个答案:

答案 0 :(得分:2)

在行中使用++count

jobExecutionContext.getJobDetail().getJobDataMap().put("foobar", ++count);
相关问题