Quartz - 无法通过JobDataMap将参数传递给作业

时间:2015-07-24 10:00:27

标签: java quartz-scheduler

我正在使用quartz来安排任务,当试图通过JobDataMap将参数传递给工作时,它不起作用,地图仍然是空的。

代码:

EchoJob.java

package eric.quartz;

import java.util.Date;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * a simple job, that echo msg,
 * 
 * @author eric
 * @date Jul 24, 2015 3:45:24 PM
 */
public class EchoJob implements Job {
    public static final String PARAM_KEY_MSG = "param_msg";

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.printf("Execute job, at: %s\n", new Date());

        // get param
        JobDetail job = context.getJobDetail();
        JobDataMap jdm = job.getJobDataMap();
        if (jdm.containsKey(PARAM_KEY_MSG)) {
            System.out.printf("param: %s = %s\n", PARAM_KEY_MSG, jdm.get(PARAM_KEY_MSG));
        }
    }
}

QuartzTest.java

package eric.quartz;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
 * Quartz test
 * 
 * @author eric
 * @date Jul 24, 2015 3:31:22 PM
 */
public class QuartzTest {

    /**
     * pass param to job test
     */
    // TODO ... this seem not working ???
    public static void jobPassParamTest() {
        try {
            // create scheduler
            Scheduler sch = StdSchedulerFactory.getDefaultScheduler();

            // create job detail
            JobDetail job = JobBuilder.newJob(EchoJob.class).withIdentity("echoJob", "echoGroup").build();

            // get job data map
            JobDataMap jdm = job.getJobDataMap();

            // pass data to job
            String msg = "hello";
            jdm.put(EchoJob.PARAM_KEY_MSG, msg);

            // create trigger, every 5 seconds,
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("echoTrigger", "echoGroup")
                    .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();

            // start
            sch.start();

            // schedule job
            sch.scheduleJob(job, trigger);

            // wait a while before shutdown, 20 seconds,
            Thread.sleep(1000 * 20);

            // shutdown
            sch.shutdown();
        } catch (SchedulerException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // startTest();
        // scheduleJobTest();
        jobPassParamTest();
    }
}

execute()函数内部,地图仍然是空的,即使我在其中添加了一个值,我使用的是石英2.2.1,有什么帮助吗?感谢。

@Update:

这是我的quartz.properties

# quartz config

org.quartz.scheduler.instanceName = SchedHello
org.quartz.scheduler.instanceId = 1

# thread pool
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
# thread count
org.quartz.threadPool.threadCount = 3

# job storage
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

# other
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false

@Update 2:

现在我看到它正常工作,我没有修改任何东西......这是eclipse或JVM或OS中的缓存吗???对不起。

1 个答案:

答案 0 :(得分:1)

你没有发送工作细节

JobDetail job = JobBuilder.newJob(EchoJob.class)
          .withIdentity("echoJob", "echoGroup")
          .usingJobData(EchoJob.PARAM_KEY_MSG, msg)
          .build();

试试这个会对你有用