fix: 修复定时任务写在controller时,不生效问题

This commit is contained in:
lhc
2025-09-02 14:20:19 +08:00
parent f6a51546ec
commit 6de131f358
3 changed files with 51 additions and 45 deletions

View File

@@ -52,13 +52,15 @@ public class DataSourceAop {
@Before("log()") @Before("log()")
public void doBefore(JoinPoint joinPoint) { public void doBefore(JoinPoint joinPoint) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest(); if (attributes != null) {
if (isMulti) { HttpServletRequest request = attributes.getRequest();
String key = request.getParameter("datasourceKey"); if (isMulti) {
if (StringUtils.isBlank(key)) { String key = request.getParameter("datasourceKey");
DBContextHolder.setDataSource(DataUnit.MASTER); if (StringUtils.isBlank(key)) {
} else { DBContextHolder.setDataSource(DataUnit.MASTER);
DBContextHolder.setDataSource(key); } else {
DBContextHolder.setDataSource(key);
}
} }
} }
} }

View File

@@ -45,38 +45,42 @@ public class RequestLogAspect {
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
Object result = proceedingJoinPoint.proceed(); Object result = proceedingJoinPoint.proceed();
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (frameConfig.getShowControllerLog()) { if (attributes != null) {
RequestInfo requestInfo = new RequestInfo(); HttpServletRequest request = attributes.getRequest();
requestInfo.setIp(request.getRemoteAddr()); if (frameConfig.getShowControllerLog()) {
requestInfo.setUrl(request.getRequestURL().toString()); RequestInfo requestInfo = new RequestInfo();
requestInfo.setHttpMethod(request.getMethod()); requestInfo.setIp(request.getRemoteAddr());
requestInfo.setClassMethod(String.format("%s.%s", proceedingJoinPoint.getSignature().getDeclaringTypeName(), requestInfo.setUrl(request.getRequestURL().toString());
proceedingJoinPoint.getSignature().getName())); requestInfo.setHttpMethod(request.getMethod());
requestInfo.setRequestParams(getRequestParamsByProceedingJoinPoint(proceedingJoinPoint)); requestInfo.setClassMethod(String.format("%s.%s", proceedingJoinPoint.getSignature().getDeclaringTypeName(),
requestInfo.setResult(result); proceedingJoinPoint.getSignature().getName()));
requestInfo.setTimeCost(System.currentTimeMillis() - start); requestInfo.setRequestParams(getRequestParamsByProceedingJoinPoint(proceedingJoinPoint));
LOGGER.info("Request Info : {}", JSON.toJSONString(requestInfo)); requestInfo.setResult(result);
requestInfo.setTimeCost(System.currentTimeMillis() - start);
LOGGER.info("Request Info : {}", JSON.toJSONString(requestInfo));
}
} }
return result; return result;
} }
@AfterThrowing(pointcut = "requestServer()", throwing = "e") @AfterThrowing(pointcut = "requestServer()", throwing = "e")
public void doAfterThrow(JoinPoint joinPoint, RuntimeException e) { public void doAfterThrow(JoinPoint joinPoint, RuntimeException e) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest(); if (attributes != null) {
RequestErrorInfo requestErrorInfo = new RequestErrorInfo(); HttpServletRequest request = attributes.getRequest();
requestErrorInfo.setIp(request.getRemoteAddr()); RequestErrorInfo requestErrorInfo = new RequestErrorInfo();
requestErrorInfo.setUrl(request.getRequestURL().toString()); requestErrorInfo.setIp(request.getRemoteAddr());
requestErrorInfo.setHttpMethod(request.getMethod()); requestErrorInfo.setUrl(request.getRequestURL().toString());
requestErrorInfo.setClassMethod(String.format("%s.%s", joinPoint.getSignature().getDeclaringTypeName(), requestErrorInfo.setHttpMethod(request.getMethod());
joinPoint.getSignature().getName())); requestErrorInfo.setClassMethod(String.format("%s.%s", joinPoint.getSignature().getDeclaringTypeName(),
requestErrorInfo.setRequestParams(getRequestParamsByJoinPoint(joinPoint)); joinPoint.getSignature().getName()));
requestErrorInfo.setException(e); requestErrorInfo.setRequestParams(getRequestParamsByJoinPoint(joinPoint));
LOGGER.error("Error Request Info : {}", JSON.toJSONString(requestErrorInfo)); requestErrorInfo.setException(e);
LOGGER.error("Error Request Info : {}", JSON.toJSONString(requestErrorInfo));
}
} }
/** /**

View File

@@ -230,25 +230,25 @@
</profile> </profile>
</profiles> </profiles>
<distributionManagement> <distributionManagement>
<repository>
<id>releases</id>
<name>Releases</name>
<url>http://192.168.0.123:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Snapshot</name>
<url>http://192.168.0.123:8081/repository/maven-snapshots/</url>
</snapshotRepository>
<!-- <repository>--> <!-- <repository>-->
<!-- <id>releases-hc</id>--> <!-- <id>releases</id>-->
<!-- <name>Releases</name>--> <!-- <name>Releases</name>-->
<!-- <url>https://qs.taixingyiji.com:8082/repository/maven-releases/</url>--> <!-- <url>http://192.168.0.123:8081/repository/maven-releases/</url>-->
<!-- </repository>--> <!-- </repository>-->
<!-- <snapshotRepository>--> <!-- <snapshotRepository>-->
<!-- <id>snapshots-hc</id>--> <!-- <id>snapshots</id>-->
<!-- <name>Snapshot</name>--> <!-- <name>Snapshot</name>-->
<!-- <url>https://qs.taixingyiji.com:8082/repository/maven-snapshots/</url>--> <!-- <url>http://192.168.0.123:8081/repository/maven-snapshots/</url>-->
<!-- </snapshotRepository>--> <!-- </snapshotRepository>-->
<repository>
<id>releases-hc</id>
<name>Releases</name>
<url>https://qs.taixingyiji.com:8082/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots-hc</id>
<name>Snapshot</name>
<url>https://qs.taixingyiji.com:8082/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement> </distributionManagement>
</project> </project>