gpt4 book ai didi

java - 用于非阻塞IO调用的aspectj计时器

转载 作者:太空宇宙 更新时间:2023-11-04 12:20:48 25 4
gpt4 key购买 nike

我正在尝试实现一个方面来为 NIO 的方法执行计时。

基本上,我需要获取最初调用方法的时间,然后触发回调的时间(回调始终是 failed() 或 successed() 调用 - 因此我需要将初始方法调用与适当的回调相关联。

我有以下实现,但除非连续调用,否则它将无法工作,当然情况并非如此。

任何帮助都会很棒

public aspect TimerAJ {
private long timer = 0;
private String methodName;
private static Logger logger = LoggerFactory.getLogger(TimerAJ.class);

//the call to the call back method
pointcut timerCall1() : target(io.vertx.core.AsyncResult) && (call( * io.vertx.core.AsyncResult.failed(..)) || call( * io.vertx.core.AsyncResult.succeeded(..)));

//the call to the method itself
pointcut timerCall2() : execution(@Timer * *(..));

before() : timerCall2() {
timer = System.currentTimeMillis();
methodName = methodSignature.getName();
}
after() : timerCall1() {
logger.info(methodName + " " + String.format("%s took %d ms", thisEnclosingJoinPointStaticPart.getSourceLocation().getWithinType().getName(), (System.currentTimeMillis() - timer)));
}
}

最佳答案

您需要重新考虑这种方法,因为它根本不适用于 Vert.x。

您尝试做的是在调用 @Timer 方法时开始计数,并在返回 any AsyncResult 时停止计数。

您可以做什么:使用 vertx.eventBus().publish() 发送开始/停止指标。使用另一个 Verticle 通过 vertx.eventBus().consumer() 收集指标并显示它们。

另外,请查看Vert.x Dropwizard Metrics 。如果您想要的只是实际统计数据,他们会为您提供,而无需您进行太多实现。

关于java - 用于非阻塞IO调用的aspectj计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38874388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com