如果任何块失败,如何中止 spring 批处理作业
how to abort a spring batch job if any chunk fails
我有一个 spring 批处理应用程序,它从主 table 读取数据并将所有早于 90 天的记录移动到存档 table。
应用程序上下文文件如下
“
<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>
我有一个要求,如果任何项目归档失败,该批处理应该停止并且不再归档任何记录并发送邮件通知给用户。
<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
}
}
我有一个 spring 批处理应用程序,它从主 table 读取数据并将所有早于 90 天的记录移动到存档 table。
应用程序上下文文件如下 “
<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>
我有一个要求,如果任何项目归档失败,该批处理应该停止并且不再归档任何记录并发送邮件通知给用户。
<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
}
}