当前位置: 首页 > 知识库问答 >
问题:

如何使用决策器终止Spring批次分割流中的步骤

汪高岑
2023-03-14

我在Spring Batch中发现了以下设计缺陷。

  1. 步骤必须具有Next属性,除非它是拆分流的最后一步或最后一步。
  2. 决定器块必须处理决定器返回的所有情况。

因此,在分割流中,最后一步不会有下一个属性,如果有决策者保护它,那么它必须有下一个属性。所以它不应该有这个属性,但它也需要它。第22条。

示例:

<!-- Process parallel steps -->
<split id="split01">
    <flow>
        <step id="step1" next="step02">
            <!-- Do something -->
        </step>
        <step id="step02">
            <!-- Do something else -->
        </step>
    </flow>
    <flow>
        <step id="step03">
            <!-- Do something -->
        </step>

        <!-- Only run under specific conditions -->
        <decision id="decideToRunStep04" decider="isStepNeededDecider" >
            <next on="RUN" to="step04"/>
            <!-- Other state is "SKIP" -->
        </decision>
        <step id="step04">
            <!-- Conditionally do something-->
        </step>
    </flow>
</split>

<step id="step05" >
    <!-- Some more stuff -->
</step>

这似乎是Spring的人会想到的,所以很好奇,实现这一点的正确、非黑客方式是什么。谢谢

共有3个答案

呼延原
2023-03-14

在XML中

<batch:decision id="customerDecision" decider="customerDecider">
            <batch:next on="FILE_FAILURE" to="fileFailureStep" />
            <batch:next on="FILE_GENERATION" to="loadData" /> 
</batch:decision>

在customerDecider类中

public class CustomerDecider implements JobExecutionDecider {    
@Override
public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecutionStatus) {
If(x)
    return new FlowExecutionStatus("FILE_FAILURE") ;
else
    return new FlowExecutionStatus("FILE_GENERATION") ;
}
}
满昊然
2023-03-14

Spring是城里最美的代码。也就是说:

<step id="step1" parent="s1">
    <end on="FAILED" />
    <next on="COMPLETED WITH SKIPS" to="errorPrint1" />
    <next on="*" to="step2" />
</step>

正如在http://docs.spring.io/spring-batch/reference/html/configureStep.html.

陈俊郎
2023-03-14

如果没有人回答这个问题,我将提供我正在使用的黑客。它不美,但Spring也不美。

创建一个无操作Tasklet,用于无操作步骤。

public class NoopTasklet implements Tasklet {
    @Override
    public RepeatStatus execute(final StepContribution contribution,
            final ChunkContext chunkContext) throws Exception {
        return RepeatStatus.FINISHED;
    }
}

将NOOP tasklet添加到原始示例中的决策块

<!-- Does nothing -->
<bean id="noopTasklet" class="com.foo.NoopTasklet" />

<!-- From example in question
<decision id="decideToRunStep04" decider="isStepNeededDecider" >
    <next on="RUN" to="step04"/>
    <next on="SKIP" to="noop01"/>
</decision>
<step id="step04">
    <!-- Conditionally do something-->
</step>
<step id="noop01">
    <!-- Does nothing in the SKIP case
    <tasklet ref="noopTasklet" />
</step>
 类似资料:
  • 我开始使用Spring批处理,我有一个关于何时使用步骤、决策器和块的问题。 考虑到以下输入: 对于每个

  • 那么,如何将作业配置为先运行单个步骤,然后并行运行多个步骤,然后运行最后一个步骤呢?

  • 我一直在玩流,然后我注意到当我执行以下操作时,它不会在控制台中产生输出: 我认为这是因为是一个非终止流方法,应该使用而不是来终止流并生成结果: 然而,是否有一种方法可以‘提前’终止流,使用一个自定义的终止方法(函数接口),它除了终止流之外什么都不做?..有没有一个适当的方法来利用Java现有的东西来做到这一点? 我知道我可以这样做: 但那感觉很浪费。

  • null 如果生成错误(文件不一致,文件不存在...),则不能执行 仅供参考,我使用的是没有XML配置的Spring批处理!只有注释:下面是我的作业配置类的样子:

  • 我想知道如何将spring反应应用于spring批次。我该怎么做。为什么我们要将spring Responsive应用于spring security?为什么我可以申请spring security,但我不能申请spring batch

  • 我需要根据工作步骤1中的某些条件来决定下一步调用哪个步骤。 请注意:在步骤1中,我使用的是纯tasklet方法。例子: 请帮助,我如何在示例tasklet中放入一些代码或进行一些配置以决定调用的下一步? 我已经调查过https://docs.spring.io/spring-batch/reference/html/configureStep.html