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

aspectj timer for non blocking IO calls

我正在尝试为 NIO 实现时间方法执行方面。

基本上,我需要获取最初调用方法的时间以及触发回调的时间(回调始终是 failed() 或 succeeded() 调用 - 所以我需要关联初始方法使用适当的回调调用。

我有以下实现,但它不会工作,除非连续调用,当然不会是这种情况。

任何帮助都会很棒

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() 发送 start/stop 指标。使用另一个 Verticle 通过 vertx.eventBus().consumer() 收集指标并显示它们。

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