利用springAOP+@Conditional注解拦截定时任务

由来

正常在公司的项目中, 为了保证项目的可靠性, 同一个项目可能会部署到多台服务器上, 通常点说的话就是集群.
那么, 在部署集群项目的时候. 定时任务的运行就是一个问题了. 我们只需要一台机器运行定时任务. 因此, 就需要把其他机器上的定时任务屏蔽掉. 那么, 就需要一个拦截定时任务的功能.

创建一个定时任务

启动类添加@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依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

创建定时任务拦截类


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表示要拦截定时器,默认不拦截

在这里插入图片描述
定时任务已经被拦截, 定时任务方法不再被执行

从以上情况也能看出, 利用了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;
    }
}

@Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件给容器注册bean。

也就是说, 只有TaskFilterCondition类中的matches方法返回true时, AOP拦截类才会被实例化

  • 当我们不需要关闭定时任务的时候, 我们不实例化AOP拦截类, 定时任务当然就不会被无端的拦截了.
  • 当我们需要关闭定时任务的时候, 我们再实例化AOP拦截类, 来实现拦截定时任务的操作
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值