Reputation: 24890
I am using quartz to schedule task, when trying to pass parameter to job via JobDataMap
, it don't work, the map is still empty.
Code:
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();
}
}
Inside the execute()
function, the map is still empty, even though I added a value into it, I am using quartz 2.2.1 , any help? Thanks.
@Update:
Here is my 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:
Now I saw it work correctly, I didn't modify anything ... Is that the cache in eclipse or JVM or OS ??? Sorry for that.
Upvotes: 3
Views: 3106
Reputation:
your not sending the job detail
JobDetail job = JobBuilder.newJob(EchoJob.class)
.withIdentity("echoJob", "echoGroup")
.usingJobData(EchoJob.PARAM_KEY_MSG, msg)
.build();
Try this it will work for you
Upvotes: 1