nj363073261 2019-08-07 14:58 采纳率: 0%
浏览 2410
已结题

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

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


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条回答 默认 最新

  • dabocaiqq 2019-08-07 21:49
    关注
    评论

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)