如何使用独立的 Main 类向 Camel 添加 EventNotifier [英] How to add an EventNotifier to Camel using Main class standalone
问题描述
我正在尝试向我的 Apache Camel 独立应用程序添加一个 EventNotifier,该应用程序使用 Camel 的 Main 类.
I'm trying to add an EventNotifier to my Apache Camel standalone application which uses the Main class from Camel.
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:
I want to add an EventNotifier like in this cookbook example:
http://camel.apache.org/eventnotifier-to-log-details-about-all-sent-exchanges.html
这是我的简单 EventNotifier
Here is my simple EventNotifier
@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,我想做一些类似的事情:
Using Java DSL, I want to do something like:
context.getManagementStrategy().addEventNotifier(new MyEventNotifier());
Main 上有一些方法似乎很有用:main.getOrCreateCamelContext() 和 main.getCamelContexts().
There are a few methods on Main that seems like they would be helpful: main.getOrCreateCamelContext() and main.getCamelContexts().
getOrCreateCamelContext 将创建一个上下文,但是当调用 main.run() 时,该上下文消失(Camel-1 是唯一的上下文,但在 main.run() 之后,Camel-2 是唯一的上下文).
getOrCreateCamelContext will create a context but when main.run() is called, that context goes away (Camel-1 and is the only context but after main.run(), Camel-2 is the only context).
getCamelContexts 是空的,直到 main.run() 被调用.
getCamelContexts is empty until main.run() is called.
在 Main 创建上下文后,我尝试在另一个线程中添加 EventNotifier,但我的日志中没有显示任何内容,因此我怀疑需要在上下文启动之前添加 EventNotifier.
I tried adding the EventNotifier in another thread after the context is created by Main but nothing shows in my log so I suspect the EventNotifier needs to be added before the context is started.
有没有简单的方法可以做到这一点?
Is there an easy way to do that?
推荐答案
我找到了一种方法:创建一个匿名内部类并覆盖 postProcessCamelContext 方法.
I found one way: create an anonymous inner class and override the postProcessCamelContext method.
Main main = new Main() {
@Override
protected void postProcessCamelContext(CamelContext camelContext) throws Exception {
super.postProcessCamelContext(camelContext);
camelContext.getManagementStrategy().addEventNotifier(new MyEventNotifier());
}
};
这篇关于如何使用独立的 Main 类向 Camel 添加 EventNotifier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!