【发布时间】:2016-10-14 13:42:01
【问题描述】:
我刚开始使用 Spring 和 CXF REST Web 服务开发 Apache Camel,在浏览当前代码时,我无法理解一些事情。
-
通常对于 apache camel,我们需要创建一个
CamelContext,然后将其链接到producerTemplate,然后我们将路由添加到CamelContext,如下所示。CamelContext context = new DefaultCamelContext(); ProducerTemplate template = context.createProducerTemplate(); context.addRoutes(new RouteBuilder() {public void configure() { ... }但是当我在 spring 应用程序中看到它不同时,我们正在 XML 文件中创建骆驼上下文,下面将允许 spring 搜索 Spring
ApplicationContext以查找路由构建器实例,即骆驼拾取任何创建的RouteBuilder实例在 Spring 的扫描过程中。<camel-spring:camelContext id="marriottCamelContext" useMDCLogging="true" xmlns="http://camel.apache.org/schema/spring"> <camel-spring:contextScan /> <template id="xxxTemplateProducer" camelContextId="xxxCamelContext" /> </camel-spring:camelContext>所以上面是RouteBuilder与骆驼上下文的链接。
-
现在对于生产者模板,在应用程序中,我们只是在
@Service类中创建对象,如下所示,并将请求发送到Route类@Autowired private ProducerTemplate template; UpsellResponse upsellResponse = template.requestBodyAndHeaders("{{upsell.route}}", upsellRequest, headers, UpsellResponse.class);但我无法理解模板如何与 Camel Context 和 Route 链接。并且还想知道第一点是否正确。
我还注意到,在 Route 类中,{{}} 习惯于在 from({{}}) 和 to({{}}) 中用于属性文件变量。这是与 Camel 相关的任何特定规则吗,因为在春天我们使用${} 来作为属性文件中的变量@PropertySource and @Value
例如from("{{upsell.route}}").routeId("v2.upsell.Hystrix.Consumer").to("{{upsell.hystrix.consumer.route}}");
【问题讨论】:
标签: spring rest apache-camel