由来 正常在公司的项目中, 为了保证项目的可靠性, 同一个项目可能会部署到多台服务器上, 通常点说的话就是集群.
那么, 在部署集群项目的时候. 定时任务的运行就是一个问题了. 我们只需要一台机器运行定时任务. 因此, 就需要把其他机器上的定时任务屏蔽掉. 那么, 就需要一个拦截定时任务的功能.
创建一个定时任务 启动类添加@EnableScheduling注解,开启定时任务功能
创建一个每五秒执行一次的定时任务
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @Author: luobendexiaoqiang
* @Date: 2020/7/24 19:39
*/
@Component
@Slf4j
public class TaskTest {
/**
* 五秒执行一次
*/
@Scheduled(fixedRate = 5 * 1000)
public void taskTest() {
log.info("五秒一次定时任务开始执行!!");
}}
启动项目, 定时任务启动成功, 每五秒执行一次
![利用springAOP+@Conditional注解拦截定时任务](https://img.it610.com/image/info8/bd4f75719b3c447b963012d7ab21ae57.jpg)
文章图片
添加springaop依赖
org.springframework.boot
spring-boot-starter-aop
创建定时任务拦截类
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Value;
/**
* @Author: luobendexiaoqiang
* @Date: 2020/7/24 19:48
*/
@Slf4j
@Component()
@Aspect
public class FilterScheduledAop {@Value("${scheduled.filter:#{false}}")
private Boolean scheduledFilter;
/* 拦截@Scheduled注解 */
@Pointcut(" @annotation(org.springframework.scheduling.annotation.Scheduled)")
public void proxyAspect() {}/**
* 拦截不到private方法
*
* @param joinPoint
* @return
*/
@Around("proxyAspect()")
public Object doInvoke(ProceedingJoinPoint joinPoint) {
if (!scheduledFilter) {
try {
log.info("开始执行定时任务!!!");
//执行被拦截的方法
return joinPoint.proceed();
} catch (Throwable e) {
log.error("定时器拦截器异常", e);
}
} else {
log.info("定时任务已被拦截!!!");
}return null;
}
}
application.properties配置文件添加scheduled.filter:true, true表示要拦截定时器,默认不拦截
![利用springAOP+@Conditional注解拦截定时任务](https://img.it610.com/image/info8/cb5e4e4eec3448699e1585edb99ed118.jpg)
文章图片
定时任务已经被拦截, 定时任务方法不再被执行
从以上情况也能看出, 利用了AOP拦截住定时器之后, 不管需不需要运行定时任务, 定时任务都会被拦截. 在这种情况下, 当我们需要执行定时任务的时候, 每个定时任务都会被AOP无端的拦截一次. 那么, 有什么方法能让我们在需要执行定时任务的时候, 不被拦截呢?
使用@Conditional注解 创建TaskFilterCondition类并实现Condition接口
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* @Author: qinqiang
* @Date: 2020/7/24 15:22
*/
public class TaskFilterCondition implements Condition {@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
//获取配置文件中scheduled.filter的值
String filterschedule = conditionContext.getEnvironment().getProperty("scheduled.filter");
return "true".equals(filterschedule);
}
}
修改AOP拦截类
import com.springboot.service.condition.TaskFilterCondition;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;
/**
* @Author: luobendexiaoqiang
* @Date: 2020/7/24 19:48
*/
@Slf4j
@Component()
@Aspect
@Conditional(TaskFilterCondition.class)
public class FilterScheduledAop {/* 拦截@Scheduled注解 */
@Pointcut(" @annotation(org.springframework.scheduling.annotation.Scheduled)")
public void proxyAspect() {}/**
* 拦截不到private方法
*
* @param joinPoint
* @return
*/
@Around("proxyAspect()")
public Object doInvoke(ProceedingJoinPoint joinPoint) {log.info("定时任务已被拦截!!!");
return null;
}
}
【利用springAOP+@Conditional注解拦截定时任务】@Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件给容器注册bean。
也就是说, 只有TaskFilterCondition类中的matches方法返回true时, AOP拦截类才会被实例化
- 当我们不需要关闭定时任务的时候, 我们不实例化AOP拦截类, 定时任务当然就不会被无端的拦截了.
- 当我们需要关闭定时任务的时候, 我们再实例化AOP拦截类, 来实现拦截定时任务的操作
推荐阅读
- =======j2ee|spring用注解实现注入的@resource,@autowired,@inject区别
- jar|springboot项目打成jar包和war包,并部署(快速打包部署)
- 数据库|效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】)...
- java人生|35K 入职华为Java开发那天,我哭了(这 5 个月做的一切都值了)
- Java毕业设计项目实战篇|Java项目:在线嘿嘿网盘系统设计和实现(java+Springboot+ssm+mysql+maven)
- 微服务|微服务系列:服务发现与注册-----Eureka(面试突击!你想了解的Eureka都在这里.持续更新中......)
- java|ApplicationListener和SpringApplicationRunListener的联系
- Spring|SpringSecurity--自定义登录页面、注销登录配置
- 性能|性能工具之 Jmeter 通过 SpringBoot 工程启动
- 代码狂魔|Spring源码分析之IOC容器初始化流程