• @Async使用记录


    Before:

    1号

    @ApiOperation("保存日志")
    @PostMapping("/saveLog")
    @Override
    public ApiResult saveLog(@RequestBody Req req) {
        return logManager.saveLog(req);
    } 

     2号

    @Async
    @Override
    public ApiResult saveLog(Req req) {
        logService.saveBusinessLog();
        return ApiResult.success(Boolean.TRUE);
    }
    

     3号

    @RestControllerAdvice
    @Order(-2147483628)
    @ConditionalOnProperty(prefix = "response.wrap",name = "enabled",matchIfMissing = true)
    @Slf4j
    public class ResponseWrapHandler implements ResponseBodyAdvice {
        @Override
        public boolean supports(MethodParameter returnType, Class converterType) {
            return true;
        }
    
        @Override
        public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
            if (body instanceof ApiResult
                    ||returnType.hasMethodAnnotation(SkipWrap.class)
                    ||body instanceof String
                    ||HttpContext.isFeignRequest()
                    || PathMatchUtil.match(MainClassConstant.SWAGGER_URL, HttpContext.getRequest().getServletPath())
                    ){
                return body;
            }
            return ApiResult.success(body);
        }
    }
    

    Before结果:

    此时,1号返回的是:

    {

        "success": true,

        "code": "200",

        "msg": "请求成功",

        "traceId": "",

        "data": null

    }

     

     After:

    @ApiOperation("保存日志")

    @PostMapping("/saveLog")

    @Override

    public ApiResult saveLog(@RequestBody Req req) {

         logManager.saveLog(req);

         return ApiResult.success(Boolean.TRUE);

    After结果:

    {

        "success": true,

        "code": "200",

        "msg": "请求成功",

        "traceId": "",

        "data": true

    }

     

    代码执行顺序:

    1号 -》3号 -》2号

    2号中抛出异常也不会对1号有任务影响 

  • 相关阅读:
    Kafka设计原理
    LeetCode_动态规划_中等_313.超级丑数
    linux驱动文件私有数据(字符设备基础二)
    网络安全(黑客)自学笔记
    [React]useEffect中return函数执行时机
    Triloga 任务——Benja 系列
    多维度梳理 MySQL 锁
    富芮坤蓝牙FR801xH GPIO
    C++多线程学习08 使用list和互斥锁进行线程通信
    腾讯云服务器16核 32G 28M带宽租用价格、性能测评及配置大全
  • 原文地址:https://blog.csdn.net/chenwen0326/article/details/126873035