2

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.

Rasool Ghafari
  • 4,128
  • 7
  • 44
  • 71
  • 1
    Why would you think you can separate them? The whole idea of AOP is that they're intertwined. The reason it takes so long is because you've put a 10 second sleep in there, so don't do that. The question doesn't even seem to be related to the title. – Kayaman Jul 27 '20 at 12:16
  • @Kayaman updated the title, i can't? 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? – Rasool Ghafari Jul 27 '20 at 12:18
  • So do you want to execute them [asynchronously](https://stackoverflow.com/questions/58275121/will-an-aspect-be-executed-asynchronously-if-i-put-async-method-on-it)? – Kayaman Jul 27 '20 at 12:24
  • Maybe, also i want to use returned value of executed method and its arguments values. – Rasool Ghafari Jul 27 '20 at 12:27
  • @Kayaman please check my update – Rasool Ghafari Jul 27 '20 at 12:29
  • So you don't actually have any problem. You just wish you could execute your logging code without it taking any time. Well, if executing code didn't take any time, all software would be a lot more efficient. – Kayaman Jul 27 '20 at 12:34
  • What can i do if unknown exception occurred in aspect? – Rasool Ghafari Jul 27 '20 at 12:39
  • Catch it and log it and either ignore or rethrow it? Why are you asking me, you're the one who knows what the software should do. – Kayaman Jul 27 '20 at 12:40
  • I'm sorry i think all of my problem was solve by using `@EnableAsync` + `@Async` over aspect method, thank you. :) – Rasool Ghafari Jul 27 '20 at 12:42
  • @Kayaman Oops, i using `HttpServletRequest` to get request headers in the aspect and when added async annotaions i get this error: **java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.** – Rasool Ghafari Jul 27 '20 at 12:47
  • Are you really asking a question about code that is different from the one you actually provided in your question? And is this problem not worth a new question? I suggest you first write a comprehensive answer (not just many comments) about how you solved the original problem via `@Async` and then ask a new question specific to your trying to do something with the HTTP request in your advice in the asynchronous situation you created, the new question linking back at this one. – kriegaex Jul 29 '20 at 04:04
  • BTW, if anyone reads this and is interested in pitfalls concerning `@Async` in connection with `@Around` advices, I wrote something about it [here](https://stackoverflow.com/a/58279169/1082681). But you are using `@AfterReturning`, so you are not affected by that. Your problem is different, trying to use an HTTP request outside the defined thread in which it is meant to be processed. – kriegaex Jul 29 '20 at 04:10

0 Answers0