如何使用主类独立的事件通知添加到骆驼 [英] How to add an EventNotifier to Camel using Main class standalone

查看:202
本文介绍了如何使用主类独立的事件通知添加到骆驼的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个事件通知器添加到使用主类从骆驼我的Apache的骆驼独立的应用程序。

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();
}

我想在这个食谱例如添加一个事件通知如下:

I want to add an EventNotifier like in this cookbook example:

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

下面是我简单的事件通知

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.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()被调用,这种情况下消失(骆驼-1,是唯一的上下文,但是main.run()后,骆驼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 是空的。

我尝试添加的事件通知在另一个线程是由主后产生的背景,但没有显示在我的日志,所以我怀疑事件通知器所需要的背景下开始前以复加。

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());
        }
    };

这篇关于如何使用主类独立的事件通知添加到骆驼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆