我使用的是 akka-camel 2.2.1,需要分别配置往返于 Consumer 和 Producer Actor 的路由。我目前正在定义路由并以编程方式将它们添加到 CamelExtension 内的内部 Camel 上下文中,如下所示:
camel.context.addRoutes(new RouteBuilder {
def configure() = {
from("file:/tmp?include=whatever.*.log&noop=true&sendEmptyMessageWhenIdle=true")
.split(body(classOf[String]).tokenize("\n"))
.streaming()
.to("seda:input-route")
from("seda:update-route")
.to("bean:db-update-bean?method=process")
}})
我有一个扩展 Consumer 的 Actor,它通过其 endPointUri 从“seda:input-route”读取,还有另一个扩展 Producer 的 Actor,写入“seda:update-route”。 “db-update-bean”在 Spring applicationContext.xml 中定义,如下所示:
<bean id="db-update-bean" class="nz.co.whatever.DBUpdate">
<constructor-arg ref="jdbcTemplate"/>
<constructor-arg value="some_other_constructor_arg"/>
</bean>
Spring 上下文在由 akka.Main 启动的监督 Actor 中加载并启动。然而(这是可以理解的),Camel 不知道这个 Spring 上下文,因此竭尽全力告诉我它不知道“db-update-bean”是什么:
2013-10-11 08:55:09,614 [SedaConsumer ] WARN 处理交换时出错。交换[消息:1378642997698,27684,true,57.000000,0.750000,97]。造成原因:
[org.apache.camel.NoSuchBeanException - 在注册表中找不到 db-update-bean 的 bean]
当然,我可以以编程方式将组件添加到 akka 提供的 CamelContext 中,或者执行如下操作:
from("seda:update-route")
.bean(new DBUpdate(jdbcTemplate, "gps_temp_status"), "process")
但我宁愿使用Spring来定义bean。显然,CamelExtension 需要使用 Spring 定义的 CamelContext,而不是创建自己的 CamelContext。
此外,更重要的是,我想将 Camel 路由的定义外部化到同一 applicationContext.xml 中<CamelContext/>
标签。根据诸如https://weblogs.java.net/blog/manningpubs/archive/2013/02/13/akka-and-camel https://weblogs.java.net/blog/manningpubs/archive/2013/02/13/akka-and-camel,似乎这样做的方法是实例化一个“camel-service”bean并注入Spring定义的CamelContext,如下所示:
<camel:camelContext id="camelContext">
</camel:camelContext>
<akka:camel-service id="camelService">
<akka:camel-context ref="camelContext" />
</akka:camel-service>
这是轮子脱落的地方。根据 Roland Kuhn 的回复为什么 akka 的 spring 集成文档仅存在于 1.3.1 而不适用于后续版本 https://stackoverflow.com/questions/16948292/why-spring-integration-doc-for-akka-exists-only-for-1-3-1-but-not-for-next-versi,akka-spring 库不包含在 Akka 2.2.1 中,因此 akka 没有 Spring 命名空间处理程序,并且我无法实例化这个骆驼服务 bean。虽然不是因为缺乏尝试。或者咒骂。
所以(最后)问题是:如何在 Spring XML 中定义 Camel 路由,其端点可以由消费者或生产者 Actor 使用 akka-camel 2.2.1 而不使用 akka-spring 使用?是否可以实例化一个工厂 bean,该工厂 bean 会生成一个带有注入 CamelContext 的 CamelService 或其他类似的巫术?其次(但相关)如果我被迫通过 RouteBuilder 定义它们,如何使用 Spring 实例化要在骆驼路由中引用的 bean?
提前致谢。