我正在使用石英来计划任务,当尝试通过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,有什么帮助吗?谢谢。@更新:
这是我的
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中的缓存?抱歉
最佳答案
您未发送工作详细信息
JobDetail job = JobBuilder.newJob(EchoJob.class)
.withIdentity("echoJob", "echoGroup")
.usingJobData(EchoJob.PARAM_KEY_MSG, msg)
.build();
试试这个,它将为您服务
关于java - Quartz-无法通过JobDataMap将参数传递给作业,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31607602/