I'm using spring mvc 4.2.5.RELEASE and quartz 2.2.1 quartz-jobs 2.2.1
I have a user interface when the user inputs the name and the cron expression of a quartz job, and then I create the job like this
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
schedFact.getScheduler().getContext().put("externalInstanceEstatus", myObject);
Scheduler sched = schedFact.getScheduler();
sched.start();
JobDetail job = null;
job = newJob(MyTask.class)
.withIdentity((String) String.valueOf(myUniqueId), "group1")
.build();
Trigger triggerCron = TriggerBuilder
.newTrigger()
.withIdentity(String.valueOf(myUniqueId), "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule(myObject.getExpresionCron()))
.build();
sched.scheduleJob(job, triggerCron);
Everytime that the user goes that interface he can create a job this works fine but when I turn off the server all of my Jobs are lost. I solved this by saving the Job name and cron expression in a database and I created a method that fetch all the records from that table and I re-create the Jobs like this
public void reCreateJobs() {
ArrayList<MyJob> listOfJobs = searchAllJobsInDB();
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler sched = schedFact.getScheduler();
//I loop the lsit and I delete all the jobs in the scheduler just in case
for (MyJob myJob : listOfJobs) {
for (String group : sched.getJobGroupNames()) {
for (JobKey jobKey : sched.getJobKeys((GroupMatcher<JobKey>) groupEquals(group))) {
if (jobKey.getName().equals(String.valueOf(myJob.getIdMyJob()))) {
sched.deleteJob(jobKey);
}
}
}
}
sched.start();
//THIS IS WHERE I CREATE ALL THE JOBS AGAIN
for (MyJob myJob : listOfJobs) {
JobDetail job = newJob(TareaImprimir.class)
.withIdentity((String) String.valueOf(myJob.getIdMyJob()), "group1")
.build();
JobDetail job = null;
Trigger triggerCron = TriggerBuilder
.newTrigger()
.withIdentity(String.valueOf(myJob.getIdMyJob()), "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule(myJob.getExpresionCron()))
.build();
sched.scheduleJob(job, triggerCron);
}
}
This also works fine I can create the Jobs again and they run fine,
My problem is that I need to pass a value to the Task Class and I don't know how to pass it, I know how to pass it when I create the job for the first time like this
schedFact.getScheduler().getContext().put("externalInstance", myObject);
and then I get it in my task Class like this
SchedulerContext schedulerContext = null;
try {
schedulerContext = context.getScheduler().getContext();
} catch (SchedulerException ex) {
ex.printStackTrace();
}
MyObject externalInstance
= (MyObject) schedulerContext.get("externalInstance");
My Problem is that I don't know how to do this when I'm recreating the Jobs in my method reCreateJobs()
where I get them from the DataBase
How can I set that externalInstance inside my method reCreateJobs()
for every Job or How can I pass that value to the MyTask class inside that loop
this is my Task Class
public class MyTask implements Job {
@Autowired
SomeDAO someDAO;
public void execute(JobExecutionContext context)
throws JobExecutionException {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
SchedulerContext schedulerContext = null;
try {
schedulerContext = context.getScheduler().getContext();
} catch (SchedulerException ex) {
Logger.getLogger(MyTask.class.getName()).log(Level.SEVERE, null, ex);
}
MyObject externalInstance
= (MyObject) schedulerContext.get("externalInstance");
someDAO.doSomething(externalInstance.getSomething());
}
}