@Configuration
@EnableBatchProcessing
public class ParameterDemo implements StepExecutionListener {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
//创建一个成员变量
private Map<String,JobParameter> parameters;
/**
* 创建job
* @return
*/
@Bean(value = "parameterDemoJob1")
public Job parameterDemoJob1(){
return jobBuilderFactory.get("parameterDemoJob1")
.start(parameterDemoStep1())
.build();
}
/**
* 创建step
* @return
*/
@Bean(value = "parameterDemoStep1")
public Step parameterDemoStep1() {
return stepBuilderFactory.get("parameterDemoStep1")
.listener(this)
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
System.out.println("info的值为:"+parameters.get("info"));
return RepeatStatus.FINISHED;
}
}).build();
}
/**
* 对step做监听,在监听之前给Job传递参数
* @param stepExecution
*/
@Override
public void beforeStep(StepExecution stepExecution) {
this.parameters = stepExecution.getJobParameters().getParameters();
}
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
return null;
}
}
网友评论