2016-05-13 48 views
0
<job id="pullPurgeProcessStoreFiles" xmlns="http://www.springframework.org/schema/batch"> 
    <bean id="PullFilesTasklet" class="com.example.PullFilesTasklet" /> 
     <step id="pullFiles" next="validation" > 
      <tasklet ref="PullFilesTasklet"> 
       <skippable-exception-classes> 
        <include class="java.io.FileNotFoundException"/> 
       </skippable-exception-classes> 
      </tasklet> 
     </step>  
</job> 

獲得以下錯誤添加可跳過的異常類: 無效的含量被發現開始元素skippable-exception-classes在微進程Impementation

在研究中,我發現skippable-exception-classes可以在區塊內使用。但我需要用ref tasklets來達到同樣的效果。

+0

通過哪種方式,我可以達到同樣的與裁判的微進程? –

回答

3

如果你想Skip Exception在你自己的Tasklet的實現,你需要編寫代碼在你自己的實現中這樣做。

請關注此原創thread並請上投票如果此解決方案適合您,請在原始主題上使用。

你可以做這樣的事情

abstract class SkippableTasklet implements Tasklet { 

    //Exceptions that should not cause job status to be BatchStatus.FAILED 
    private List<Class<?>> skippableExceptions; 

    public void setSkippableExceptions(List<Class<?>> skippableExceptions) { 
     this.skippableExceptions = skippableExceptions; 
    } 

    private boolean isSkippable(Exception e) { 
     if (skippableExceptions == null) { 
      return false; 
     } 

     for (Class<?> c : skippableExceptions) { 
      if (e.getClass().isAssignableFrom(c)) { 
       return true; 
      } 
     } 
     return true; 
    } 

    protected abstract void run(JobParameters jobParameters) throws Exception; 

    @Override 
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) 
      throws Exception { 

     StepExecution stepExecution = chunkContext.getStepContext().getStepExecution(); 
     JobExecution jobExecution = stepExecution.getJobExecution(); 
     JobParameters jobParameters = jobExecution.getJobParameters(); 

     try { 
      run(prj); 
     } catch (Exception e) { 
      if (!isSkippable(e)) { 
       throw e; 
      } else { 
       jobExecution.addFailureException(e); 
      } 
     } 

     return RepeatStatus.FINISHED; 
    } 
} 

而且在SpringXML配置

<batch:tasklet> 
    <bean class="com.MySkippableTasklet" scope="step" autowire="byType"> 
     <property name="skippableExceptions"> 
      <list> 
       <value>org.springframework.mail.MailException</value> 
      </list> 
     </property> 
    </bean> 
</batch:tasklet>