手写@Async异步注解
2023-02-18 16:45:17 时间
一、自定义注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CopyAsync {
}
二、编写AOP切面类
@Aspect
@Component
public class CopyAsyncAop {
@Around(value = "@annotation(com.xx.CopyAsync)")
public void around(ProceedingJoinPoint joinPoint) {
try {
// 一般使用线程池
new Thread(() -> {
try {
joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}, "线程xx").start();
} catch (Throwable throwable) {
}
}
}
三、调用测试类
@RestController
@Slf4j
public class Test {
@Autowired
private TestService testService;
@RequestMapping("/add")
public String add() {
log.info("1");
testService.asyncLog();
log.info("3");
return "3";
}
}
四、TestService类
@Component
@Slf4j
public class TestService {
@CopyAsync
public void asyncLog() {
try {
log.info("目标方法开始执行,阻塞10秒");
Thread.sleep(10000);
log.info("2");
} catch (Exception e) {
}
}
}
相关文章
- Redis 中的 BitMaps(位图)命令详解
- Redis 概述安装
- Redis 配置文件
- Redis 发布和订阅
- Redis 持久化操作
- Redis 新数据类型
- Redis Java整合
- Redis 缓存问题
- Redis 事务与锁
- Redis 6.0新功能
- 安卓项目五子棋代码详解(三)
- 安卓项目五子棋代码详解(二)
- 安卓项目五子棋代码详解(一)
- 3xx HTTP状态码的终极指南
- React报错之React.Children.only expected to receive single React element child
- React报错之Element type is invalid
- React报错之Too many re-renders
- React报错之Function components cannot have string refs
- 如何发布一个 TypeScript 编写的 npm 包
- 如何使用zx编写shell脚本