​ 最近在工作使用boot+quartz整合,开发定时调度平台,遇到需要对Quartz的Job进行异常后将异常记录到日志表的操作,第一反应就想到了使用Spring的AOP,利用AfterThrowing来完成这个操作。

话不多说,直接上代码:

 <!--整合Quartz-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

1.正常的一个job类:

2. 创建JobBeanFactory类,重写 SpringBeanJobFactory 的方法。

 import org.quartz.SchedulerContext;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.lang.Nullable;
import org.springframework.scheduling.quartz.SpringBeanJobFactory; /**
* @Author: Hujh
* @Date: 2019/7/5 9:38
* @Description: 配置job工厂
*/
public class JobBeanFactory extends SpringBeanJobFactory{ @Nullable
private String[] ignoredUnknownProperties; @Nullable
private SchedulerContext schedulerContext; private final BeanFactory beanFactory; JobBeanFactory(BeanFactory beanFactory){
this.beanFactory = beanFactory;
} public void setIgnoredUnknownProperties(String... ignoredUnknownProperties) {
this.ignoredUnknownProperties = ignoredUnknownProperties;
} @Override
public void setSchedulerContext(SchedulerContext schedulerContext) {
this.schedulerContext = schedulerContext;
} /**
* @Title: createJobInstance
* @Author : Hujh
* @Date: 2019/7/5 10:19
* @Description : 创建调度工作接口
* @param : bundle
* @Return : java.lang.Object
*/
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Class<?> jobClass = bundle.getJobDetail().getJobClass();
Object job = beanFactory.getBean(jobClass);
if (isEligibleForPropertyPopulation(job)) {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(job);
MutablePropertyValues pvs = new MutablePropertyValues();
if (this.schedulerContext != null) {
pvs.addPropertyValues(this.schedulerContext);
}
pvs.addPropertyValues(bundle.getJobDetail().getJobDataMap());
pvs.addPropertyValues(bundle.getTrigger().getJobDataMap());
if (this.ignoredUnknownProperties != null) {
for (String propName : this.ignoredUnknownProperties) {
if (pvs.contains(propName) && !bw.isWritableProperty(propName)) {
pvs.removePropertyValue(propName);
}
}
bw.setPropertyValues(pvs);
}
else {
bw.setPropertyValues(pvs, true);
}
}
return job;
}
}

3.创建 QuartzConfig类,交给spring容器进行管理

 import org.springframework.beans.BeansException;
import org.springframework.boot.autoconfigure.quartz.SchedulerFactoryBeanCustomizer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @Author: Hujh
* @Date: 2019/7/5 9:39
* @Description: Quartz相关配置类
*/
@Configuration
public class QuartzConfig implements ApplicationContextAware { private ApplicationContext applicationContext; @Bean
public SchedulerFactoryBeanCustomizer schedulerFactoryBeanCustomizer() {
// Spring 项目整合 Quartz 主要依靠添加 SchedulerFactoryBean 这个 FactoryBean
return schedulerFactoryBean -> schedulerFactoryBean.setJobFactory(new JobBeanFactory(applicationContext));
} /**
* 初始化加载applicationContext
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}

4. 创建AOP拦截:JobLogAop

  <!-- aop依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
 import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; /**
* @Author: Hujh
* @Date: 2019/7/4 17:12
* @Description: 工作调度任务拦截 (日志记录)
*/
@Component
@Aspect
public class JobLogAop { private Logger logger = LoggerFactory.getLogger(JobLogAop.class); /**
* 只对于execute切入
*/
@Pointcut("execution(public void com.ylsp.jobs..*.execute(..))")
public void pointJobLog(){} /**
* 切点通知(方法执行后)
*/
@AfterReturning("pointJobLog()")
public void handlerControllerMethod() {
logger.info("execute方法执行后..---->> 开始写入日志");
} }

  注:直接创建一个Job,可以看到Aop已经生效。

[AOP拦截 ]SpringBoot+Quartz Aop拦截Job类中的方法的更多相关文章

  1. Spring @Cacheable注解 && 事务@Transactional 在同一个类中的方法调用不生效

    @Cacheable 注解在对象内部调用不会生效 代码示例:ProductServiceImpl.java public List<ProductInfoVO> getProductLis ...

  2. 分析spring事务@Transactional注解在同一个类中的方法之间调用不生效的原因及解决方案

    问题: 在Spring管理的项目中,方法A使用了Transactional注解,试图实现事务性.但当同一个class中的方法B调用方法A时,会发现方法A中的异常不再导致回滚,也即事务失效了. 当这个方 ...

  3. thymeleaf模板引擎调用java类中的方法(附源码)

    前言 <Docker+SpringBoot+Mybatis+thymeleaf的Java博客系统开源啦> 由于开源了项目的缘故,很多使用了My Blog项目的朋友遇到问题也都会联系我去解决 ...

  4. 第6章 Java类中的方法

    1.如何定义java的方法 什么是方法:方法使用来解决一类问题的代码集合,是一个功能模块在类中定义个方法的方法是: 访问修饰符 返回值类型 方法名(参数列表){ 方法体 } 1.访问修饰符,是限制该方 ...

  5. java 27 - 6 反射之 通过配置文件运行类中的方法

    在以前,如果我们想要调用一个类中的方法,只能这样子: 例: 有Cat和Dog两个类,里面有eat和run两个成员方法: public class Dog { public void eat() { S ...

  6. WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据

    WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据 WebForm1.aspx 页面 (原生AJAX请求,写法一) <%@ Page Langu ...

  7. Hibernate中对象的三种状态以及Session类中saveOrUpdate方法与merge方法的区别

    首先,用一张图说明一个对象,在Hibernate中,在调用了不同方法之后对象所处的不同状态 在Hibernate中,一个对象的状态可以被分为如图所示的三种 Transient:瞬时对象,该对象在数据库 ...

  8. Java基础知识强化之集合框架笔记33:Arrays工具类中asList()方法的使用

    1. Arrays工具类中asList()方法的使用 public static <T> List<T> asList(T... a): 把数组转成集合 注意事项: 虽然可以把 ...

  9. swift -- 类中的方法

    一. 引用类型  类 在类中定义方法 class Person { //属性 var name : String = "" //方法 //实例方法 : 在类里面创建一个方法 fun ...

随机推荐

  1. 一个 Qt 显示图片的控件(继承QWidget,使用QPixmap记录图像,最后在paintEvent进行绘制,可缩放)

    Qt 中没有专门显示图片的控件,通常我们会使用QLabel来显示图片.但是QLabel 显示图片的能力还是有点弱.比如不支持图像的缩放一类的功能,使用起来不是很方便.因此我就自己写了个简单的类. 我这 ...

  2. Delphi使用TObject类对象创建接受window消息(使用Classes.AllocateHWnd为对象创建一个尺寸为0的窗口,从而有了Handle)good

    在delphi中,有时候我们希望对象可以接收windows消息,怎么办呢?因为要接收windows消息起码要有windows Handle,难道要建立的一个可见窗口?那样似乎太差强人意了.delphi ...

  3. Qt实现网络播放器

        写了这么多的博客,关于网络的还不算多,经常有人询问一些关于网络传输.制作在线试听及下载音乐.构造及解析数据等的一些问题,今天就在这里一并讲解.   网络操作:     主要涉及:QNetwor ...

  4. QTcpSocket 对连接服务器中断的不同情况进行判定(六种情况,其中一种使用IsNetworkAlive API方法)

    简述 对于一个C/S结构的程序,客户端有些时候需要实时得知与服务器的连接状态.而对于客户端与服务器断开连接的因素很多,现在就目前遇到的情况进行一下总结. 分为下面六种不同情况 客户端网线断开 客户端网 ...

  5. centos 部署 asp.net core Error -99 EADDRNOTAVAIL address not available解决

    centos7.3上部署 asp.net core 错误如下: Hosting environment: Production Content root path: /home/netcore Now ...

  6. Python自学day-7

    一.静态方法(@staticmethod) class Dog(object): def __init__(self): pass @staticmethod def talk(): #静态方法 pa ...

  7. HBase 学习之路(四)—— HBase集群环境配置

    一.集群规划 这里搭建一个3节点的HBase集群,其中三台主机上均为Regin Server.同时为了保证高可用,除了在hadoop001上部署主Master服务外,还在hadoop002上部署备用的 ...

  8. AJAX异步提交form表单

    记录: 网上有说怎么做,没说怎么接收,打印了一下数据,记录一下取值: 比如说有如下form: <form id="form1" name="form1" ...

  9. Visual Studio模板代码注释小技巧分享

    在日常开发过程中,难免有这样一种需求:就是你所建的每一个类文件或者接口文件都需要标注下作者姓名以及类的用途.如果我们每次创建文件的时候都需要写一遍这些信息是很烦神的.还好Visual Studio给我 ...

  10. 使用release自动打包发布正式版详细教程

    昨天写了个release插件的版本管理,今天就在自动发布过程中遇到了许多坑,只能再写一篇自动发布详细教程,纪念我那昨日逝去的青春 (╥ _ ╥`) release正常打包发布流程按照如下几个阶段: C ...