在Spring Boot切面中的延迟导致连接点方法的延迟。

huangapple 未分类评论56阅读模式
英文:

Delay in spring boot aspect cause to delay in joinpoint method

问题

我有一个简单的切面,如下所示:

@Aspect
@Component
public class ApiCallLogAspect {

    @AfterReturning(pointcut = "execution(* com.example.web.rest.api.ApiResource.getAddress(String)) && args(mobile)", argNames = "mobile")
    public void endpointAfterReturning(String mobile) throws InterruptedException {
        Thread.sleep(10000);
        System.out.println("This is aspect log.");
    }
}

这是ApiResource类中的getAddress方法,正如您所看到的,这是一个非常简单且快速的方法:

@RestController
@RequestMapping("/api/v1")
public class ApiResource {

    public String getAddress(String mobile) {
        return "This is your address";
    }
}

现在,当我调用getAddress方法时,切面中的Thread.sleep(10000)会导致getAddress方法的执行延迟。

我如何将切面的执行与连接点方法分开?

注意

我使用了Thread.sleep(10000)来演示我的问题。我想在方法执行后执行一些操作,而且不想影响主方法,无论是延迟、异常还是其他任何情况。为了达到这个目的,我应该使用什么?

更新

我的目标是记录对REST API的请求并存储调用详细信息,例如IP、参数值、请求头和返回值。

英文:

I have a simple aspect as you can see below:

@Aspect
@Component
public class ApiCallLogAspect {

    @AfterReturning(pointcut = "execution(* com.example.web.rest.api.ApiResource.getAddress (String)) && args(mobile))", argNames = "mobile")
        public void endpointAfterReturning(String mobile) throws InterruptedException {
            Thread.sleep(10000);
            System.out.println("This is aspect log.");
        }
    }
}

And this is getAddress method in ApiResource class, as you see this is very simple and fast method:

@RestController
@RequestMapping("/api/v1")
public class ApiResource {

    public String getAddress(String mobile) {
        return "This is your address";
    }
}

Now, when i call getAddress method the Thread.sleep(10000) in aspect cause to delay in execution of getAddress method.

How can i seprate execution of aspect from joinpoint method?

NOTE

I used Thread.sleep(10000) to demonstrate my problem. I want to do something after a method executed, and don't want to affect on main method, either delay, exception or anything. what should i use to do for this purpose?

UPDATE

My goal is to log requests to my rest apis and store call details, such as ip, arguments values, request headers and returned value.

huangapple
  • 本文由 发表于 2020年7月27日 20:10:20
  • 转载请务必保留本文链接:https://java.coder-hub.com/63115037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定