2011-10-24 73 views
2

問題是這樣的:我有一個步驟的Spring Batch作業。這一步被稱爲多次。如果每次調用一切正常(無例外),則作業狀態爲「已完成」。如果有什麼不好的happends在步驟的執行的至少一個(拋出一個異常),我已經配置改變退出代碼失敗StepListener:Spring-Batch:我如何從StepListener返回一個自定義的Job exit代碼

public class SkipCheckingListener extends StepExecutionListenerSupport { 

    public ExitStatus afterStep(StepExecution stepExecution) { 
     String exitCode = stepExecution.getExitStatus().getExitCode(); 
     if (stepExecution.getProcessorSkipCount() > 0) { 
      return new ExitStatus(ExitStatus.FAILED); 
     } 
     else { 
      return null; 
     } 
    } 

} 

這工作得很好,當條件滿足執行「if」塊並且作業完成狀態爲FAILED。不過請注意,我在這裏返回的退出代碼仍然是Spring Batch的標準代碼。我想在某些時候返回我的個性化退出代碼,例如「完成SKIP」。現在,我已經嘗試過更新上面的代碼只是返回:

public class SkipCheckingListener extends StepExecutionListenerSupport { 

    public ExitStatus afterStep(StepExecution stepExecution) { 
     String exitCode = stepExecution.getExitStatus().getExitCode(); 
     if (stepExecution.getProcessorSkipCount() > 0) { 
      return new ExitStatus("COMPLETED WITH SKIPS"); 
     } 
     else { 
      return null; 
     } 
    } 

} 
,因爲它在文檔中描述

:(5.3.2.1批狀態與退出狀態)http://static.springsource.org/spring-batch/reference/html/configureStep.html。我甚至試過

stepExecution.getJobExecution().setExitStatus("COMPLETED WITH SKIPS"); 

果然,在執行到達「如果」塊,執行的代碼,而我的工作有完整的退出代碼完成,完全地忽略我已經通過設置退出代碼聽衆。

在他們的文檔中沒有關於這方面的更多詳細信息,我還沒有發現任何使用Google的東西。有人可以告訴我如何以這種方式改變作業退出代碼?感謝名單

回答

6

的樣子,你就不能改變BatchStatus,但你可以用exitstatus

這個代碼用JobListener作品嚐試對我來說

// JobListener with interface or annotation 
public void afterJob(JobExecution jobExecution) { 
    jobExecution.setExitStatus(new ExitStatus("foo", "fooBar")); 
} 
+0

加入工作的代碼片段 –

+0

謝謝,工作得很好。 –

+0

@MichaelLange你能解釋一下你發現的選擇(失敗條件),你在這裏討論:http://forum.springsource.org/showthread.php?71328-Promote-Step-ExitStatus-to-Job –

相關問題