SpringBoot-AOP切面-接口请求时不走定时任务-发送通知-Aspect全解析-Before-Aspect-After-AfterThrowing
EduMigrateTaskControlAspect.java
package cn.jiangjiesheng.edu.service.train.migrate.aspect;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* 更多说明 https://blog.csdn.net/lmx125254/article/details/84398412
*/
@Aspect
@Component
@Slf4j
public class EduMigrateTaskControlAspect {
@Getter
@Setter
public static boolean isMigrateTraining = false;
// 第一个*代表返回类型不限
// 第二个*代表所有类(.java)
// 第三个*代表所有方法
// (..) 代表参数不限
// @Pointcut("execution(public * com.lmx.blog.controller.*.*(..))")
// @Before("execution(* cn.jiangjiesheng.edu.controller.handleOldData.train.EduTrainMigrateController.*(..)) && @annotation(org.springframework.web.bind.annotation.GetMapping)")
// public void before(JoinPoint joinPoint) throws Throwable {
// 有异步的情况
// EduMigrateTaskControlAspect.isMigrateTraining = true;
// }
// @After("execution(* cn.jiangjiesheng.edu.controller.handleOldData.train.EduTrainMigrateController.*(..)) && @annotation(org.springframework.web.bind.annotation.GetMapping)")
// public void after() throws Throwable {
// 有异步的情况
// EduMigrateTaskControlAspect.isMigrateTraining = false;
// }
@AfterThrowing("within(cn.jiangjiesheng.edu.controller.handleOldData.train.EduTrainMigrateController)")
public void afterThrowing() {
//执行afterThrowing时,after 也会执行
log.error("afterThrowing拦截到迁移接口出现异常(nginx超时可能也会走到这里)");
EduMigrateTaskControlAspect.isMigrateTraining = false;
}
/**
* 配置织入点
*/
@Pointcut("@annotation(org.springframework.scheduling.annotation.Scheduled)")
public void taskControlPointCut() {
}
@Around("taskControlPointCut()")
public Object doAround(ProceedingJoinPoint point) throws Throwable {
// 如果不在迁移
if (!isMigrateTraining) {
// 执行当前任务
return point.proceed();
}
log.warn("迁移进行中,不走定时任务");
return null;
}
}
EduTrainCourseOrExamChangeAspect.java
package cn.jiangjiesheng.edu.component.aspect;
import cn.jiangjiesheng.commons.utils.UserInfoUtil;
import cn.jiangjiesheng.edu.core.executor.AfterCommitExecutor;
import cn.jiangjiesheng.edu.entity.train.EduOfflineCourse;
import cn.jiangjiesheng.edu.service.train.EduTrainRemindService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
/**
* 结合 @Transactional 实现异步处理 ,不影响主流程
*/
@Aspect
@Configuration
@Slf4j
public class EduTrainCourseOrExamChangeAspect {
@Autowired
private EduTrainRemindService eduTrainRemindService;
@Autowired
private AfterCommitExecutor afterCommitExecutor;
@AfterReturning("@annotation(cn.jiangjiesheng.edu.component.aspect.annotation.EduTrainCourseOrExamChangeCut)")
public void eduTrainCourseOrExamChanged(JoinPoint joinPoint) throws Throwable {
String containerId = UserInfoUtil.getCurContainerId();
afterCommitExecutor.execute(() -> doEduTrainCourseOrExamChanged(containerId, joinPoint.getArgs()));
}
private void doEduTrainCourseOrExamChanged(String containerId, Object[] args) {
for (Object arg : args) {
if (arg instanceof EduOfflineCourse) {
EduOfflineCourse eduOfflineCourseVo = (EduOfflineCourse) arg;
if (eduOfflineCourseVo.getId() != null) {
//编辑场景
return;
}
log.info("线下课程增加入参:eduOfflineCourseVo:{}", eduOfflineCourseVo);
eduTrainRemindService.remindUserWhenChangeCourseOrExam(containerId, eduOfflineCourseVo.getImplementId());
}
}
}
}
EduTrainCourseOrExamChangeCut.java
package cn.jiangjiesheng.edu.component.aspect.annotation;
import java.lang.annotation.*;
/**
* 发布后课程或考试发生变化
*/
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EduTrainCourseOrExamChangeCut {
}
正文到此结束
- 本文标签: Spring Spring Boot Java
- 本文链接: https://code.jiangjiesheng.cn/article/27
- 版权声明: 本文由小江同学原创发布,转载请先联系本站长,谢谢。