How to install JobParameters in spring package using spring-boot

I followed the tutorial at http://spring.io/guides/gs/batch-processing/ , but it describes the task without configurable parameters. I use Maven to create my project.

I am migrating an existing job that I defined in XML and would like to pass the jobParameters command using the command.

I tried the following:

@Configuration @EnableBatchProcessing public class MyBatchConfiguration { // other beans ommited @Bean public Resource destFile(@Value("#{jobParameters[dest]}") String dest) { return new FileSystemResource(dest); } } 

Then I will compile my project using:

 mvn clean package 

Then I try to run the program as follows:

 java my-jarfile.jar dest=/tmp/foo 

And I get an exception:

 [...] Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' 

Thanks!

+6
source share
2 answers

I managed to get this working by simply annotating my bean as follows:

 @Bean @StepScope public Resource destFile(@Value("#{jobParameters[dest]}") String dest) { return new FileSystemResource(dest); } 
+4
source

Parse job parameters from the command line, and then create and write JobParameters.

 public JobParameters getJobParameters() { JobParametersBuilder jobParametersBuilder = new JobParametersBuilder(); jobParametersBuilder.addString("dest", <dest_from_cmd_line); jobParametersBuilder.addDate("date", <date_from_cmd_line>); return jobParametersBuilder.toJobParameters(); } 

Submit them to your work through JobLauncher -

 JobLauncher jobLauncher = context.getBean(JobLauncher.class); JobExecution jobExecution = jobLauncher.run(job, jobParameters); 

Now you can access them using the code, for example -

 @Bean @StepScope public Resource destFile(@Value("#{jobParameters[dest]}") String dest) { return new FileSystemResource(dest); } 

Or in the @Configuration class, which configures Spring batch artifacts of the type - ItemReader, ItemWriter, etc.

 @Bean @StepScope public JdbcCursorItemReader<MyPojo> reader(@Value("#{jobParameters}") Map jobParameters) { return new MyReaderHelper.getReader(jobParameters); } 
+8
source

All Articles