2

I'm trying to launch a parameterized Spring Batch job through Spring Cloud Task. My goal is to launch it through the command line and have the application shut down after the job is executed.

There is an example in the samples repo for how to achieve this, but it's a very simple job with no parameters. I haven't found the answer to my question in the reference documentation.

--- EDIT : following Michael's question, I set up a simplified example, and it works out of the box. I'll investigate my real example and in the meantime post the answer to my initial question here ---

Below is the simplified example:

@Configuration
@EnableTask
@EnableBatchProcessing
public class BatchConfig {

    private final static Logger log = LoggerFactory.getLogger(BatchConfig.class.getName());

    @Autowired
    StepBuilderFactory steps;

    @Bean
    @StepScope
    Tasklet helloTasklet(@Value("#{jobParameters['name']}") String name) {
        return new Tasklet() {
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) {
                log.info("Hello {}", name);
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Step step1(Tasklet helloTasklet) {
        return steps.get("step1").tasklet(helloTasklet).build();
    }

    @Bean
    public Job helloJob(JobBuilderFactory jobs, Step step1) {
        return jobs.get("helloJob")
            .incrementer(new RunIdIncrementer())
            .start(step1)
            .build();
    }
}

In pom.xml, I have the following dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-task-batch</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.200</version>
</dependency>

I compile the program and run it as an executable jar. My initial question is that I didn't know how to pass-in the job parameters. See my answer for how it works.

Philippe
  • 6,703
  • 3
  • 30
  • 50
  • How are you attempting to launch it? – Michael Minella Jul 30 '20 at 15:58
  • I created a springboot app with the cloud-task starter, setup a batch job with a parameter, then tried to run the executable jar. It starts the job, but it crashes as I don't know how to provide the job parameters – Philippe Jul 30 '20 at 16:03
  • Please provide the exact command you are running and the configuration for the parameters you are attempting to use. – Michael Minella Jul 30 '20 at 20:43

1 Answers1

0

Following my simplified example, I tried out the following, and it worked:

java -jar myprogram.jar name=Bob

This results in the following log statement:

2020-07-30 22:42:21.927  INFO 3269 --- [           main] com.example.demo.BatchConfig             : Hello Bob
Philippe
  • 6,703
  • 3
  • 30
  • 50