Reputation: 51
I have a cron job in quartz (spring boot application). I have created beans for scheduler, trigger, cron job , and all other beans. The purpose of job is to run based on specified cron expression. But for each fresh execution of the job, I want to pass some data specific to that job, to the job detail object. But currently, when i try to pass such data in JobDetailFactory bean, it is only created once(for the first time), and for all further executions of the job, same stale data is being passed. I want to pass fresh new data each time job is executed. How can i achieve that?
Upvotes: 1
Views: 1209
Reputation: 1880
This is what the job data map can be used for. Please check JobExecutionContext.getMergedJobDataMap() method and the JobDataMap API.
If you want to modify the job data map parameters programmatically at runtime, you have two options:
Obtain the Scheduler reference, e.g. in JobExecutionContext.getScheduler() method and modify the desired job data map parameters of any job. Unfortunately, the Quartz API provides no method to update a job detail so you have to delete a job detail and add a new one if you wish to modify an existing job.
Let your job(s) implement the StatefulJob interface. This makes Quartz automatically persist the job data map when the job execution finishes. This can be used, for example, to resume processing of data from a specific point after the previous job execution finished at that point etc.
Upvotes: 0
Reputation: 12215
Maybe you could use some kind of callback? So something like this in your Job
:
@Resource
private MyJobJobdataProvider dataProvider;
public void execute(JobExecutionContext context) throws JobExecutionException {
JobData data = dataProvider.getData();
/// configure job with the data before actual exection ...
}
where MyJobJobdataProvider
would be any component that provides this data:
@Component
public MyJobJobdataProvider {
public Data getData() {
//...
}
public void setData(Data data) {
//...
}
}
So instead trying to set new data let the job query for it before each execution.
Upvotes: 1