nj363073261
nj363073261
采纳率0%
2019-08-07 14:58 阅读 2.1k

@Async 异步失效。。。。。。。。。。。。。

5

我有一个切面用来管理操作日志的。代码如下


package com.amn.aspect;

import com.amn.controller.SystemControllerLog;
import com.amn.exception.AmnException;
import com.amn.model.SysLog;
import com.amn.service.SysLogService;
import com.amn.util.DateUtil;
import com.amn.util.StringUtil;
import com.amn.util.TokenManager;
import io.jsonwebtoken.Claims;
import net.sf.json.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.NamedThreadLocal;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

@Aspect
@Component
public class SystemLogAspect {
private static final Logger logger = LoggerFactory.getLogger(SystemLogAspect.class);

@Autowired(required=false)
private HttpServletRequest request;


@Autowired
private SysLogService logService;

//@Autowired
//private LogService logService;

/**
 * Controller层切点 注解拦截
 */
//@Pointcut("execution(* com.amn.controller.*.*(..))")
@Pointcut("@annotation(com.amn.controller.SystemControllerLog)")
public void controllerAspect(){
    System.out.println("fsdfssssssssssssss");
}

@Before("controllerAspect()")
public void doBefore(JoinPoint joinPoint) throws InterruptedException{

}

public static String getControllerMethodDescription2(JoinPoint joinPoint) {
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    Method method = signature.getMethod();
    SystemControllerLog controllerLog = method
            .getAnnotation(SystemControllerLog.class);
    String discription = controllerLog.description();
    return discription;
}

@SuppressWarnings("unchecked")
@After("controllerAspect()")
public void doAfter(JoinPoint joinPoint) {
    SysLog sysLog=getSysLog(request);
    String param="";
    param=com.alibaba.fastjson.JSONObject.toJSONString(joinPoint.getArgs());
    sysLog.setTitle(getControllerMethodDescription2(joinPoint));
    sysLog.setParams(sysLog.getParams()+"请求参数:"+param);
    logService.saveSysLog(sysLog);
    //sysLog.setTimeout(DateUtils.formatDateTime(endTime - beginTime));
    //threadPoolTaskExecutor.execute(new SaveLogThread(sysLog, logService));
    //logThreadLocal.set(sysLog);
}


@AfterThrowing(pointcut = "controllerAspect()", throwing = "e")
public  void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
    SysLog sysLog=getSysLog(request);
    sysLog.setType("error");
    sysLog.setException(e.toString());
    sysLog.setLogId(StringUtil.guid());
    String param="";
    param=com.alibaba.fastjson.JSONObject.toJSONString(joinPoint.getArgs());
    sysLog.setParams(sysLog.getParams()+"请求参数:"+param);
    sysLog.setTitle(getControllerMethodDescription2(joinPoint));
    logService.saveSysLog(sysLog);
}


public SysLog getSysLog(HttpServletRequest request) {
    String title = "";
    String type = "info";
    String remoteAddr = request.getRemoteAddr();//请求的IP
    String requestUri = request.getRequestURI();//请求的Uri
    String method = request.getMethod();        //请求的方法类型(post/get)
    String token = request.getParameter("token");
    Map<String, String[]> params = request.getParameterMap(); //请求提交的参数
    String operationid = "";
    if (StringUtil.isEmpty(token)) {
        try {
            Claims claims = TokenManager.parseJWT(token);
            if (claims != null) {
                operationid = claims.get("jti").toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    SysLog sysLog = new SysLog();
    sysLog.setLogId(StringUtil.guid());
    sysLog.setTitle(title);
    sysLog.setType(type);
    sysLog.setRemoteAddr(remoteAddr);
    sysLog.setRequestUri(requestUri);
    sysLog.setMethod(method);
    if (params != null && params.size() > 0) {
        sysLog.setParams(JSONObject.fromObject(params).toString());
    }
    sysLog.setUserId(operationid);
    Date operateDate = new Date();
    sysLog.setOperateDate(operateDate);
    return sysLog;
}

}
然后@async 标记的方法不能异步执行了。有大神知道什么原因吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

相关推荐