Passing Job Parameters using JobParametersBuilder

Passing Job Parameters

In this post under Spring Batch I will explain how to pass parameters to a job.

We will take the help of org.springframework.batch.core.JobParametersBuilder class which follows Builder design pattern.

We will use JobParametersBuilder class to build the job parameters.

Then we pass the job parameters along with the job (to be executed) as parameters to JobLauncher interface’s run method. Below is the signature of run method.

    JobExecution run(Job job, JobParameters jobParameters)
    throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException

Below is the complete main method

Main Code

1  package package17;
3  import java.util.Date;
5  import org.springframework.batch.core.Job;
6  import org.springframework.batch.core.JobExecution;
7  import org.springframework.batch.core.JobParameters;
8  import org.springframework.batch.core.JobParametersBuilder;
9  import org.springframework.batch.core.JobParametersInvalidException;
10 import org.springframework.batch.core.launch.JobLauncher;
11 import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
12 import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
13 import org.springframework.batch.core.repository.JobRestartException;
14 import org.springframework.context.ApplicationContext;
15 import;
17 public class Example17 {
18  public static void main(String[] args) throws JobParametersInvalidException, JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException,  JobRestartException {
19      ApplicationContext context = new ClassPathXmlApplicationContext("package17\\job.xml");
20      JobLauncher jobLauncher = (JobLauncher)context.getBean("jobLauncher");
21      JobParameters jobParameters = new JobParametersBuilder().addDate("date", new Date())
22                                      .addString("jobName", "job1").toJobParameters();
23      Job job = (Job)context.getBean("importProductsJob");
25      JobExecution jobExecution =, jobParameters);
27      System.out.println(jobExecution.getJobParameters().getDate("date"));
28      System.out.println(jobExecution.getJobParameters().getString("jobName"));
29  }
30 }

At line 20, we get the instance of JobLauncher from the application context.

At line 21, we create an instance of JobParameters using JobParametersBuilder. We add two parameters current date and job name which is in this instance is “job1”.

At line 23, we get the instance of job from the application context.

At line 25, we execute the job by calling the JobLauncher’s run method, passing the job (to be executed) and job parameters as the run method’s arguments.

At line 27 and 28, we print the parameters by retrieving them from JobExecution instance.


Nov 10, 2018 7:05:43 PM prepareRefresh
INFO: Refreshing startup date [Sat Nov 10 19:05:43 IST 2018]; root of context hierarchy
Nov 10, 2018 7:05:43 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [package17/job.xml]
Nov 10, 2018 7:05:43 PM afterPropertiesSet
INFO: No TaskExecutor has been set, defaulting to synchronous executor.
Nov 10, 2018 7:05:43 PM run
INFO: Job: [FlowJob: [name=importProductsJob]] launched with the following parameters: [{date=1541856943666, jobName=job1}]
Nov 10, 2018 7:05:43 PM org.springframework.batch.core.job.SimpleStepHandler handleStep
INFO: Executing step: [readWriteProducts]
Nov 10, 2018 7:05:43 PM run
INFO: Job: [FlowJob: [name=importProductsJob]] completed with the following parameters: [{date=1541856943666, jobName=job1}] and the following status: [COMPLETED]
Sat Nov 10 19:05:43 IST 2018

Leave a Reply