如何从spring Batch步骤返回Custom RepeatStatus

时间:2017-11-04 07:13:17

标签: java spring spring-boot spring-batch

步骤类: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,我如何返回自己定义的状态,并根据它们确定下一步。自定义状态,如成功,失败,部分等。

batchjob.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>

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您可以轻松地从 <HTML> <HEAD> <TITLE> This Result.php </TITLE> <?php import_request_variables("p","f_"); ?> </HEAD> <body> <?php if($f_uname=="Ali" && $f_pass=="123") { echo ("Welcome <br/>\n"); } else { echo ("Invalid Username & Password!!"); } ?> </body> </HTML> 发送自定义状态。 ExitStatus是简单的枚举。您可以将任何字符串作为退出状态传递。

根据存在状态确定批次流程。

以下是示例java配置代码。

Thread.Sleep(1000);