Skip to content

Commit b58cc8c

Browse files
lor6KevinGilmore
authored andcommitted
spring performance monitoring interceptors (eugenp#965)
1 parent 3253a25 commit b58cc8c

6 files changed

Lines changed: 164 additions & 1 deletion

File tree

‎aspectj/pom.xml‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,37 @@
4444
<artifactId>junit</artifactId>
4545
<version>${junit.version}</version>
4646
</dependency>
47-
47+
48+
<dependency>
49+
<groupId>org.springframework</groupId>
50+
<artifactId>spring-context</artifactId>
51+
<version>4.3.4.RELEASE</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springframework</groupId>
55+
<artifactId>spring-beans</artifactId>
56+
<version>4.3.4.RELEASE</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.springframework</groupId>
60+
<artifactId>spring-core</artifactId>
61+
<version>4.3.4.RELEASE</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>cglib</groupId>
65+
<artifactId>cglib</artifactId>
66+
<version>3.2.4</version>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.springframework</groupId>
70+
<artifactId>spring-aop</artifactId>
71+
<version>4.3.4.RELEASE</version>
72+
</dependency>
73+
<dependency>
74+
<groupId>log4j</groupId>
75+
<artifactId>log4j</artifactId>
76+
<version>1.2.17</version>
77+
</dependency>
4878
</dependencies>
4979

5080
<build>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.performancemonitor;
2+
3+
import org.aspectj.lang.annotation.Aspect;
4+
import org.aspectj.lang.annotation.Pointcut;
5+
import org.springframework.aop.Advisor;
6+
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
7+
import org.springframework.aop.interceptor.PerformanceMonitorInterceptor;
8+
import org.springframework.aop.support.DefaultPointcutAdvisor;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
12+
13+
@Configuration
14+
@EnableAspectJAutoProxy
15+
public class AopConfiguration {
16+
17+
@Pointcut("execution(public int com.baeldung.performancemonitor.Person.getAge())")
18+
public void monitor() { }
19+
20+
@Pointcut("execution(public void com.baeldung.performancemonitor.Person.setAge(int))")
21+
public void myMonitor() { }
22+
23+
@Bean
24+
public PerformanceMonitorInterceptor performanceMonitorInterceptor() {
25+
return new PerformanceMonitorInterceptor(true);
26+
}
27+
28+
@Bean
29+
public Advisor performanceMonitorAdvisor() {
30+
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
31+
pointcut.setExpression("com.baeldung.performancemonitor.AopConfiguration.monitor()");
32+
return new DefaultPointcutAdvisor(pointcut, performanceMonitorInterceptor());
33+
}
34+
35+
@Bean
36+
public Person person(){
37+
return new Person();
38+
}
39+
40+
@Bean
41+
public MyPerformanceMonitorInterceptor myPerformanceMonitorInterceptor() {
42+
return new MyPerformanceMonitorInterceptor(true);
43+
}
44+
45+
@Bean
46+
public Advisor myPerformanceMonitorAdvisor() {
47+
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
48+
pointcut.setExpression("com.baeldung.performancemonitor.AopConfiguration.myMonitor()");
49+
return new DefaultPointcutAdvisor(pointcut, myPerformanceMonitorInterceptor());
50+
}
51+
52+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.performancemonitor;
2+
3+
import java.util.Date;
4+
5+
import org.aopalliance.intercept.MethodInvocation;
6+
import org.apache.commons.logging.Log;
7+
import org.apache.log4j.Logger;
8+
import org.aspectj.lang.annotation.Aspect;
9+
import org.springframework.aop.interceptor.AbstractMonitoringInterceptor;
10+
import org.springframework.util.StopWatch;
11+
12+
public class MyPerformanceMonitorInterceptor extends AbstractMonitoringInterceptor {
13+
14+
public MyPerformanceMonitorInterceptor() {
15+
}
16+
17+
public MyPerformanceMonitorInterceptor(boolean useDynamicLogger) {
18+
setUseDynamicLogger(useDynamicLogger);
19+
}
20+
21+
@Override
22+
protected Object invokeUnderTrace(MethodInvocation invocation, Log log) throws Throwable {
23+
24+
String name = createInvocationTraceName(invocation);
25+
long start = System.currentTimeMillis();
26+
log.info("Method "+name+" execution started at:"+new Date());
27+
try {
28+
return invocation.proceed();
29+
}
30+
finally {
31+
long end = System.currentTimeMillis();
32+
long time = end - start;
33+
log.info("Method "+name+" execution lasted:"+time+" ms");
34+
log.info("Method "+name+" execution ended at:"+new Date());
35+
36+
if (time > 10){
37+
log.warn("Method execution longer than 10 ms!");
38+
}
39+
40+
}
41+
}
42+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.performancemonitor;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5+
6+
public class PerfomanceApp {
7+
public static void main(String[] args) {
8+
9+
ApplicationContext context = new AnnotationConfigApplicationContext(AopConfiguration.class);
10+
Person person = (Person) context.getBean("person");
11+
12+
person.setAge(20);
13+
System.out.println("Age is:"+person.getAge());
14+
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.performancemonitor;
2+
3+
public class Person {
4+
private int age;
5+
6+
public int getAge() {
7+
return age;
8+
}
9+
10+
public void setAge(int age) {
11+
this.age = age;
12+
}
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
log4j.rootLogger=TRACE, stdout
2+
3+
# Redirect log messages to console
4+
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
5+
log4j.appender.stdout.Target=System.out
6+
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
7+
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
8+
9+
log4j.logger.org.springframework.aop.interceptor.PerformanceMonitorInterceptor=TRACE, stdout
10+
log4j.logger.com.baeldung.performancemonitor.MyPerformanceMonitorInterceptor=INFO, stdout

0 commit comments

Comments
 (0)