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 !
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]} .
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.
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.
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With