• @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号有任务影响 

  • 相关阅读:
    Spring(一)
    动态内存管理
    某城商银行生产容器云平台详细设计方案分享及经验总结
    Vue零基础实战教程
    可口可乐用新的“Y3000”口味拥抱有争议的人工智能图像生成器
    qemu搭建arm嵌入式linux开发环境
    go调用阿里云接口查询ECS信息
    大模型架构创新已死?
    MySQL体系-日志与MVCC(源码层面)
    多角度了解ABeam(德硕)技术架构
  • 原文地址:https://blog.csdn.net/chenwen0326/article/details/126873035