java Spring Batch在步骤内访问作业参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32465142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Batch accessing job parameter inside step
提问by alexanoid
I have a following Spring Batch Job config:
我有以下 Spring Batch 作业配置:
@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();
}
}
I'm starting the job with a following code:
我用以下代码开始工作:
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();
}
How to access ccReportId
parameter from job steps ?
如何ccReportId
从作业步骤访问参数?
回答by luboskrnac
Tasklet.execute()
method takes parameter ChunkContext
, where Spring Batch injects all the metadata. So you just need to dig into job parameters via these metadata structures:
Tasklet.execute()
方法采用参数ChunkContext
,其中 Spring Batch 注入所有元数据。因此,您只需要通过这些元数据结构深入了解作业参数:
chunkContext.getStepContext().getStepExecution()
.getJobParameters().getString("ccReportId");
or other option is to access job parameters map this way:
或其他选项是以这种方式访问作业参数映射:
chunkContext.getStepContext().getJobParameters().get("ccReportId");
but this gives you Object
and you need to cast it to string.
但这给了您Object
,您需要将其转换为字符串。