【问题标题】:How to return Custom RepeatStatus from spring Batch step如何从春季批处理步骤返回自定义重复状态
【发布时间】:2017-11-04 07:13:16
【问题描述】:

步骤类:GenerateReferenceNumber

package com.npst.imps.action;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import com.npst.imps.utils.TransactionResponseData;
public class GenerateReferenceNumber implements Tasklet {
    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {    
    double rrn= Math.random();
    chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().put("rrn", rrn);   
    double tid= (double) chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().get("tid");       
    chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().put("trnsactionstatus", "RRN generated for Tid::"+tid+" is "+rrn);
    TransactionResponseData transactionResponseData =(TransactionResponseData) chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().get("transactionResponseData");
    transactionResponseData.setRrn(rrn+"");
    chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().put("transactionResponseData", transactionResponseData); 

          return RepeatStatus.FINISHED; 
    }

}

而不是 Repeatstatus.FINISHED ,我怎样才能返回我自己定义的状态,并根据它们决定下一步。自定义状态,如成功、失败、部分等。

批处理作业.xml

<beans:beans xmlns="http://www.springframework.org/schema/batch"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/batch
           http://www.springframework.org/schema/batch/spring-batch-3.0.xsd">
       <job id="MBSFT">
         <step id="PrepareTid" allow-start-if-complete="true" next="PrepareRRN">
            <tasklet ref="PrepareTransactionId" />
        </step>

        <step id="PrepareRRN" allow-start-if-complete="true">
            <tasklet ref="GenerateReferenceNumber" />
                <next on="COMPLETED" to="IdentifyImpsService" />
        </step>

        <step id="IdentifyImpsService" allow-start-if-complete="true">
            <tasklet ref="IdentifyIMPSRequestType" />
            <next on="COMPLETED" to="FetchNBIN" />
        </step>

        <step id="FetchNBIN" allow-start-if-complete="true">
            <tasklet ref="FetchNBINFromIFSC" />
        </step>
    </job>
</beans:beans>

【问题讨论】:

    标签: java spring spring-boot spring-batch


    【解决方案1】:

    我想这是不可能的。
    您可以将自定义返回状态放入 StepExecution,使用 ExecutionContextPromotionListener 将属性从步骤移动到作业执行上下文,然后使用 JobExecutionDecider 重定向流程。

    【讨论】:

      【解决方案2】:

      您可以轻松地从StepExecutionListener 发送自定义状态。 ExitStatus 是简单的枚举。您可以将任何字符串作为退出状态传递。

      根据现有状态决定批处理流程。

      以下是示例 java 配置代码。

        //Return status from step listener
            @Override
      public ExitStatus afterStep(StepExecution stepExecution) {
                  if(condition1)
                  return new ExitStatus("CUSTOM_STATUS");     
                  if(condition2)
                  return new ExitStatus("CUSTOM_STATUS_XYZ");     
                  if(condition3)
                  return new ExitStatus.COMPLETED
      }
      
      
      @Bean
          public Job myJob(JobBuilderFactory jobs) throws Exception {
              return jobs.get("myJob")
                      .start(Step1())
                      .next(Step2()).on("CUSTOM_STATUS").to(step3())
                      .next(Step2()).on("CUSTOM_STATUS_XYZ").to(step4())
                      .next(Step2())).on("COMPLETED").to(step4())             
                      .build()
                      .listener(listener)
                      .preventRestart()
                      .build();
      
          }
      

      【讨论】:

      • @NaveenKumarMishra 如果对您有帮助,请您接受答案吗?以便其他有相同问题的人可以使用相同的解决方案:)
      • 但是返回自定义状态的原因是什么?谁处理这个状态?
      猜你喜欢
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      相关资源
      最近更新 更多