目錄
package com.springbatch._08job_param;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* SpringBatchDemo項目啓動類
*
* @Package: PACKAGE_NAME
* @ClassName: com.springbatch._01helloworld.SpringBatchConfig
* @author: zq
* @since: 2020/5/22 20:31
* @version: 1.0
* @Copyright: 2020 zq. All rights reserved.
*/
@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchConfig {
public static void main(String[] args) {
SpringApplication.run(SpringBatchConfig.class, args);
}
}
package com.springbatch._08job_param;
import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Map;
@Configuration
public class ParamJobConfig implements StepExecutionListener {
//創建 創建job的對象
@Autowired
private JobBuilderFactory jobBuilderFactory;
//創建 創建step的對象
@Autowired
private StepBuilderFactory stepBuilderFactory;
Map<String, JobParameter> parameterMap;
@Bean
public Job paramJob() {
return jobBuilderFactory.get("paramJob-01")
.start(paramJobStep())
.build();
}
/**
* 使用監聽器獲取參數
*
* @param
* @throws
* @Title: <p>paramJobStep</p>
* @return: org.springframework.batch.core.Step
* @author: zq
* @Date: 2020/6/21 20:42
* @version: 1.0
* @Copyright: zq. All rights reserved.
*/
@Bean
public Step paramJobStep() {
return stepBuilderFactory.get("paramJobStep")
.listener(this)
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
System.out.println(parameterMap.get("info"));
return RepeatStatus.FINISHED;
}
})
.build();
}
@Override
public void beforeStep(StepExecution stepExecution) {
parameterMap = stepExecution.getJobParameters().getParameters();
}
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
return null;
}
}