Manas Saxena
Manas Saxena

Reputation: 2365

Adding job parameter in before job doesnt work spring batch

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

Answers (1)

Mahmoud Ben Hassine
Mahmoud Ben Hassine

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

Related Questions