1

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 などを使用して...)

4

2 に答える 2

4

ドキュメントにも記載されているように、ステップの終了ステータスに基づいてフローを分岐することしかできません。カスタムの終了ステータス (バッチステータスから自動的にマップされたものとは異なる可能性があります) を報告できるようにするにafterStepは、StepExecutionListener.

最初のステップstep1( TaskletclassのインスタンスStep1) があり、次のことを行いたいとします。

  • 失敗した場合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();
}
于 2017-09-14T00:51:51.537 に答える
1

多分。フロー ディサイダーに似たものを「プログラム的に」(つまり、SB のフレームワーク インターフェイスを使用して) 記述したい場合は、組み込みの実装があり、ほとんどのユース ケースで十分です。

XML 構成とは反対に、JavaConfigアノテーションに精通していれば、それを使用できます。個人的にはXML定義の方が好きですが、あくまで個人的な意見です。

于 2014-02-18T07:20:18.343 に答える