Spring Batch Commandline Arugments Jobparameter

Command Line argument with main

Previous post, I explained how to use command line arguments Spring Batch Commandline Arugments.
In this case, we use CommandLineRunner and XML. This is legacy style.
We want to use our own main method and JavaConfig.
And pass command line arguments to JobParameter.

Preparation

To use main method and JavaConfig for this goal, we need to do following

  • application.yml
  • JavaConfig
  • main

application.yml(src/main/

spring:
  batch:
    job:
      enabled: false

To do this, we can prevent batch auto run from config and main.

JavaConfig

This includes job definition

@Configuration
@EnableBatchProcessing
@EnableAutoConfiguration
public class BatchConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
            //.listener(steplitener())
            .tasklet(new Tasklet(){
                public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
                    // Get Job parameter
                    JobParameters parameters = chunkContext.getStepContext().getStepExecution().getJobExecution().getJobParameters();
                    System.out.println("Step1:" + parameters.getString("date"));
                    return RepeatStatus.FINISHED;
            }
        }).build();
    }    

    @Bean
    public Job job(Step step1) throws Exception {
        return jobBuilderFactory.get("job")
                .incrementer(new RunIdIncrementer())
                .start(step1)
                .build();
    }
}

Main.java

@SpringBootApplication
public class Main {

    public static void main(String... args) throws Exception {
        if (args.length != 0 && args.length % 2 == 1) {
            // First parameter is jobname
            String jobName = args[0];
            Map<String, String> params = new HashMap<String, String>();
            for (int i=1; i < args.length; i+=2) {
                String key = args&#91;i&#93;;
                String value = args&#91;i+1&#93;;
                params.put(key.replace("-", ""), value);
            }
            // TODO need to consider double run prevention
            params.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));

            // Start
            ConfigurableApplicationContext context = SpringApplication.run(Main.class, args);
            JobLauncher jobLauncher = context.getBean("jobLauncher", JobLauncher.class);
            Properties property = new Properties();
            for (Map.Entry<String, String> set : params.entrySet()) {
                property.put(set.getKey(), set.getValue());
            }
            JobParameters jobParameters = new DefaultJobParametersConverter().getJobParameters(property);
            Job job = (Job)context.getBean(jobName, Job.class);
            JobExecution jobExecution = jobLauncher.run(job, jobParameters);
        }
        else {
            // Error
            throw new Exception("Need at least one argument jobName and parameter should be --key value");
        }
    }
}

Set up configuration using SpringApplication.run, but do nothing because disable batch run.
After getting context we prepare our own job runner and run job

Run

Please build jar file and run with java command
Example

java -jar build/libs/BatchPractice-0.0.1-SNAPSHOT.jar job --type daily --date 20161201

job is jobname, –key value are arguments