[英]Spring Batch (java-config) executing step after a jobExeuctionDecider
[英]Controlling Spring-Batch Step Flow in a java-config manner
根据Spring-Batch文档( http://docs.spring.io/spring-batch/2.2.x/reference/html/configureStep.html#controllingStepFlow ),在xml配置文件中控制步骤流非常简单:
例如,我可以编写以下作业配置:
<job id="myJob">
<step id="step1">
<fail on="CUSTOM_EXIT_STATUS"/>
<next on="*" to="step2"/>
</step>
<step id="step2">
<end on="1ST_EXIT_STATUS"/>
<next on="2ND_EXIT_STATUS" to="step10"/>
<next on="*" to="step20"/>
</step>
<step id="step10" next="step11" />
<step id="step11" />
<step id="step20" next="step21" />
<step id="step21" next="step22" />
<step id="step22" />
</job>
有没有一种简单的方法以java-config方式定义这样的作业配置? (使用JobBuilderFactory等)
正如文档中还提到的那样,我们只能基于步骤的退出状态来分支流程。 为了能够报告自定义的退出状态(从批次状态自动映射的一个可能不同),我们必须提供一个afterStep
一种方法StepExecutionListener
。
假设我们有一个初始步骤step1
( Tasklet
类Step1
一个实例),并且我们想要执行以下操作:
step1
失败(例如,引发运行时异常),则应将整个作业视为FAILED
。 step1
以COMPLETED-WITH-A
的退出状态完成,则我们想转到某个步骤step2a
,该步骤应该可以处理此特定情况。 step2
。 现在,在Step1
类中提供afterStep
方法(也实现了StepExecutionListener
):
private static class Step1 implements Tasklet, StepExecutionListener
{
@Override
public ExitStatus afterStep(StepExecution stepExecution)
{
logger.info("*after-step1* step-execution={}", stepExecution.toString());
// Report a different exit-status on a random manner (just a demo!).
// Some of these exit statuses (COMPLETED-WITH-A) are Step1-specific
// and are used to base a conditional flow on them.
ExitStatus exitStatus = stepExecution.getExitStatus();
if (!"FAILED".equals(exitStatus.getExitCode())) {
double r = Math.random();
if (r < 0.50)
exitStatus = null; // i.e. COMPLETED
else
exitStatus = new ExitStatus(
"COMPLETED-WITH-A",
"Completed with some special condition A");
}
logger.info("*after-step1* reporting exit-status of {}", exitStatus);
return exitStatus;
}
// .... other methods of Step1
}
最后,在JobFactory
实现的createJob
方法中构建作业流程:
@Override
public Job createJob()
{
// Assume some factories returning instances of our Tasklets
Step step1 = step1();
Step step2a = step2a();
Step step2 = step2();
JobBuilder jobBuilder = jobBuilderFactory.get(JOB_NAME)
.incrementer(new RunIdIncrementer())
.listener(listener); // a job-level listener
// Build job flow
return jobBuilder
.start(step1)
.on("FAILED").fail()
.from(step1)
.on("COMPLETED-WITH-A").to(step2a)
.from(step1)
.next(step2)
.end()
.build();
}
也许。 如果您打算以编程方式(类似于SB的框架接口)编写类似于流决策器的内容,那么它是内置的实现,足以满足大多数使用情况。
与XML配置相反,如果您熟悉它们,则可以使用JavaConfig批注。 我个人更喜欢XML定义,但这只是个人观点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.