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.