I am testing out Quartz to schedule a job. However the job contains 3 non-serializable parameters.
I have created a sample application below indicating how I am implementing the functionality. Does anyone know how I can use custom objects as parameters using Quartz?
Below is the trigger which schedules the job, I have commented the area which is giving me issues.
public class Trigger {
public void run() throws Exception {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
Date startTime = DateBuilder.nextGivenSecondDate(null, 15);
JobDetail job = newJob(SimpleJob.class)
.withIdentity("job6", "group1")
.build();
SimpleTrigger trigger = newTrigger()
.withIdentity("trigger6", "group1")
.startAt(startTime)
.withSchedule(simpleSchedule()
.withIntervalInSeconds(60)
.repeatForever())
.build();
Date ft = sched.scheduleJob(job, trigger);
TestObject testObject = new TestObject();
// This is the part giving trouble!
job.getJobDataMap().put(SimpleJob.test,testObject);
sched.start();
}
}
Here is the job I am looking to schedule.
public class SimpleJob implements Job {
public static final TestObject test = null;
public SimpleJob() {
}
public void execute(JobExecutionContext context) throws JobExecutionException {
test.saySomething();
}
}
And finally, the TestObject class.
public class TestObject {
public TestObject() {
}
public void saySomething() {
System.out.println("Test Object initialized");
}
}
Please notice, I am only looking for a way to get Quartz to allow non-serializable objects to be used as a paramater (please do not comment on the actual task or job that is being carried out above)
I have also tried implementing the Serializable interface for the TestObject aswell, and no joy.
Any help would be greatly appreciated. Thanks you.
Implement your own JobFactory. It will customize the underlying Job injecting any properties you need.
class MyJobFactory extends SimpleJobFactory {
@Override
public Job newJob(TriggerFiredBundle bundle, Scheduler Scheduler) throws SchedulerException {
SimpleJob job = (SimpleJob) super.newJob(bundle, Scheduler);
job.setTestObject(testObject);
return job;
}
}
You still need to use JobDetails to inform the class of your job, and you need to modify the scheduler to use your factory.
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.setJobFactory(new MyJobFactory());
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
来源:https://stackoverflow.com/questions/11507850/custom-object-parameters-within-jobs-using-quartz-scheduler