如果任何块失败,如何中止spring批处理作业

时间:2016-03-29 10:32:16

标签: spring spring-batch

我有一个弹簧批处理应用程序,它从主表中读取数据,并将所有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>

我要求如果任何项目的归档失败,批处理应停止,不再存档任何记录并将邮件通知发送给用户。

1 个答案:

答案 0 :(得分: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
  }
}