2016-03-29 51 views
0

我有一個彈簧批處理應用程序,它從主表中讀取數據並將90天以前的所有記錄移動到歸檔表中。如果任何塊失敗,如何中止彈簧批處理作業

應用程序上下文文件是 「

<context:property-placeholder location="classpath:batch.properties" /> 

<context:component-scan base-package="com.prax.batch" /> 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" > 
    <property name="url" value="${batch.jdbc.url}"/> 
    <property name="driverClassName" value="${batch.jdbc.driver}"/> 
    <property name="username" value="${batch.jdbc.user}"/> 
    <property name="password" value="${batch.jdbc.password}"/> 
</bean> 

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dataSource"/> 
</bean> 

<bean id="jobReposotoryTransactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/> 

<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> 
    <property name="jobRepository" ref="jobRepository"></property> 
</bean> 

<!-- <jdbc:initialize-database data-source="dataSource"> 
    <jdbc:script location="${batch.schema.script}" /> 
</jdbc:initialize-database> --> 

<!-- <batch:job-repository id="jobRepository" transaction-manager="jobReposotoryTransactionManager"></batch:job-repository> --> 

<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" > 
    <property name="transactionManager" ref="jobReposotoryTransactionManager"/> 
</bean> 

<import resource="classpath:/META-INF/spring/module-context.xml" /> 



</beans> 

和工作方面的文件將作爲

<batch:job id="fqtvArchiveJob"> 
    <batch:step id="readWriteDeleteStep"> 
     <batch:tasklet transaction-manager="transactionManager" 
      start-limit="10"> 
      <batch:chunk reader="fqtvreader" writer="fqtvcompositewriter" 
       commit-interval="1000" /> 
     </batch:tasklet> 
    </batch:step> 
</batch:job> 


<bean id="fqtvreader" 
    class="org.springframework.batch.item.database.JdbcCursorItemReader"> 
    <property name="rowMapper" ref="fqtvrowmapper" /> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="sql" value="${batch.reader.sql}" /> 
    <property name="maxRows" value="0"/> 
    <property name="fetchSize" value="0" /> 
</bean> 

<bean id="fqtvinsertwriter" 
    class="org.springframework.batch.item.database.JdbcBatchItemWriter"> 
    <property name="assertUpdates" value="true" /> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="itemPreparedStatementSetter" ref="fqtvpreparedstatementsetter" /> 
    <property name="sql" value="${batch.writer.insert.sql}" /> 
</bean> 

<bean id="fqtvdeletewriter" 
    class="org.springframework.batch.item.database.JdbcBatchItemWriter"> 
    <property name="assertUpdates" value="true" /> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="itemPreparedStatementSetter" ref="deletestatementsetter" /> 
    <property name="sql" value="${batch.writer.delete.sql}" /> 
</bean> 

<bean id="fqtvcompositewriter" 
    class="org.springframework.batch.item.support.CompositeItemWriter"> 
    <property name="delegates"> 
     <list> 
      <ref local="fqtvinsertwriter"/> 
      <ref local="fqtvdeletewriter"/> 
     </list> 
    </property> 
</bean> 

<bean id="fqtvrowmapper" class="com.prax.job.util.FqtvRowMapper"   /> 

<bean id="fqtvpreparedstatementsetter" 
    class="com.prax.job.util.FqtvPreparedStatementSetter" /> 

<bean id="deletestatementsetter" 
    class="com.prax.job.util.FqtvDeleteStatementSetter" /> 

</beans> 

我的要求是,如果任何項目的歸檔失敗,批處理應該停止,而不是存檔更多記錄並將郵件通知發送給用戶。

回答

0
<batch:job id="fqtvArchiveJob"> 
    <batch:step id="readWriteDeleteStep"> 
    <batch:tasklet transaction-manager="transactionManager" 
     start-limit="10"> 
     <batch:chunk reader="fqtvreader" writer="fqtvcompositewriter" 
      commit-interval="1000" skip-limit="10" > 
     <skippable-exception-classes> 
      <exclude class="customExceptionIDontWannaSkip"/> 
     </skippable-exception-classes> 
     </batch:chunk> 
    </batch:tasklet> 
</batch:step> 
    <batch:listeners> 
       <batch:listener ref="jobListener" /> 
    </batch:listeners> 
</batch:job> 

JobListener應該實現JobExecutionListener並重寫:

@Override 
public void afterJob(JobExecution exec) { 
    BatchStatus status = exec.getStatus(); 
    if (!status.equals(BatchStatus.COMPLETED)){ 
    //send the mail 
    } 
} 
+0

你好,感謝您提供幫助。這裏跳過限制被定義爲10.但是要求是,如果批處理不能處理任何記錄,那麼它應該停止而不是跳過該記錄,並且應該隨後發送郵件通知。 –

+0

該進程無法處理任何記錄==>不可跳過的異常。如果你不想跳過任何異常''',至於跳過限制,你可以按照你想要的方式設置它 –

相關問題