使用Aspect的@Around注解实现定时任务执行

本文介绍了一个使用Spring Boot的CPS任务Aspect,通过Redis的SetNX操作来控制定时任务执行。它根据`ScheduledSwitchConfig`的开关状态和方法名来决定是否执行`scheduledCpsWallet`或`scheduledWallet`方法。
package com.jumi.microservice.redissetnx;
import com.jumi.microservice.common.exception.BaseException;
import com.jumi.microservice.config.ScheduledSwitchConfig;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Aspect
@Component
public class CpsTaskAspect {
    @Value("${spring.application.name}")
    private String projectName;
    @Resource
    private RedisTemplate<Object, Object> redisTemplate;
    @Resource
    ScheduledSwitchConfig scheduledSwitchConfig;
    /**
     * ProceedingJoinPoint:
     * -- point
     * -- 切入点对象
     * -- point.getSignature().getName() 【执行方法】
     * execution:
     * -- 定义在microservice包和所有子包里的任意类的任意方法的执行
     * -- execution(* com.jumi.microservice..*.*(..))
     * annotation:
     * -- 自定义的注解对象
     * -- @annotation(org.springframework.scheduling.annotation.Scheduled)
     */
    @Around("execution(* com.jumi.microservice..*.*(..)) && @annotation(org.springframework.scheduling.annotation.Scheduled)")
    public Object task(ProceedingJoinPoint point) {
        /*
         * 控制器开关为0
         * 执行方法 不是 scheduledCpsWallet
         * 执行方法 不是 scheduledWallet
         */
        
//        /*
//         * coupon服务【coupon-provider】
//         * ScheduledSwitchConfig配置类
//         */
//        @Configuration
//        @RefreshScope
//        public class ScheduledSwitchConfig {
//            @Value("${scheduler.enable}")
//            private String flag;
//
//            public String getFlag() {
//                return flag;
//            }
//            public void setFlag(String flag) {
//                this.flag = flag;
//            }
//        }
        
//        /*
//         * coupon服务【coupon-provider】
//         * CouponServiceImpl实现类
//         */
//        @Scheduled(cron = "0 0 3 * * ?")
//        public void scheduledCpsWallet(){
//            List<Map<String, BigDecimal>> listMap = userWalletMapper.listDayWaitCashByTime();
//            for(Map<String,BigDecimal> map:listMap){
//                redisTemplate.opsForValue().set(userLastDayWaitCash.concat(String.valueOf(map.get("userId"))),
//                        String.valueOf(map.get("amount")));
//            }
//        }
        
//        /*
//         * wallet服务【wallet-provider】
//         * WalletServiceImpl实现类
//         */
//        @Scheduled(cron = "0 0 3 * * ?")
//        public void scheduledWallet(){
//            List<Map<String,BigDecimal>> listMap = userWalletMapper.listDayWaitCashByTime();
//            for(Map<String,BigDecimal> map:listMap){
//                redisTemplate.opsForValue().set(userLastDayWaitCash.concat(String.valueOf(map.get("userId"))),
//                        String.valueOf(map.get("amount")));
//            }
//        }
        if (
                "0".equals(scheduledSwitchConfig.getFlag())
                && !"scheduledWallet".equals(point.getSignature().getName())
                && !"scheduledCpsWallet".equals(point.getSignature().getName())
        ) {
            return null;
        }
        String key = projectName + "::" + point.getSignature().getName();
        Boolean result = redisTemplate.opsForValue().setIfAbsent(key, "", 29, TimeUnit.SECONDS);
        if (result != null && result) {
            try {
                return point.proceed();
            } catch (Throwable e) {
                redisTemplate.delete(key);
                e.printStackTrace();
                throw new BaseException(500, "定时任务出错 :" + e.getMessage());
            }
        }
        return null;
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值