Reputation: 2365
I have a spring batch job wherein I need to set job parameter in beforeStep I am using below code for the same:
@Override
public void beforeJob(JobExecution jobExecution) {
String pid = fetchPid();
jobExecution
.getJobParameters()
.getParameters()
.put("pid", new JobParameter(pid));
}
When I run above code and debug , I see that pid
is not present in the jobparameters . What could be wrong here?
Upvotes: 0
Views: 1794
Reputation: 31600
What could be wrong here?
The JobParameters#getParameters
returns a unmodifiable map of parameters (See Javadoc). So adding the pid
key as you did it wont work.
I need to set job parameter in beforeStep
I guess you mean in beforeJob
and not beforeStep
since your code shows the beforeJob
method. Using the JobExecutionListener
to add parameters is too late because the parameters are used to identify the instance, and at the time of invoking beforeJob
, the execution has been already launched with the given parameters. You need to prepare the parameters upfront then use them to launch the job using jobLauncher.run(job, jobParameters)
.
Upvotes: 1