How do I set JobParameters in spring batch with spring-boot
Asked Answered
B

3

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 !

Bunsen answered 4/2, 2014 at 16:15 Comment(2)
How do you set the parameters in the first place? i.e. how does jobParameters[dest] get filled?Prelacy
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/…Bunsen
B
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);
}
Bunsen answered 4/2, 2014 at 17:1 Comment(0)
P
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);
}
Phytology answered 1/9, 2016 at 18:5 Comment(2)
Thanks for nice suggestions. Please let me know how we can do the same with DefaultBatchConfigurer?Urbano
But what if I don't have a custom job launcher? I just have a Job and spring starts it for meTriiodomethane
B
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);
}
Bunsen answered 4/2, 2014 at 17:1 Comment(0)
H
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;
}
Hath answered 19/7, 2023 at 11:50 Comment(1)
this work properlyHath

© 2022 - 2024 — McMap. All rights reserved.