简体   繁体   English

如何使用Apache Camel测量经过时间

[英]How to measure elapsed time with Apache Camel

I need to measure how much time it takes to process external service response in Apache Camel. 我需要测量在Apache Camel中处理外部服务响应所花费的时间。

I've search the web for solutions and surprisingly there was no acceptable one. 我在网上搜索解决方案,令人惊讶的是没有一个可接受的解决方案。

Using an EventNotifier feels too heavy weight and more framework level performance measurement tool. 使用EventNotifier会使您感到负担沉重,并且需要更多框架级别的性能衡量工具。

So I've made my own simple solution, but I'm hoping for something better out there I've missed or at least a constructive feedback. 因此,我制定了自己的简单解决方案,但我希望能得到一些我错过的更好的东西,或者至少是建设性的反馈。

A solution attempt below. 下面的解决方案尝试。

I think that camel do this by default. 我认为骆驼默认情况下会这样做。

Available as of Camel 2.12 Message History is enabled by default from Camel 2.12. 从骆驼2.12开始,默认情况下从骆驼2.12启用消息历史记录。 During routing Camel captures how the Exchange is routed, as a org.apache.camel.MessageHistory entity that is stored on the Exchange. 在路由过程中,骆驼捕获了如何路由Exchange,作为存储在Exchange上的org.apache.camel.MessageHistory实体。 On the org.apache.camel.MessageHistory there is information abut the route id, processor id, timestamp, and elapsed time it took to process the Exchange by the processor. 在org.apache.camel.MessageHistory上,有关于路由ID,处理器ID,时间戳和处理器处理Exchange所用时间的信息。

Only that you have to do is get the MessageHistory in your "doFinally" code: 只需要做的就是在“ doFinally”代码中获取MessageHistory:

List<MessageHistory> list = exchange.getProperty(Exchange.MESSAGE_HISTORY,List.class);

There you can get the the elapsed time for each route 在那里,您可以获取每条路线的经过时间

This is how my business logic looks like: 这就是我的业务逻辑:

from(incomingUri).routeId(ROUTE_ID)
            ...
            .doTry()
                .bean(stopwatch, "start")
                .to(externalService)
            .doCatch(NoHttpResponseException.class, ProtocolException.class, IOException.class)
                .process(externalServiceExceptionProcessor(ERROR_MESSAGE_SERVICE_NOT_RESPONDING))
            .doFinally()
                .bean(stopwatch, "stop")
            .end()
            ...

The Stopwatch bean instantition: Stopwatch bean实例:

StopWatchBean stopwatch = new StopWatchBean(new ExchangeConsumer<Stopwatch>() {
        @Override
        public void accept(Exchange exchange, Stopwatch stopwatch) {
            Long taken = stopwatch.elapsed(MILLISECONDS);
            exchange.setProperty(RESPONSE_TIME, constant(taken));
        }
});

and class definition: 和类定义:

public class StopWatchBean {

    private final Stopwatch stopwatch;
    private final ExchangeConsumer<Stopwatch> onStopFunction;

    public StopWatchBean(ExchangeConsumer<Stopwatch> onStopFunction) {
        this.stopwatch = Stopwatch.createUnstarted();
        this.onStopFunction = onStopFunction;
    }

    public void stop(Exchange exchange) {
        if (!stopwatch.isRunning()) {
            return;
        }
        stopwatch.stop();
        onStopFunction.accept(exchange, stopwatch);
    }

    public void start(Exchange unused) {
        stopwatch.start();
    }

    public void reset(Exchange unused) {
        stopwatch.reset();
    }

}

The Stopwatch is from Guava and the Consumer is just a custom functional interface. Stopwatch来自番石榴,而使用者只是一个自定义功能界面。

Waiting for comments. 等待评论。

EDIT: I've added some simple code, like: 编辑:我添加了一些简单的代码,例如:

started = System.currentTimeMillis();
...
stopped = System.currentTimeMillis();

elapsed = stopped - started;

and made some measurements, on normal case looks good: 并进行了一些测量,在正常情况下看起来不错:

[xxxxxxxxxProxy    ] [stopWatchStart    ] [bean[StopWatchBean{elapsed=100, stopwatch=100.8 ms}]                          ] [         1]
[xxxxxxxxxProxy    ] [toXxxxxxxxxx      ] [https://127.0.0.1:5680/xxxxxxxxx?throwExceptionOnFailure=false&bridgeEndpoint=] [       100]
[xxxxxxxxxProxy    ] [stopWatchStop     ] [bean[StopWatchBean{elapsed=100, stopwatch=100.8 ms}]                          ] [         3]

and on error/exception is different: 并且在错误/异常上有所不同:

[xxxxxxxxxProxy    ] [stopWatchStart    ] [bean[StopWatchBean{elapsed=38, stopwatch=344.1 ms}]                           ] [         1]
[xxxxxxxxxProxy    ] [toXxxxxxxxxx      ] [https://127.0.0.1:5680/xxxxxxxxx?throwExceptionOnFailure=false&bridgeEndpoint=] [        37]
[xxxxxxxxxProxy    ] [stopWatchStop     ] [bean[StopWatchBean{elapsed=38, stopwatch=344.1 ms}]                           ] [         1]

I'm puzzled, why the difference, 38 vs 344, could it be due to Camel not taking Exception handling into account? 我很困惑,为什么差异38和344可能是由于Camel没有考虑到异常处理?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM