I have below Java code which intercepts the method getUserDetails()
using Spring AOP and logs its execution time. The method calculateExecutionTime
gets executed in the same thread as getUserDetails()
.
package com.poc.app.data;
import org.springframework.stereotype.Repository;
@Repository
public class User {
public String getUserDetails() {
return "user details";
}
}
package com.poc.app.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
@Aspect
@Configuration
public class ExecutionTimeAspect {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Around("execution(* com.poc.app..*.*(..))")
public Object calculateExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object returnValue = joinPoint.proceed();
long timeTaken = System.currentTimeMillis() - startTime;
logger.info("Time taken by {} to complete execution is: {}", joinPoint, timeTaken);
return returnValue;
}
}
Question: I am working on creating another Aspect to capture runtime data of various methods (like the input parameter values, the return values) using @Before and @After Spring AOP advices.
- Is it possible to run those advices in a separate thread then the
one running the actual methods say
getUserDetails
in this example? - Is it possible to create a custom advice types similar to @Before and @After and have them run in separate thread then the method being intercepted?