本文介紹了步驟內(nèi)彈簧批量取數(shù)作業(yè)參數(shù)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我有以下Spring批處理作業(yè)配置:
@Configuration
@EnableBatchProcessing
public class JobConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.flow(stepA()).on("FAILED").to(stepC())
.from(stepA()).on("*").to(stepB()).next(stepC())
.end().build();
}
@Bean
public Step stepA() {
return stepBuilderFactory.get("stepA").tasklet(new RandomFailTasket("stepA")).build();
}
@Bean
public Step stepB() {
return stepBuilderFactory.get("stepB").tasklet(new PrintTextTasklet("stepB")).build();
}
@Bean
public Step stepC() {
return stepBuilderFactory.get("stepC").tasklet(new PrintTextTasklet("stepC")).build();
}
}
我使用以下代碼開始作業(yè):
try {
Map<String,JobParameter> parameters = new HashMap<>();
JobParameter ccReportIdParameter = new JobParameter("03061980");
parameters.put("ccReportId", ccReportIdParameter);
jobLauncher.run(job, new JobParameters(parameters));
} catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException
| JobParametersInvalidException e) {
e.printStackTrace();
}
如何從作業(yè)步驟訪問ccReportId
參數(shù)?
推薦答案
Tasklet.execute()
方法帶參數(shù)ChunkContext
,Spring Batch注入所有元數(shù)據(jù)。因此您只需通過以下元數(shù)據(jù)結(jié)構(gòu)挖洞傳入作業(yè)參數(shù):
chunkContext.getStepContext().getStepExecution()
.getJobParameters().getString("ccReportId");
或其他選項是通過以下方式訪問作業(yè)參數(shù)映射:
chunkContext.getStepContext().getJobParameters().get("ccReportId");
但這會給您Object
,您需要將其轉(zhuǎn)換為字符串。
這篇關(guān)于步驟內(nèi)彈簧批量取數(shù)作業(yè)參數(shù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,