2014-02-17 4 views
1

스프링 회분 문서 (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 등을 사용하여 ...)

답변

1

어쩌면. SB의 프레임 워크 인터페이스를 사용하여 "프로그래밍 방식으로"플로우 결정기와 비슷한 것을 작성하려는 의도가있는 경우 내장 된 구현이 있으며 대부분의 사용 사례만으로 충분합니다.

XML 설정과 반대로 사용자는 익숙한 사용자라면 JavaConfig 주석을 사용할 수 있습니다. 개인적으로 나는 XML 정의를 선호하지만 그것은 개인적인 의견 일뿐입니다.

1

설명서에 언급 된 것처럼 우리는 단계의 종료 상태에 따라 흐름을 분기 할 수만 있습니다. 맞춤 이탈 상태 (일괄 처리 상태에서 자동으로 매핑 된 상태와 다름)를보고 할 수 있으려면 StepExecutionListener에 대해 afterStep 메소드를 제공해야합니다.

한다고 가정 우리는이 초기 단계 step1 (A Tasklet 클래스 Step1의 인스턴스), 우리는 다음을 수행 할 :

  • step1 경우 (실행시 예외를 던져 예) 실패 후 전체 직업은 FAILED으로 간주되어야합니다.
  • 이 종료 상태가 COMPLETED-WITH-A으로 완료되면 step2a 단계로 분기하여이 특정 사례를 처리했다고 가정합니다.
  • 우리는 작업의 주요 트럭에 머물러서 step2 단계로 계속 진행합니다. 마지막으로

    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 
    } 
    

    우리 JobFactory 구현의 createJob 방법 내부의 작업 흐름을 구축 :

  • 이제

, (또한 StepExecutionListener를 구현) Step1 클래스 내부에 afterStep 방법을 제공

@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(); 
}