26

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 !

Philippe
  • 6,703
  • 3
  • 30
  • 50
  • How do you set the parameters in the first place? i.e. how does `jobParameters[dest]` get filled? – Kees de Kooter Dec 11 '15 at 09:24
  • 1
    Well, just create the JobParameters in the main class of your executable jar file using something like JobParametersBuilder().addString("dest", args[0]).toJobParameters(), then pass the resulting JobParameters to the JobLauncher. See http://docs.spring.io/spring-batch/reference/htmlsingle/#domainJobLauncher – Philippe Dec 11 '15 at 14:12

3 Answers3

21

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);
}
Ashok
  • 211
  • 2
  • 4
15

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);
}
Philippe
  • 6,703
  • 3
  • 30
  • 50
0
    @Bean
@StepScope
public RepositoryItemReader<ProfileView> profile_ManagerItemReader(@Value("#{jobParameters[start_date]}") String start_date) {
    log.info("profile_manager_reader call");
    RepositoryItemReader<ProfileView> reader = new RepositoryItemReader<>();
    reader.setRepository(profileRepositorySlave);
    reader.setMethodName("getProfileFromBk");
    var listArgs = new ArrayList<>();
    listArgs.add(LocalDate.parse(start_date));
    reader.setArguments(listArgs);
    reader.setPageSize(500);
    Map<String, Sort.Direction> sort = new HashMap<String, Sort.Direction>();
    sort.put("id", Sort.Direction.ASC);
    reader.setSort(sort);
    return reader;
}