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

  • 相关阅读:
    Python单例模式(3种常用方式)
    Apple:万亿收入指日可待
    申报绿色工厂的流程、费用和资料大全。
    状态管理 Pinia
    基本算法-冒泡排序
    Delphi中关于PChar、Char数组、string[](ShortString)及结构体长度及占用空间的一些特性说明和测试
    数字IC/FPGA——锁存器/触发器/寄存器
    数据结构~~~~ [队列] ~~~~
    简单对比一下 C 与 Go 两种语言
    ubuntu VNC 配置
  • 原文地址:https://blog.csdn.net/chenwen0326/article/details/126873035