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;
}
}
使用Aspect的@Around注解实现定时任务执行
最新推荐文章于 2025-01-10 17:18:06 发布