|
@@ -0,0 +1,67 @@
|
|
|
+package cn.efunbox.manage.base.assist.async;
|
|
|
+
|
|
|
+import cn.efunbox.manage.base.entity.OperationLog;
|
|
|
+import cn.efunbox.manage.base.entity.User;
|
|
|
+import cn.efunbox.manage.base.service.OperationLogService;
|
|
|
+import cn.efunbox.manage.common.utils.IpUtils;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.scheduling.annotation.AsyncResult;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.concurrent.Future;
|
|
|
+
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class AsyncOperationLogHandler {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ OperationLogService operationLogService;
|
|
|
+
|
|
|
+
|
|
|
+ @Async("threadPoolAsyncTaskExecutor")
|
|
|
+ public void asyncHandleLog(JoinPoint joinPoint, HttpServletRequest request,Object retVal) {
|
|
|
+
|
|
|
+ User loginUser = (User) request.getAttribute("userInfo");
|
|
|
+
|
|
|
+ //如果用户不为空
|
|
|
+ if(Objects.nonNull(loginUser)) {
|
|
|
+ OperationLog operationLog = new OperationLog();
|
|
|
+ try {
|
|
|
+ //设置操作用户 信息
|
|
|
+ operationLog.setUserName(loginUser.getNickName());
|
|
|
+ operationLog.setUserId(loginUser.getId()+"");
|
|
|
+ //设置 ip
|
|
|
+ operationLog.setIp(IpUtils.getIp(request));
|
|
|
+ //设置 商户类型
|
|
|
+ operationLog.setMethodName(joinPoint.getSignature().getName());
|
|
|
+ operationLog.setRequestUrl(request.getRequestURI());
|
|
|
+ operationLog.setParamJson(JSON.toJSONString(joinPoint.getArgs()));
|
|
|
+ operationLog.setReturnData(JSON.toJSONString(retVal));
|
|
|
+ operationLogService.saveOpertionLog(operationLog);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("URL : {} 记录操作日志信息失败 message :{} ", request.getRequestURI(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("用户:{} 完成日志切面 url:{}",loginUser.getNickName(),request.getRequestURI());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Async
|
|
|
+ public Future<String> asyncMethodWithReturnType() {
|
|
|
+ log.error("Execute method asynchronously " + Thread.currentThread().getName());
|
|
|
+ try {
|
|
|
+ Thread.sleep(5000);
|
|
|
+ return new AsyncResult<>("hello world !!!!");
|
|
|
+ } catch (final InterruptedException e) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|