PerformanceMonitorInterceptor 

Let's see how to perform profiling or monitoring on our method execution. This is done with the help of a simple option provided by Spring AOP using the PerformanceMonitorInterceptor class.

As we have learned, Spring AOP allows the defining of crosscutting concerns in applications by intercepting the execution of one or more methods to add extra functionality without touching the core business classes.

The PerformanceMonitorInterceptor class from Spring AOP is an interceptor that can be tied to any custom method to be executed at the same time. This class uses a StopWatch instance to log the beginning and ending time of the method execution.

Let's monitor the transfer method of TransferService. The following is the TransferService code:

public class TransferServiceImpl implements TransferService {

private static final Logger LOGGER =
LogManager.getLogger(TransferServiceImpl.class);

@Override
public boolean transfer(Account source, Account dest, int amount) {
// transfer amount from source account to dest account
LOGGER.info("Transferring " + amount + " from " +
source.getAccountName() + "
to " + dest.getAccountName());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
LOGGER.error(e);
}
return true;
}
}

The following code is @Pointcut to monitor the advice method using Spring interceptor:

@Aspect 
public class TransferMonitoringAspect {

@Pointcut("execution(*
com.packt.springhighperformance.ch03.bankingapp.service
.TransferService.transfer(..))")
public void transfer() { }
}

The following code is the advisor class:

public class PerformanceMonitorAdvisor extends DefaultPointcutAdvisor {

private static final long serialVersionUID = -3049371771366224728L;

public PerformanceMonitorAdvisor(PerformanceMonitorInterceptor
performanceMonitorInterceptor) {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression(
"com.packt.springhighperformance.ch03.bankingapp.aspect.TransferMonito ringAspect.transfer()");
this.setPointcut(pointcut);
this.setAdvice(performanceMonitorInterceptor);
}
}

The following code is the Spring Java configuration class:

@EnableAspectJAutoProxy
@Configuration
public class PerformanceInterceptorAppConfig {
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}

@Bean
public PerformanceMonitorInterceptor performanceMonitorInterceptor() {
return new PerformanceMonitorInterceptor(true);
}

@Bean
public TransferMonitoringAspect transferAspect() {
return new TransferMonitoringAspect();
}

@Bean
public PerformanceMonitorAdvisor performanceMonitorAdvisor() {
return new
PerformanceMonitorAdvisor(performanceMonitorInterceptor());
}
}

The expression in pointcut identifies the methods that we want to intercept. We have defined PerformanceMonitorInterceptor as a bean and then created PerformanceMonitorAdvisor to associate pointcut with the interceptor.

In our Appconfig, we have annotated with the @EnableAspectJAutoProxy annotation to enable AspectJ support for our beans to create a proxy automatically. 

To have PerformanceMonitorInterceptor work, we need to set the log level of the target object, TransferServiceImpl, to the TRACE level as this is the level at which it logs messages.

For every execution of the transfer method, we will see the TRACE message in the console log:

2018-02-07 22:14:53 TRACE TransferServiceImpl:222 - StopWatch 'com.packt.springhighperformance.ch03.bankingapp.service.TransferService.transfer': running time (millis) = 5000
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset