Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set JobParameters in spring batch with spring-boot

Tags:

I followed the guide at http://spring.io/guides/gs/batch-processing/ but it describes a job with no configurable parameters. I'm using Maven to build my project.

I'm porting an existing job that I have defined in XML and would like to pass-in the jobParameters through 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 compile my project using :

mvn clean package 

Then I try to launch the program like this :

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

And I get an exception saying :

[...] 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 !

like image 733
Philippe Avatar asked Feb 04 '14 16:02

Philippe


People also ask

How does Spring Batch read JobParameters?

JobParameters can be used for identification or even as reference data during the job run. They have reserved names, so to access them we can use Spring Expression Language. For example to access a property 'abc' on job parameters: we can access it using the syntax #{jobParameters[abc]} .

How do I run a specific job in Spring Batch?

Spring Batch auto configuration is enabled by adding @EnableBatchProcessing (from Spring Batch) somewhere in your context. By default it executes all Jobs in the application context on startup (see JobLauncherCommandLineRunner for details). You can narrow down to a specific job or jobs by specifying spring. batch.

How do I trigger a position in Spring Batch?

Spring Batch Scheduling: Spring Batch Jobs Scheduling You can configure Spring Batch Jobs in two different ways: Using the @EnableScheduling annotation. Creating a method annotated with @Scheduled and providing recurrence details with the job. Then add the job execution logic inside this method.


Video Answer


1 Answers

Parse in job parameters from the command line and then create and populate 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(); } 

Pass them to your job via JobLauncher -

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

Now you can access them using code like -

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

Or in a @Configuration class that is configuring Spring Batch Job artifacts like - ItemReader, ItemWriter, etc...

@Bean @StepScope public JdbcCursorItemReader<MyPojo> reader(@Value("#{jobParameters}") Map jobParameters) {     return new MyReaderHelper.getReader(jobParameters); } 
like image 119
Ashok Avatar answered Sep 28 '22 19:09

Ashok