3

我正在使用camel-cdi,它正在注入CamelContext,检测项目中的所有路由。但是我想要一个带有注册表的 CamelContext,因为我有一些在骆驼路线中使用的组件,如下所示。

    SimpleRegistry registry = new SimpleRegistry();
    registry.put("actionProcessor", actionProcessor);
    registry.put("jpa", jpaComponent);
    registry.put("jtaTransactionManager", platformTransactionManager);

    CamelContext camelContext = new DefaultCamelContext(registry);

当我注入 CamelContext 时,无法识别 actionProcess、jpa 等组件。在我的路线中,我有

    .to("bean:actionProcessor?method=myMethod(${body})")

但我的 bean 没有被执行。我阅读的文档在注册表中的组件名称之前使用了#,但它仍然无法正常工作。

请建议我如何使用 camel-cdi 实现这一目标。

4

2 回答 2

0

我们多年来一直在使用它,没有任何问题

public class ContextFactory {

    @Produces
    @ApplicationScoped
    @ContextName("Demo")  
    static final CamelContext createContext() {
        CdiCamelContext context = new CdiCamelContext();                
        context.setStreamCaching(true);                
        context.disableJMX();       
        return context;
    }

}   


@ContextName("Demo")
public class MyRouteBuilder extends RouteBuilder {

    from("...")
    .to("bean:actionProcessor?method=myMethod")
}


@Named("actionProcessor")
public class MyActionProcessor{

    public void myMethod(@Body String body) {}
}

当然,为了工作,您需要激活 JEE bean 发现(=在 META-INF 或 WEB-INF 中添加“beans.xml”文件)!

于 2018-06-22T07:30:14.670 回答
0

您是否已经尝试过创建CdiCamelContext(DefaultCamelContext 的子类)?

否则,更优雅的方法是注释您的各种类,例如:

@Named("actionProcessor")
public class MyActionProcessor{
...
}
于 2018-06-21T14:03:10.483 回答