问题描述
我正在尝试使用 Micrometer 在我的 Java 应用程序中记录执行时间。这与我关于使用的 @Timed
注释的其他 question 相关。
我有一个 CountedObject
类,它有以下 2 个方法:
@Measured
@Timed(value = "timer1")
public void measuredFunction() {
try {
int sleepTime = new Random().nextInt(3) + 1;
Thread.sleep(sleepTime * 1000L);
} catch (InterruptedException e) {}
}
@Timed(value = "timer2")
public void timedFunction() {
try {
int sleepTime = new Random().nextInt(3) + 1;
Thread.sleep(sleepTime * 1000L);
} catch (InterruptedException e) {}
}
我定义了一个自定义注解 @Measured
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Measured {
}
还有一个 MeasuredAspect
来拦截对用我的 @Measured
注释注释的方法的调用:
@Aspect
public class MeasuredAspect {
@Around("execution(* *(..)) && @annotation(Measured)")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
return AppMetrics.getInstance().handle(pjp);
}
}
在我的 AppMetrics
类中,我初始化了一个千分尺的 TimedAspect 实例,并在 handle(ProceedingJoinPoint pjp)
方法中将 ProceedingJoinPoint pjp
传递给 TimedAspect 实例。
public class AppMetrics {
private static final AppMetrics instance = new AppMetrics();
private MeterRegistry registry;
private TimedAspect timedAspect;
public static AppMetrics getInstance() {
return instance;
}
private AppMetrics() {
this.registry = new SimpleMeterRegistry();
this.timedAspect = new TimedAspect(registry);
}
public Object handle(ProceedingJoinPoint pjp) throws Throwable {
return timedAspect.timedMethod(pjp);
}
}
在我的应用程序主程序中,我创建了一个 CountedObject
对象并调用了 measuredFunction()
和 timedFunction()
然后我检查了我的 registry.getMeters();
只使用了 timer1由measuredFunction()
[由@Measured 和@Timed 注释] 找到,而应由timedFunction()
使用的timer2 [仅由{{3} 注释}] 不存在。
我将 eclipse 与 @Timed 一起使用,我的项目是具有 AspectJ 功能的 Gradle 项目。我在 Gradle 插件中使用了 id "io.freefair.aspectj" version "5.1.1"
插件。这是一个基本的 Java 应用程序,而不是 Spring 应用程序。
需要做哪些配置或者需要修改哪些代码才能让千分尺AspectJ Development Tools Plugin可以直接拦截我的方法调用【即timedFunction()
应该是定时的,timer2应该是在注册表中找到]而不需要我的自定义注释?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)