【问题标题】:How to add an EventNotifier to Camel using Main class standalone如何使用独立的 Main 类向 Camel 添加 EventNotifier
【发布时间】:2015-05-22 02:38:38
【问题描述】:

我正在尝试将 EventNotifier 添加到我的 Apache Camel 独立应用程序中,该应用程序使用来自 Camel 的 Main 类。

http://camel.apache.org/running-camel-standalone-and-have-it-keep-running.html

public void boot() throws Exception {
    Main main = new Main();
    main.addRouteBuilder(new MyRouteBuilder());
    System.out.println("Starting Camel. Use ctrl + c to terminate the JVM.\n");
    main.run();
}

我想像这个食谱示例一样添加一个 EventNotifier:

http://camel.apache.org/eventnotifier-to-log-details-about-all-sent-exchanges.html

这是我的简单事件通知器

@Override
public void notify(EventObject event) throws Exception {
    logger.info(event.toString());
}

@Override
public boolean isEnabled(EventObject event) {
    logger.info("Checked if enabled");
    return true;
}

使用 Java DSL,我想做类似的事情:

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

Main 上有一些方法似乎很有帮助:main.getOrCreateCamelContext() 和 main.getCamelContexts()。

getOrCreateCamelContext 将创建一个上下文,但当 main.run() 被调用时,该上下文消失(Camel-1 是唯一的上下文,但在 main.run() 之后,Camel-2 是唯一的上下文)。

getCamelContexts 在调用 main.run() 之前为空。

我尝试在 Main 创建上下文后在另一个线程中添加 EventNotifier,但我的日志中没有显示任何内容,因此我怀疑需要在启动上下文之前添加 EventNotifier。

有没有简单的方法可以做到这一点?

【问题讨论】:

    标签: apache-camel


    【解决方案1】:

    我找到了一种方法:创建一个匿名内部类并覆盖 postProcessCamelContext 方法。

        Main main = new Main() {
            @Override
            protected void postProcessCamelContext(CamelContext camelContext) throws Exception {
                super.postProcessCamelContext(camelContext);
                camelContext.getManagementStrategy().addEventNotifier(new MyEventNotifier());
            }
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      相关资源
      最近更新 更多