| | |
| | | package com.ltkj.framework.aspectj; |
| | | |
| | | import com.ltkj.common.annotation.DataSource; |
| | | import com.ltkj.common.utils.StringUtils; |
| | | import com.ltkj.db.DataSourceContextHolder; |
| | | import com.ltkj.framework.datasource.DynamicDataSourceContextHolder; |
| | | 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.aspectj.lang.reflect.MethodSignature; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.core.annotation.AnnotationUtils; |
| | | import org.springframework.core.annotation.Order; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * ClassName: AsynAspect <br/> |
| | | * Description: <br/> |
| | | * Description: 在异步方法执行时,获取并传递当前线程的数据源。<br/> |
| | | * date: 2025/2/15 18:07<br/> |
| | | * |
| | | * @author zjh<br /> |
| | | */ |
| | | @Aspect |
| | | @Order(2) |
| | | @Component |
| | | //@Aspect |
| | | //@Component |
| | | public class AsynAspect { |
| | | |
| | | protected Logger logger = LoggerFactory.getLogger(getClass()); |
| | | private static final Logger logger = LoggerFactory.getLogger(AsynAspect.class); |
| | | |
| | | @Pointcut("@annotation(org.springframework.scheduling.annotation.Async)" |
| | | + "|| @within(org.springframework.scheduling.annotation.Async)") |
| | | public void dsPointCut() { |
| | | |
| | | @Pointcut("@annotation(org.springframework.scheduling.annotation.Async) || @within(org.springframework.scheduling.annotation.Async)") |
| | | public void asyncPointCut() { |
| | | // 异步方法切入点 |
| | | } |
| | | |
| | | @Around("dsPointCut()") |
| | | @Around("asyncPointCut()") |
| | | public Object around(ProceedingJoinPoint point) throws Throwable { |
| | | String key = DataSourceContextHolder.getDataSourceKey(); |
| | | logger.info("执行之前"); |
| | | logger.info("DataSourceContextHolder ->{}",key); |
| | | logger.info("DynamicDataSourceContextHolder ->{}", DynamicDataSourceContextHolder.getDataSourceType()); |
| | | // 获取当前线程的 DataSource |
| | | String currentDataSource = DataSourceContextHolder.getDataSourceKey(); |
| | | |
| | | DynamicDataSourceContextHolder.setDataSourceType(key); |
| | | logger.info("当前线程数据源: {}", currentDataSource); |
| | | |
| | | // 在异步方法执行前,设置数据源到 DynamicDataSourceContextHolder |
| | | DynamicDataSourceContextHolder.setDataSourceType(currentDataSource); |
| | | |
| | | try { |
| | | // 执行异步方法 |
| | | return point.proceed(); |
| | | } finally { |
| | | logger.info("执行之后"); |
| | | logger.info("DataSourceContextHolder ->{}",key); |
| | | logger.info("DynamicDataSourceContextHolder ->{}",DynamicDataSourceContextHolder.getDataSourceType()); |
| | | // 销毁数据源 在执行方法之后 |
| | | // 执行完毕后清理数据源 |
| | | DynamicDataSourceContextHolder.clearDataSourceType(); |
| | | logger.info("执行之后 clear之后"); |
| | | logger.info("DataSourceContextHolder ->{}",key); |
| | | logger.info("DynamicDataSourceContextHolder ->{}",DynamicDataSourceContextHolder.getDataSourceType()); |
| | | logger.info("清理数据源,恢复默认数据源"); |
| | | } |
| | | } |
| | | } |