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 !

2
  • How do you set the parameters in the first place? i.e. how does jobParameters[dest] get filled? Dec 11, 2015 at 9: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 docs.spring.io/spring-batch/reference/htmlsingle/…
    – Philippe
    Dec 11, 2015 at 14:12

3 Answers 3

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);
}
2
  • 1
    Thanks for nice suggestions. Please let me know how we can do the same with DefaultBatchConfigurer?
    – Jeff Cook
    Aug 25, 2018 at 15:06
  • 2
    But what if I don't have a custom job launcher? I just have a Job and spring starts it for me Aug 15, 2019 at 12:44
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);
}
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;
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.