[Logo] Terracotta Discussion Forums (LEGACY READ-ONLY ARCHIVE)
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
[Expert]
how do I get JobDataMap to work?  XML
Forum Index -> Quartz
Author Message
Anonymous



I need to set some data on my job instances for each trigger. I also need a timeout mechanism, which seems to be working for me so far. However, my property does not seem to be getting set from the JobDataMap when my job runs.

What am I doing wrong?


Code:
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
 scheduler.start();
 JobListener listener1 = new ExampleJobListener("job1Listener");
 JobListener listener2 = new ExampleJobListener("job2Listener");
 JobDetail job1 = new JobDetail("job1", "group1", ExampleJob.class);
 JobDetail job2 = new JobDetail("job2", "group1", ExampleJob.class);
 job1.addJobListener("job1Listener");
 job2.addJobListener("job2Listener");
 JobDataMap jobData1 = new JobDataMap();
 jobData1.put("mykey", "Hello Job1");
 JobDataMap jobData2 = new JobDataMap();
 jobData2.put("mykey", "Hello Job2");
 Trigger trigger1 = new CronTrigger("trigger1", "group1", "10 * * * * ?");
 Trigger trigger2 = new CronTrigger("trigger2", "group1", "10 * * * * ?");
 trigger1.setJobDataMap(jobData1);
 trigger2.setJobDataMap(jobData2);
 scheduler.addJobListener(listener1);
 scheduler.addJobListener(listener2);
 scheduler.scheduleJob(job1, trigger1);
 scheduler.scheduleJob(job2, trigger2);



Code:
public class ExampleJob implements Job, InterruptableJob {
 	private String mykey;
 
 	public void setMykey(String mykey) {
 		this.mykey = mykey;
 	}
 
 	@Override
 	public void execute(JobExecutionContext jobContext) throws JobExecutionException {
 		System.out.println("starting job");
 		try {
 			System.out.println("mykey: " + this.mykey);
 			Thread.sleep(30000); // pretend like we are doing something
 		} catch (InterruptedException ie) {
 			System.out.println("job interrupted by a timeout");
 		}
 		System.out.println("ending job");
 	}
 
 	@Override
 	public void interrupt() throws UnableToInterruptJobException {
 		System.out.println("job interrupted");
 	}
 }


Code:
public class ExampleJobListener implements JobListener {
 
 	private Thread timeoutThread;
 	private final String name;
 
 	public ExampleJobListener(String name) {
 		this.name = name;
 	}
 
 	@Override
 	public String getName() {
 		return this.name;
 	}
 
 	@Override
 	public void jobExecutionVetoed(JobExecutionContext jobContext) {}
 
 	@Override
 	public void jobToBeExecuted(JobExecutionContext jobContext) {
 		this.timeoutThread = new JobTimeoutThread(jobContext);
 		this.timeoutThread.run();
 	}
 
 	@Override
 	public void jobWasExecuted(JobExecutionContext jobContext, JobExecutionException jee) {
 		this.timeoutThread.interrupt();
 	}
 
 }


Code:
public class JobTimeoutThread extends Thread {
 
 	private final JobExecutionContext jobContext;
 
 	public JobTimeoutThread(JobExecutionContext jobContext) {
 		this.jobContext = jobContext;
 	}
 
 	@Override
 	public void run() {
 		try {
 			System.out.println("starting job timeout thread");
 			sleep(5000);
 			System.out.println("thread timed out, interrupting job ...");
 			((InterruptableJob) this.jobContext.getJobInstance()).interrupt();
 		} catch (InterruptedException e) {
 			System.out.println("timeout thread interrupted");
 		} catch (UnableToInterruptJobException e) {
 			System.out.println("timeout thread failed to interrupt job");
 		}
 	}
 
 }
Anonymous



I fixed it like so ....


Code:
public class ExampleJob implements Job, InterruptableJob {
 
 
 	@Override
 	public void execute(JobExecutionContext jobContext) throws JobExecutionException {
 		JobDataMap dataMap = jobContext.getMergedJobDataMap();
 		System.out.println("starting job");
 		try {
 			System.out.println("mykey: " + dataMap.get("mykey"));
 			Thread.sleep(30000); // pretend like we are doing something
 		} catch (InterruptedException ie) {
 			System.out.println("job interrupted by a timeout");
 		}
 		System.out.println("ending job");
 	}
 
 	@Override
 	public void interrupt() throws UnableToInterruptJobException {
 		System.out.println("job interrupted");
 	}
 }
jhouse

seraphim
[Avatar]
Joined: 11/06/2009 15:29:56
Messages: 1703
Offline

As an FYI,

A JobFactory impl that reflectively sets properties on the Job (based upon JobDataMap contents) CAN be used by setting the job factory in your quartz.properties to "org.quartz.simpl.PropertySettingJobFactory".

Code:
 org.quartz.scheduler.jobFactory.class = org.quartz.simpl.PropertySettingJobFactory
 
 
Forum Index -> Quartz
Go to:   
Powered by JForum 2.1.7 © JForum Team