骆驼如何添加一些东西到注册表 - 一般用java

时间:2016-11-09 09:07:12

标签: apache-camel

有时我必须向camel注册表添加一个对象(当然还有java)。在大多数情况下,它是一个dataSource。

我的问题是我无法找到一般的工作方式。

我总是开始获得注册表:

getContext().getRegistry();

但"注册表"没有任何方法来添加对象。所以我必须尝试(使用调试器)正在使用哪种注册表

getContext().getRegistry(some.class)<some method to add something>;

例如,在一个项目(骆驼蓝图)中,我必须致电

SimpleRegistry registry = new SimpleRegistry();
registry.put("some", bean);
getContext().getRegistry(CompositeRegistry.class).addRegistry(registry);

现在我创建了一个具有相同结构的项目(也是相同的maven父项),但现在上面的代码停止工作,因为由于某种原因现在camel使用PropertyPlaceholderDelegateRegistry我确信会有代码添加我的bean但是;

是否有适用于每个设置的代码可以向camels注册表添加内容?

4 个答案:

答案 0 :(得分:7)

以下是在RouteBuilder类中向注册表添加内容的一种方法。下面我将添加一个TCPServerInitializerFactory,稍后将使用它。我总是使用camel-blueprint原型,但是使用java dsl创建路由。这对我来说很好。

TCPServerInitializerFactory serverFactory = new TCPServerInitializerFactory(null);
final CamelContext camelContext = getContext();
        final org.apache.camel.impl.SimpleRegistry registry = new org.apache.camel.impl.SimpleRegistry();
        final org.apache.camel.impl.CompositeRegistry compositeRegistry = new org.apache.camel.impl.CompositeRegistry();
        compositeRegistry.addRegistry(camelContext.getRegistry());
        compositeRegistry.addRegistry(registry);
        ((org.apache.camel.impl.DefaultCamelContext) camelContext).setRegistry(compositeRegistry);
    registry.put("spf", serverFactory);

答案 1 :(得分:0)

如果您使用的是camel-spring包,则可以使用Spring来管理注册表。这是我用来通过Spring在Java中添加单例bean的代码示例:

MyApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://camel.apache.org/schema/spring       http://camel.apache.org/schema/spring/camel-spring.xsd">

    <bean id="myRouteBuilder" class="com.example.MyRouteBuilder"/>

    <camel:camelContext id="my-camel-context">
        <camel:routeBuilder ref="myRouteBuilder"/>
    </camel:camelContext>
</beans>

MyApplication.java

public class MyApplication {
    public static void main(final String[] args) {
        final FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("/path/to/MyApplicationContext.xml");
    }
}

MyRouteBuilder.java

public void MyRouteBuilder extends RouteBuilder implements ApplicationContextAware {
    @Override
    public void configure() {
        from("ftps://localhost:21/foo?pollStrategy=#myPollingStrategy")
            .log("${body}");
    }

    @Override
    public void setApplicationContext(final ApplicationContext applicationContext) {
        final ConfigurableApplicationContext context  = (ConfigurableApplicationContext)applicationContext;
        final DefaultListableBeanFactory     registry = (DefaultListableBeanFactory)context.getBeanFactory();
        registry.registerSingleton("myPollingStrategy", new MyPollingStrategy());
    }
}

这里的重要部分是在您要用来手动将bean添加到注册表中的类中实现ApplicationContextAware接口。这个接口可以在Spring初始化的任何bean上实现。您不必使用RouteBuilder。在这种情况下,将在调用setApplicationContext(...)之前在configure()执行期间的启动过程中创建Bean,但是您也可以存储应用程序上下文以供以后使用(这可能是更常见的用法)。

这已通过骆驼2.19.0和春季4.3.10-RELEASE进行了测试。

答案 2 :(得分:0)

如果您将Spring与骆驼一起使用,则可以在Application上下文中注册您的bean,它将在ApplicationContextRegistry中注册。

例如ConfigurableListableBeanFactory beanFactory =(((ConfigurableApplicationContext) applicationContext).getBeanFactory(); beanFactory.registerSingleton(“ beantolookup”,bean);

您可以查找 camelContext.getRegistry()。lookupByName(“ beantolookup”)

答案 3 :(得分:0)

对于 Camel 3.x,使用代码片段在运行时添加到注册表中

DefaultRegistry registry = (DefaultRegistry) camelContext.getRegistry();
registry.bind("key", <Object to bind>);
((org.apache.camel.impl.DefaultCamelContext) camelContext).setRegistry(registry);

这个可以进一步参考, enter link description here