Spring Batch之Job级拦截器实现(四)_人……杰的博客-CSDN博客
二、Step级拦截器
Spring Batch之Step级拦截器实现(十三)_人……杰的博客-CSDN博客
三、ChunkListener
主要操作:Chunk执行前、Chunk执行后。
(1)实现接口方式:
接口ChunkListener声明如下:
public interface ChunkListener extends StepListener {
void beforeChunk(ChunkContext var1);
void afterChunk(ChunkContext var1);
void afterChunkError(ChunkContext var1);
- beforeChunk():在Chunk执行前触发,在Step事务中(同@BeforeChunk)
- afterChunk():在Chunk执行后触发,不在Step事务中(同@AfterChunk)
(2)通过Annotation机制方式
四、ItemReadListener
主要操作:在Chunk读阶段的拦截器,可以在读之前、读之后、读发生异常时候触发该拦截器。
(1)实现接口方式:
接口ItemReadListener声明如下:
public interface ItemReadListener<T> extends StepListener {
void beforeRead();
void afterRead(T var1);
void onReadError(Exception var1);
- beforeRead():在ItemReader#read()之前执行(同@BeforeRead)
- afterRead():在ItemReader#read()之后执行(同@AfterRead)
- onReadError():当ItemReader#read()抛出异常时候触发该操作(同@OnReadError)
(2)通过Annotation机制方式
- @BeforeRead
- @AfterRead
- @OnReadError
五、ItemProcessListener
主要操作:在Chunk处理阶段的拦截器,可以在处理之前、处理之后、处理发生异常时候触发该拦截器。
(1)实现接口方式:
接口ItemProcessListener声明如下:
public interface ItemProcessListener<T, S> extends StepListener {
void beforeProcess(T var1);
void afterProcess(T var1, S var2);
void onProcessError(T var1, Exception var2);
- beforeProcess():在ItemProcessor.process()之前执行(同@BeforeProcess)
- afterProcess():在ItemProcessor.process()之后执行,即使process方法返回null,仍然会触发拦截器操作(同@AfterProcess)
- onProcessError():当ItemProcessor.process()抛出异常时触发该操作(同@OnProcessError)
(2)通过Annotation机制方式
- @BeforeProcess
- @AfterProcess
- @OnProcessError
六、ItemWriteListener
主要操作:在Chunk写阶段的拦截器,可以在写之前、写之后、写发生异常时候触发该拦截器。
(1)实现接口方式:
接口ItemWriteListener声明如下:
public interface ItemWriteListener<S> extends StepListener {
void beforeWrite(List<? extends S> var1);
void afterWrite(List<? extends S> var1);
void onWriteError(Exception var1, List<? extends S> var2);
- beforeWrite():在ItemWrite#write()之前执行(同@BeforeWrite)
- afterWrite():在ItemWrite#write()之后执行,该操作会在事务提交之前、ChunkListener#afterChunk()之前执行(同@AfterWrite)
- onWriteError():在ItemWrite#write()抛出异常时触发该操作(同@OnWriteError)
(2)通过Annotation机制方式
- @BeforeWrite
- @AfterWrite
- @OnWriteError
七、SkipListener
主要操作:SkipListener在Chunk处理阶段抛出跳过定义的异常时触发,在Chunk读、处理、写阶段发生的异常都会触发该拦截器。
(1)实现接口方式:
接口SkipListener声明如下:
public interface SkipListener<T, S> extends StepListener {
void onSkipInRead(Throwable var1);
void onSkipInWrite(S var1, Throwable var2);
void onSkipInProcess(T var1, Throwable var2);
- onSkipInRead():在读阶段发生异常并且配置了异常可以跳过时触发该操作(同@OnSkipInRead)
- onSkipInWrite():在写阶段发生异常并且配置了异常可以跳过时触发该操作(同@OnSkipInWrite)
- onSkipInProcess():在处理阶段发生异常并且配置了异常可以跳过时触发该操作(同@OnSkipInProcess)
(2)通过Annotation机制方式
- @OnSkipInRead
- @OnSkipInWrite
- @OnSkipInProcess
八、RetryListener
主要操作:RetryListener在Chunk处理阶段抛出重试定义的异常时触发,通过拦截器可以在重试动作发生时进行日志记录、收集重试信息等。可以直接实现接口RetryListener,也可以直接继承RetryListenerSupport,通常只需要实现onError操作,在重试发生错误时触发该操作。
(1)实现接口方式:
接口RetryListener声明如下:
public interface RetryListener {
<T, E extends Throwable> boolean open(RetryContext var1, RetryCallback<T, E> var2);
<T, E extends Throwable> void close(RetryContext var1, RetryCallback<T, E> var2, Throwable var3);
<T, E extends Throwable> void onError(RetryContext var1, RetryCallback<T, E> var2, Throwable var3);
- open():在进入retry之前执行该操作,可以在该操作中准备重试需要的资源;如果该操作返回false,将会终止本次重试操作,且会抛出异常:org.springframework.batch.retry.TerminatedRetryException.
- close():在retry结束之前执行该操作,可以在该方法中关闭在open操作中打开的资源
- onError():重试发生错误时触发该操作
九、拦截器执行顺序
拦截器根据作用域的代销分别执行对应的before和after操作,其中读、处理、写三个拦截器没有嵌套关系,三者按照顺序先后执行。拦截器先后执行顺序入下:
十、项目实例
1.项目架构
2.代码实现
Demo18BatchMain.java:
package com.xj.demo18;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo18BatchMain {
public static void main(String[] args) {
Demo18BatchMain batchMain = new Demo18BatchMain();
batchMain.executeJob("demo18/job/demo18-job.xml", "listenerJob", "jobLauncher", new JobParametersBuilder().toJobParameters());
*执行Job
* @param jobXmlPath 配置job的xml文件路径
* @param jobId job的id
* @param jobLauncherId jobLauncher的id
* @param jobParameters 参数
public void executeJob(String jobXmlPath, String jobId, String jobLauncherId, JobParameters jobParameters){
ApplicationContext context = new ClassPathXmlApplicationContext(jobXmlPath);
JobLauncher jobLauncher = (JobLauncher) context.getBean(jobLauncherId);
//获取要执行的Job
Job job = (Job)context.getBean(jobId);
//开始执行作业Job
JobExecution jobExecution = jobLauncher.run(job, jobParameters);
//输出执行结果
System.out.println(jobExecution.toString());
}catch (Exception e){
e.printStackTrace();
AutoReader.java:
package com.xj.demo18;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
public class AutoReader implements ItemReader<String> {
private int count = 0;
private int maxCount = 30;
@Override
public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
if(count > maxCount){
return null;
}else{
count++;
System.out.println("reader--->" + count);
return count + "";
public void setMaxCount(int maxCount) {
this.maxCount = maxCount;
public int getMaxCount() {
return maxCount;
AutoWriter.java:
package com.xj.demo18;
import org.springframework.batch.item.ItemWriter;
import java.util.List;
public class AutoWriter implements ItemWriter<String> {
@Override
public void write(List<? extends String> list) throws Exception {
System.out.println("Writer Begin!");
for(String item : list){
System.out.println("Writer --->" + item);
System.out.println("Writer End!");
PassProcessor.java:
package com.xj.demo18;
import org.springframework.batch.item.ItemProcessor;
public class PassProcessor<T> implements ItemProcessor<T,T> {
@Override
public T process(T t) throws Exception {
System.out.println("processor:" + (String) t);
return t;
SystemOutJobExecutionListener.java:
package com.xj.demo18.listener;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
public class SystemOutJobExecutionListener implements JobExecutionListener {
/* (non-Javadoc)
* @see org.springframework.batch.core.JobExecutionListener#beforeJob(org.springframework.batch.core.JobExecution)
public void beforeJob(JobExecution jobExecution) {
System.out.println("JobExecutionListener.beforeJob()");
/* (non-Javadoc)
* @see org.springframework.batch.core.JobExecutionListener#afterJob(org.springframework.batch.core.JobExecution)
public void afterJob(JobExecution jobExecution) {
System.out.println("JobExecutionListener.afterJob()");
SystemOutStepExecutionListener.java:
package com.xj.demo18.listener;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
* @author bruce.liu(mailto:jxta.liu@gmail.com)
* 2013-3-21下午10:52:28
public class SystemOutStepExecutionListener implements StepExecutionListener {
/* (non-Javadoc)
* @see org.springframework.batch.core.StepExecutionListener#beforeStep(org.springframework.batch.core.StepExecution)
public void beforeStep(StepExecution stepExecution) {
System.out.println("StepExecutionListener.beforeStep()");
/* (non-Javadoc)
* @see org.springframework.batch.core.StepExecutionListener#afterStep(org.springframework.batch.core.StepExecution)
public ExitStatus afterStep(StepExecution stepExecution) {
System.out.println("StepExecutionListener.afterStep()");
return null;
SystemOutChunkListener.java:
package com.xj.demo18.listener;
import org.springframework.batch.core.ChunkListener;
import org.springframework.batch.core.scope.context.ChunkContext;
public class SystemOutChunkListener implements ChunkListener {
@Override
public void beforeChunk(ChunkContext context) {
System.out.println("ChunkListener.beforeChunk()");
@Override
public void afterChunk(ChunkContext context) {
System.out.println("ChunkListener.afterChunk()");
@Override
public void afterChunkError(ChunkContext context) {
System.out.println("ChunkListener.afterChunkError()");
SystemOutItemReadListener.java:
package com.xj.demo18.listener;
import org.springframework.batch.core.ItemReadListener;
public class SystemOutItemReadListener implements ItemReadListener<String> {
public void beforeRead() {
System.out.println("ItemReadListener.beforeRead()");
public void afterRead(String item) {
System.out.println("ItemReadListener.afterRead()");
public void onReadError(Exception ex) {
System.out.println("ItemReadListener.onReadError()");
SystemOutItemProcessListener.java:
package com.xj.demo18.listener;
import org.springframework.batch.core.ItemProcessListener;
public class SystemOutItemProcessListener implements ItemProcessListener<String, String> {
public void beforeProcess(String item) {
System.out.println("ItemProcessListener.beforeProcess()");
public void afterProcess(String item, String result) {
System.out.println("ItemProcessListener.afterProcess()");
public void onProcessError(String item, Exception e) {
System.out.println("ItemProcessListener.onProcessError()");
SystemOutItemWriteListener.java:
package com.xj.demo18.listener;
import org.springframework.batch.core.ItemWriteListener;
import java.util.List;
public class SystemOutItemWriteListener implements ItemWriteListener<String> {
public void beforeWrite(List<? extends String> items) {
System.out.println("ItemWriteListener.beforeWrite()");
public void afterWrite(List<? extends String> items) {
System.out.println("ItemWriteListener.afterWrite()");
public void onWriteError(Exception exception, List<? extends String> items) {
System.out.println("ItemWriteListener.onWriteError()");
SystemOutSkipListener.java:
package com.xj.demo18.listener;
import org.springframework.batch.core.SkipListener;
public class SystemOutSkipListener implements SkipListener<String, String> {
public void onSkipInRead(Throwable t) {
System.out.println("SkipListener.onSkipInRead()");
public void onSkipInWrite(String item, Throwable t) {
System.out.println("SkipListener.onSkipInWrite()");
public void onSkipInProcess(String item, Throwable t) {
System.out.println("SkipListener.onSkipInProcess()");
SystemOutRetryListener.java:
package com.xj.demo18.listener;
import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.RetryListener;
public class SystemOutRetryListener implements RetryListener {
@Override
public <T, E extends Throwable> boolean open(RetryContext retryContext, RetryCallback<T, E> retryCallback) {
System.out.println("RetryListener.open()");
return true;
@Override
public <T, E extends Throwable> void close(RetryContext retryContext, RetryCallback<T, E> retryCallback, Throwable throwable) {
System.out.println("RetryListener.close()");
@Override
public <T, E extends Throwable> void onError(RetryContext retryContext, RetryCallback<T, E> retryCallback, Throwable throwable) {
System.out.println("RetryListener.onError()");
demo18-job.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">
<import resource="classpath:demo18/job/demo18-jobContext.xml"/>
<import resource="classpath:demo18/job/demo18-jobListener.xml"/>
<batch:job id="listenerJob">
<batch:step id="listenerStep">
<batch:tasklet>
<batch:chunk reader="reader" processor="processor" writer="writer" commit-interval="1" retry-limit="1">
<!--重试拦截器-->
<batch:retry-listeners>
<batch:listener ref="retryListener"/>
</batch:retry-listeners>
<batch:retryable-exception-classes>
<batch:include class="java.lang.RuntimeException"/>
</batch:retryable-exception-classes>
<batch:listeners>
<!--Chunk级拦截器-->
<batch:listener ref="chunkListener"/>
<!--ItemRead级拦截器-->
<batch:listener ref="itemReadListener"/>
<!--ItemProcess级拦截器-->
<batch:listener ref="itemProcessListener"/>
<!--ItemWrite级拦截器-->
<batch:listener ref="itemWriteListener"/>
<!--跳过拦截器-->
<batch:listener ref="skipListener"/>
</batch:listeners>
</batch:chunk>
</batch:tasklet>
<batch:listeners>
<!--Step级拦截器-->
<batch:listener ref="stepExecutionListener"/>
</batch:listeners>
</batch:step>
<batch:listeners>
<!--Job级拦截器-->
<batch:listener ref="jobExecutionListener"/>
</batch:listeners>
</batch:job>
</beans>
demo18-jobListener.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--定义各种拦截器-->
<bean id="jobExecutionListener" class="com.xj.demo18.listener.SystemOutJobExecutionListener" />
<bean id="stepExecutionListener" class="com.xj.demo18.listener.SystemOutStepExecutionListener" />
<bean id="chunkListener" class="com.xj.demo18.listener.SystemOutChunkListener" />
<bean id="itemReadListener" class="com.xj.demo18.listener.SystemOutItemReadListener" />
<bean id="itemProcessListener" class="com.xj.demo18.listener.SystemOutItemProcessListener" />
<bean id="itemWriteListener" class="com.xj.demo18.listener.SystemOutItemWriteListener" />
<bean id="retryListener" class="com.xj.demo18.listener.SystemOutRetryListener" />
<bean id="skipListener" class="com.xj.demo18.listener.SystemOutSkipListener" />
</beans>
demo18-jobContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"/>
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository"/>
</bean>
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
<bean id="reader" class="com.xj.demo18.AutoReader" >
<property name="maxCount" value="0" />
</bean>
<bean id="writer" class="com.xj.demo18.AutoWriter" />
<bean id="processor" class="com.xj.demo18.PassProcessor"/>
</beans>
3.运行结果
通过运行结果可以看出,拦截器的运行顺序符合描述。
一、Job级拦截器Spring Batch之Job级拦截器实现(四)_人……杰的博客-CSDN博客二、Step级拦截器Spring Batch之Step级拦截器实现(十三)_人……杰的博客-CSDN博客三、ChunkListener主要操作:Chunk执行前、Chunk执行后。(1)实现接口方式:接口ChunkListener声明如下:public interface ChunkListener extends StepListener { void before
boost::process::throw_on_error相关的测试程序实现功能C++实现代码
boost::process::throw_on_error相关的测试程序
C++实现代码
#include <boost/core/lightweight_test.hpp>
#include <boost/process.hpp>
#include <boost/process/cmd.hpp>
#include <system_error>
写在前面: 我是「境里婆娑」。我还是从前那个少年,没有一丝丝改变,时间只不过是考验,种在心中信念丝毫未减,眼前这个少年,还是最初那张脸,面前再多艰险不退却。
写博客的目的就是分享给大家一起学习交流,如果您对
Java感兴趣,可以关注我,我们一起学习。
前言:为什么要写这篇文章,由于长时间都是在使用连接数据库第三方框架Mybatis等,不使用JDBC操作,导致很多基础知识都朦朦胧胧似懂非懂,今天抽空把这部分内容认真复习了下,顺便写篇文章加深印象。本文以MySql为例。
Spring Batch项目实现Step级
拦截器有两种方法:
(1)实现接口:org.
springframework.
batch.core.StepExecution
Listener
public interface StepExecution
Listener extends Step
Listener {
//Step执行之前调用该方法
void beforeStep(StepExecution var1);
什么是SpringBatch
Spring Batch 是一个轻量级的、完善的批处理框架(并不是调度框架,需要配合Quartz等框架,实现定时任务),旨在帮助企业建立健壮、高效的批处理应用。
Spring Batch 提供了大量可重用的组件,包括了日志、追踪、事务、任务作业统计、任务重启、跳过、重复、资源管理。对于大数据量和高性能的批处理任务,Spring Batch 同样提供了高级功能和特性来支...
Spring Batch项目实现
拦截器有两种方法:
(1)实现接口:org.
springframework.
batch.core.JobExecution
Listener
public interface JobExecution
Listener {
///ob执行之前调用该方法
void beforeJob(JobExecution var1);
//Job执行之后调用该方法
void afterJob(JobExecu
<div class="company_info" v-for="(item, index) in corpList" :key="index">
<van-uploader class="uploader_wrapper"
:after-read="afterRead(index)">
<div class="uploader uploader2">
<van-image