Spring Boot 和 Camel- 日志路由持续时间

Spring Boot and Camel- Log Route Duration

我正在迁移应用程序以使用最新版本的 Spring Boot. 目前所有的骆驼路线都在 XML 中,我使用这种方法 运行。

当前所有路由在处理结束时记录一条消息。 我想知道是否有一种方法可以检测特定路由执行并添加到日志消息中需要多长时间。 有了这些信息,我们就可以创建数据狗仪表板来显示我们骆驼路线的统计数据

提前致谢 达米安

public class MyLoggingSentEventNotifer extends EventNotifierSupport {

  public void notify(EventObject event) throws Exception {

    if (event instanceof ExchangeCompletedEvent) {;
      ExchangeCompletedEvent completedEvent = (ExchangeCompletedEvent) event;
      Exchange exchange = completedEvent.getExchange();
      String routeId = exchange.getFromRouteId();
      Date created = ((ExchangeCompletedEvent) event).getExchange()
                        .getProperty(Exchange.CREATED_TIMESTAMP, Date.class);
      // calculate elapsed time
      Date now = new Date();
      long elapsed = now.getTime() - created.getTime();
      log.info("Took " + elapsed + " millis on the route : " + routeId);
    }

 }

 public boolean isEnabled(EventObject event) {
        // we only want the sent events
        return event instanceof ExchangeSentEvent;
 }

 protected void doStart() throws Exception {
        // noop
 }

 protected void doStop() throws Exception {
        // noop
 }

}

context.getManagementStrategy().addEventNotifier(new MyLoggingSentEventNotifer());

参考

https://people.apache.org/~dkulp/camel/eventnotifier-to-log-details-about-all-sent-exchanges.html

更新

Exchange.CREATED_TIMESTAMP 不再存储为 exchange 属性,但您应该在 Exchange 上使用 getCreated 方法。