获取独立Apache Camel实例的CamelContext

时间:2016-01-04 16:47:47

标签: apache-camel

我正在使用Apache Camel的Main类来独立运行它。 我需要使用JMS组件,所以我必须将它添加到Main类使用的CamelContext实例。 当然我需要在调用main.run(args)方法之前这样做。

问题是以下...... 使用main.getCamelContexts()。get(0)返回索引超出范围的异常。 使用main.getOrCreateCamelContext()返回一个名为“camel-1”的有效CamelContext实例,我可以添加我的JMS组件,但是......当我执行main.run(args)时,另一个名为“camel-2”的CamelContext “被使用了!

我发现添加我的JMS组件的唯一方法是使用:

main.bind(“jms”,JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));

这是唯一的方法还是CamelContext方式应该有效?

谢谢, 保罗。

2 个答案:

答案 0 :(得分:0)

您可以提供完全自己的驼峰上下文来使用。为此,您可以继承org.apache.camel.main.Main并仅覆盖一个方法

protected Map<String, CamelContext> getCamelContextMap()

以下是继承体的示例:

@Override
protected Map<String, CamelContext> getCamelContextMap() {
    Map<String, CamelContext> camelContextMap = new HashMap<>();
    DefaultCamelContext camelContext = new DefaultCamelContext();
    camelContext.setName("MyContext");

    // Add your context configuration here

    camelContextMap.put("connectorContext", camelContext);
    return camelContextMap;
}

一般来说,最好不要在&#34; getCamelContextMap()&#34;中创建上下文映射。继承的方法,但在构造函数的某处。

答案 1 :(得分:0)

旧问题,但能够弄明白 - 尝试使用camel 2.17.x版本

private void runMyExample() {
    //Add a Main Listener
    main.addMainListener(new MyMainListener);
    main.run();
}

public static class MyMainListener extends MainListenerSupport {
    @Override
    public void afterStart(MainSupport main) {
        System.out.println("MainExample with Camel is now started!");

        //This is the right instance
        CamelContext context = main.getCamelContexts().get(0); 
    }

    @Override
    public void beforeStop(MainSupport main) {
        System.out.println("MainExample with Camel is now being stopped!");
    }
}

此致 R上。

相关问题