【问题标题】:Spring Boot and Camel- Log Route DurationSpring Boot 和 Camel- 日志路由持续时间
【发布时间】:2020-10-30 05:45:54
【问题描述】:

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

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

提前致谢 达米安

【问题讨论】:

    标签: java spring-boot apache-camel spring-camel


    【解决方案1】:
    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 上使用getCreated 方法。

    【讨论】:

    • 你知道这是否可以在不使用自定义 EventNotifier 类的情况下在骆驼 xml 中完成?
    • 两者都需要EventNotifier。如果不创建事件通知器,我就看不到机制
    • 字段 Exchange.CREATED_TIMESTAMP 已弃用。你有什么建议用它代替它的地方?
    猜你喜欢
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多