控制弹簧批步骤流量

根据弹簧批处理文件(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等...)

回答:

也许。如果你的意图是以“编程方式”编写类似于流程决策的东西(使用SB的框架接口,我的意思是)有内置的实现,并且对于大多数用例来说已经足够了。

与XML配置相反,如果您熟悉XML配置,则可以使用JavaConfig批注;我个人更喜欢XML定义,但这只是个人观点。

回答:

正如文档中提到的,我们只能根据一个步骤的退出状态来分支流程。为了能够报告自定义退出状态(可能与自动从批处理状态映射的退出状态不同),我们必须为StepExecutionListener提供afterStep方法。

假设我们有一个初始步骤step1(一TaskletStep1的实例),我们要做到以下几点:

  • 如果step1失败(例如通过抛出运行时异常),那么整个工作应被视为FAILED
  • 如果step1以退出状态COMPLETED-WITH-A完成,那么我们想要跳转到某个步骤step2a,该步骤可以处理这个特定情况。
  • 否则,我们停留在工作的主要卡车上并继续执行步骤step2

现在,提供afterStep方法中Step1类(也实现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

}

最后,建立内部createJob方法我们JobFactory实现作业流程:

@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();

}

以上是 控制弹簧批步骤流量 的全部内容, 来源链接: utcz.com/qa/266485.html

回到顶部