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

标签 java aspectj nio vert.x

我正在尝试实现一个方面来为 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/

相关文章:

java - 使用Java NIO 10000个并发连接

java - SocketChannel 如何知道文件读取完成?

java - JScrollPane 中的 JTable 具有可调整大小的 JFrame 固定大小?

spring - Maven、Scala、Spring、AspectJ

java - 避免 AOP 中重叠的切入点和切面

java - 是否存在类似 Babel 的 Java 编译器?

java - 如何访问 Java 7 java.nio.file.Path 中的子文件/文件夹?

java - 在 JUnit 切换到 TestNG 后我的测试不起作用

java - 从 org.w3c.dom.Node 获取 Xpath

java - Java 方法操作数栈