首页 文章

如何在Spring应用程序中链接ProducerTemplate和Apache Camel路由?

提问于
浏览
0

我刚刚开始使用Spring和CXF休息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中搜索路由构建器实例,即Camel拾取Spring在其扫描过程中创建的任何RouteBuilder实例 .

<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私人ProducerTemplate模板;

UpsellResponse upsellResponse = template.requestBodyAndHeaders(“{}”,upsellRequest,headers,UpsellResponse.class);

但我无法理解模板如何与Camel Context和Route链接 . 并且还想知道第一点是否正确 .

我还注意到在Route类中,{{}}用于从({{}})到({{}})的属性文件变量 . 这是与Camel相关的任何特定规则,因为在 Spring 天我们使用$ {}来为属性文件中的变量 @PropertySource and @Value

例如 from("{{upsell.route}}").routeId("v2.upsell.Hystrix.Consumer").to("{{upsell.hystrix.consumer.route}}");

1 回答

相关问题