Spring Batch之Job级拦截器实现(四)
一、拦截器实现说明
1.拦截器实现的方式
Spring Batch项目实现Job级拦截器有两种方法:
(1)实现接口:org.springframework.batch.core.JobExecutionListener
public interface JobExecutionListener {//Job执行之前调用该方法void beforeJob(JobExecution var1);//Job执行之后调用该方法void afterJob(JobExecution var1);
}
(2)通过Annotation机制实现
- @BeforeJob
- @AfterJob
2.拦截器的配置
通过下面的方法配置拦截器:
<batch:job id="jobId">
<batch:step id="stepId">
. . . . . .
</batch:step>
<!--定义拦截器-->
<batch:listeners>
<batch:listener ref="listenerId"/>
</batch:listeners>
</batch:job>
<!--拦截器实现类-->
<bean id="listenerId" class="com.xj.demo3.MyJobExecutionListener"/>
3.拦截器异常
拦截器方法如果抛出异常会影响Job的正常执行,所以在执行自定义的拦截器时候,要考虑对拦截器发生的异常做处理,避免影响业务。如果拦截器发生异常,会导致Job执行的状态为“FAILED”。
4.拦截器执行顺序
在配置文件中可以配置多个listener,拦截器之间的执行顺序按照listener定义的顺序执行。before方法按照listener定义的顺序执行,after方法按照相反的顺序执行。(参考下面项目的运行结果)
二、拦截器项目实现
1.项目结构:
2.代码实现:
Demo3BatchMain.java:
package com.xj.demo3;import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @Author : xjfu* @Date : 2021/11/7 18:29* @Description :*/
public class Demo3BatchMain {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("demo3/job/demo3-job.xml");//Spring Batch的作业启动器JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");//在demo3-jobContext.xml中配置一个作业Job job = (Job) context.getBean("demo3TaskletJob");try{//开始执行这个作业,获得出来结果(要运行的job,job参数对象)JobExecution result = launcher.run(job, new JobParameters());System.out.println(result.toString());}catch (Exception e){e.printStackTrace();}}
}
MyAnnotationListener.java:
package com.xj.demo3;import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.annotation.AfterJob;
import org.springframework.batch.core.annotation.BeforeJob;/*** @Author : xjfu* @Date : 2021/11/7 18:57* @Description :通过Annotation实现JobExecutionListener接口的拦截器*/
public class MyAnnotationListener {//Job执行之前调用该方法@BeforeJobpublic void beforeJob(JobExecution jobExecution){System.out.println("MyAnnotationListener——before: create time:" + jobExecution.getCreateTime());}//Job执行之后调用该方法@AfterJobpublic void afterJob(JobExecution jobExecution){System.out.println("MyAnnotationListener——after: create time:" + jobExecution.getCreateTime());}
}
MyJobExecutionListener.java:
package com.xj.demo3;import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;/*** @Author : xjfu* @Date : 2021/11/7 18:53* @Description :实现JobExecutionListener接口的拦截器*/
public class MyJobExecutionListener implements JobExecutionListener {//Job执行之前调用该方法@Overridepublic void beforeJob(JobExecution jobExecution) {System.out.println("MyJobExecutionListener——before: create time:" + jobExecution.getCreateTime());}//Job执行之后调用该方法@Overridepublic void afterJob(JobExecution jobExecution) {System.out.println("MyJobExecutionListener——after: create time:" + jobExecution.getCreateTime());}
}
MyTasklet.java:
package com.xj.demo3;import org.apache.commons.collections.bag.SynchronizedSortedBag;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;/*** @Author : xjfu* @Date : 2021/11/7 18:29* @Description :*/
public class MyTasklet implements Tasklet {@Overridepublic RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {System.out.println("This is a test for interceptor(拦截器)");return RepeatStatus.FINISHED;}
}
demo3-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:demo3/job/demo3-jobContext.xml"/><batch:job id="demo3TaskletJob"><batch:step id="demo3Step"><batch:tasklet ref="myTasklet"/></batch:step><!--定义两个拦截器--><batch:listeners><batch:listener ref="myJobExecutionListener"/><batch:listener ref="myAnnotationListener"/></batch:listeners></batch:job>
</beans>
demo3-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><bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/><bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"><property name="jobRepository" ref="jobRepository"/></bean><!--定义MyTasklet类--><bean id="myTasklet" class="com.xj.demo3.MyTasklet"/><!--定义MyJobExecutionListener拦截器--><bean id="myJobExecutionListener" class="com.xj.demo3.MyJobExecutionListener"/><!--定义MyAnnotationListener拦截器--><bean id="myAnnotationListener" class="com.xj.demo3.MyAnnotationListener"/>
</beans>
3.运行结果:
拦截器的执行顺序:
(1)MyJobExecutionListener的before()方法;
(2)MyAnnotationListener的before()方法;
(3)MyAnnotationListener的after()方法;
(4)MyJobExecutionListener的after()方法。
Spring Batch之Job级拦截器实现(四)相关推荐
- Spring AOP 源码分析 - 拦截器链的执行过程
1.简介 本篇文章是 AOP 源码分析系列文章的最后一篇文章,在前面的两篇文章中,我分别介绍了 Spring AOP 是如何为目标 bean 筛选合适的通知器,以及如何创建代理对象的过程.现在我们的得 ...
- Spring AOP源码解析-拦截器链的执行过程
一.简介 在前面的两篇文章中,分别介绍了 Spring AOP 是如何为目标 bean 筛选合适的通知器,以及如何创建代理对象的过程.现在得到了 bean 的代理对象,且通知也以合适的方式插在了目标方 ...
- spring aop与strut2的拦截器冲突
作者:yan 当配置spring aop(比如日志的aop)拦截所有类(包括)action时,会与struts2的拦截器机制相冲突,导致struts2的机制失效,功能用不了: 解决方法:spring ...
- Spring框架注入注解与拦截器浅谈
依赖注入注解 1实现注入的注解: (1)修饰属性.方法.构造函数 @Autowire:自动注入,自动去spring容器中寻找指定的bean来注入,require属性指定注入这个bean是否是必须的,如 ...
- Spring Boot细节挖掘(拦截器)
拦截器的原理很简单,是 AOP 的一种实现,专门拦截对动态资源的后台请求,即拦截对控制层的请求.常见的使用场景包括判断用户是否有权限请求后台,再拔高一层的使用场景,比如拦截器可以结合 WebSocke ...
- Spring中过滤器(Filter)和拦截器(Interceptor)的区别和联系
在我们日常的开发中,我们经常会用到Filter和Interceptor.有时同一个功能.Filter可以做,Interceptor也可以做.有时就需要考虑使用哪一个比较好.这篇文章主要介绍一下,二者的 ...
- 【Spring学习】过滤器和拦截器
1.认识过滤器(Filter) 1.1.过滤器的定义 过滤器是JavaWeb的三大组件之一,是实现Filter接口的Java类. 过滤器是实现对请求资源(jsp.servlet.html)的过滤功能, ...
- WebService学习总结十 使用Spring发布WebService并添加拦截器
首先使用Spring方式发布成功WebService,再在客户端和服务器端引入出拦截器和入拦截器,引入的方式是写在配置文件中的. 客户端: 自定义的拦截器 package ws.client.inte ...
- Spring Boot 系列:过滤器+拦截器+监听器
原 Swagger 文章合并到 Spring Boot 系列:配置 Swagger2 一.过滤器 - Filter 过滤器是处于客户端和服务器资源文件之间的一道过滤网,帮助我们过滤掉一些不符合要求的请 ...
- springboot拦截html页面元素,Spring Boot 中如何使用拦截器(十五)
关于拦截器,大家一定都不陌生,spring boot 中是如何使用拦截器的呢?今天就举个例子,来给大家说明一下,废话不多说,开始撸代码!!! 1.创建一个新的spring boot项目,并引入相应的j ...
最新文章
- 黑科技:CSS定制多行省略
- android 中 webview 怎么用 localStorage?
- 运算符 - PHP手册笔记
- python自学需要多久-自学Python多久能找到工作
- 【C 语言】指针 与 数组 ( 指针 | 数组 | 指针运算 | 数组访问方式 | 字符串 | 指针数组 | 数组指针 | 多维数组 | 多维指针 | 数组参数 | 函数指针 | 复杂指针解读)
- Jeecg-boot 使用心得建议
- nginx linux windows 忽略大小写_React 基础 在 Windows 下使用 React , 你需要注意这些问题...
- Postman 根据nginx日志查账号
- R文件报错:cannot resolve symbol ‘R’
- RJ-45接口信号定义
- 从0使用webpack构建reactjs
- JAVA基础知识总结:一
- 51单片机与JQ8900语音播报模块
- 怎么启动mysql2008_SQL Server 2008初次启动
- 【OpenCV学习笔记】之六 手写图像旋转函数---万丈高楼平地起
- 手机浏览网页页面缩放
- 图片社交php,图像社交时代
- java基础 IO流
- 解决无法使用IMAP将Gmail帐户添加到Outlook的问题
- java ctr_java – CTR模式使用初始向量(IV)