Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache camel 获取独立Apache Camel实例的CamelContext_Apache Camel - Fatal编程技术网

Apache camel 获取独立Apache Camel实例的CamelContext

Apache camel 获取独立Apache Camel实例的CamelContext,apache-camel,Apache Camel,我使用ApacheCamel中的主类作为独立类运行它。 我需要使用JMS组件,因此我必须将其添加到主类使用的CamelContext实例中。 当然,在调用main.run(args)方法之前,我需要这样做 问题如下。。。 使用main.getCamelContexts().get(0)返回索引越界异常。 使用main.getOrCreateCamelContext()返回一个名为“camel-1”的有效CamelContext实例,我可以将JMS组件添加到该实例中,但。。。。当我执行main.r

我使用ApacheCamel中的主类作为独立类运行它。 我需要使用JMS组件,因此我必须将其添加到主类使用的CamelContext实例中。 当然,在调用main.run(args)方法之前,我需要这样做

问题如下。。。 使用main.getCamelContexts().get(0)返回索引越界异常。 使用main.getOrCreateCamelContext()返回一个名为“camel-1”的有效CamelContext实例,我可以将JMS组件添加到该实例中,但。。。。当我执行main.run(args)时,会使用另一个名为“camel-2”的CamelContext

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

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

这是唯一可行的方法还是唯一可行的方法

谢谢,
Paolo.

您可以提供完全自己的骆驼上下文供使用。为了实现这一点,您可以继承org.apache.camel.main.main并只重写一个方法

protected Map<String, CamelContext> getCamelContextMap()
protectedmap 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;
}
@覆盖
受保护的映射getCamelContextMap(){
Map camelContextMap=新HashMap();
DefaultCamelContext camelContext=新的DefaultCamelContext();
camelContext.setName(“MyContext”);
//在此处添加您的上下文配置
camelContextMap.put(“connectorContext”,camelContext);
返回camelContextMap;
}

一般来说,最好不要在继承的“getCamelContextMap()”方法中创建上下文映射,而是在构造函数中的某个位置创建上下文映射。

老问题,但能够解决它-在Camel2.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.

您是否知道Camel主类的Spring变体:
org.apache.Camel.Spring.Main
Camel-Spring.jar
中?有了它,您可以使用Spring上下文来实例化组件、自定义处理器等。无需以编程方式填充Camel上下文。