【问题标题】:How are ProducerTemplate and Apache Camel routes linked in Spring application?Spring 应用程序中的 ProducerTemplate 和 Apache Camel 路由如何链接?
【发布时间】:2016-10-14 13:42:01
【问题描述】:

我刚开始使用 Spring 和 CXF REST Web 服务开发 Apache Camel,在浏览当前代码时,我无法理解一些事情。

  1. 通常对于 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与骆驼上下文的链接。

  2. 现在对于生产者模板,在应用程序中,我们只是在 @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


    【解决方案1】:

    当使用 Spring XML 文件和 @Autowire 等时,camel-spring 具有创建 ProducerTemplate 并链接到 CamelContext 的工厂 bean。它还有一个命名空间解析器来解析 &lt;camelContext&gt; 并创建 Camel 上下文。

    对于使用{{ }},那么它的语法就是 Camel 用于属性占位符的语法。在此处查看更多信息:http://camel.apache.org/how-do-i-use-spring-property-placeholder-with-camel-xml.htmlhttp://camel.apache.org/using-propertyplaceholder.html

    【讨论】:

    • 下面的 XML 行中模板 ID 的名称是否重要
    • 如果你想给它一个特殊的名字,那就是bean id。默认情况下,它将名称分配为 producerTemplate 公平
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 2020-06-14
    • 2021-04-30
    • 2017-07-05
    相关资源
    最近更新 更多