2017-11-04 3 views
1

단계 클래스 정의 RepeatStatus을 반환하는 방법 대신 Repeatstatus.FINISHED의 스프링 배치 단계에서

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; 
    } 

} 

GenerateReferenceNumber

, 난 내 자신의 정의 상태를 반환하고 다음 단계가 결정됩니다이를 기반으로 할 수있는 방법을. 성공과 같은 사용자 정의 상태, 실패, 부분 등

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> 

답변

0

StepExecutionListener에서 사용자 지정 상태를 쉽게 보낼 수 있습니다. ExitStatus는 간단한 enum입니다. 모든 문자열을 종료 상태로 전달할 수 있습니다.

존재 상태에 따라 배치 흐름을 결정하십시오.

다음은 샘플 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(); 

    } 
+0

예 실현은 동일합니다 –

+0

@NaveenKumarMishra 도움이된다면 답을 받아 들여 주시겠습니까? 같은 문제가있는 다른 사람들도 같은 솔루션을 사용할 수 있습니다 :) – Niraj

1

나는 이것이 가능하지 않은 가정합니다.
사용자 지정 반환 상태를 StepExecution에 넣고 ExecutionContextPromotionListener을 사용하여 속성을 단계별 작업 실행 컨텍스트로 이동하고 JobExecutionDecider을 사용하여 흐름을 리디렉션 할 수 있습니다.