2017-02-17 1 views
0

두 작업을 차례로 실행하고 싶습니다. 예, 온라인으로 검색했지만 두 번째 작업을 첫 단계로 추가하는 방법이 있습니다. 하지만 다른 요구 사항이 있습니다. 첫 번째 일괄 처리 작업이 완료된 후 알림을 받고 두 번째 알림은이 알림을받은 후에 만 ​​실행됩니다. Spring 부트에서 두 개의 작업을 차례로 실행할 수도 있습니까? 도와주세요!첫 번째 작업 실행이 완료된 후 두 번째 일괄 작업 실행 봄 부팅

+0

[내 답변] (http://stackoverflow.com/questions/41364220/how-to-run-spring-batch-jobs-in-certain-order-spring-boot/41437552#41437552) 도움이 되셨습니까? –

답변

0

가장 쉬운 해결책은 FirstJob 완료 후 콜백에 JobExecutionListener을 추가하는 것입니다. 그리고 AfterJob 콜백에서 두 번째 작업을 시작하십시오. FirstJob의 executionContext를 얻었으므로 Job을 기반으로 작업 실행 전략을 적용 할 수 있습니다. 당신은 깔끔한 디자인이 될 수 메시징 서비스에 대한 이러한 접근 방식을 향상시킬 수 있습니다

<batch:job id="kpJob1" incrementer="jobIncrementer" 
     restartable="true"> 
     <batch:listeners> 
      <batch:listener> 
       <bean class="com.kp.job.KpJobListener"> 
        <constructor-arg name="job" ref="kpJob2" /> 
        <constructor-arg name="jobLauncher" ref="jobLauncher" /> 
       </bean> 
      </batch:listener> 
     </batch:listeners> 
     <batch:step id="kpJob1.step1" allow-start-if-complete="true"> 
      <batch:tasklet> 
       <bean class="com.kp.job.step.task.KpTasklet" /> 
      </batch:tasklet> 
     </batch:step> 
    </batch:job> 

public class KpJobListener implements JobExecutionListener { 

    private final static Logger LOG = LoggerFactory.getLogger(KpJobListener.class); 

    private Job job; 
    private JobLauncher jobLauncher; 

    public KpJobListener(final Job job, final JobLauncher jobLauncher) { 
     this.job = job; 
     this.jobLauncher = jobLauncher; 
    } 

    @Override 
    public void beforeJob(JobExecution jobExecution) { 
     LOG.info("Strarting job {}"); 
    } 

    @Override 
    public void afterJob(JobExecution jobExecution) { 
     LOG.info("Job is completed job"); 
     CompletableFuture.runAsync(()->{ 
      try { 
       jobLauncher.run(job, new JobParametersBuilder().toJobParameters()); 
      } catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException 
        | JobParametersInvalidException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     }); 
    } 

} 

샘플 봄 - batch.xml.

관련 문제