Tarun Bharti
Tarun Bharti

Reputation: 195

How are ProducerTemplate and Apache Camel routes linked in Spring application?

I just started working on a Apache Camel with Spring and CXF rest web service and while going through the current code I am not able to understand the few things.

  1. Normally for apache camel we need to create a CamelContext and then link it to producerTemplate and then we add the route to the CamelContext like below.

    CamelContext context = new DefaultCamelContext();
    ProducerTemplate template = context.createProducerTemplate();
    context.addRoutes(new RouteBuilder() {public void configure() {
    ...
    }
    

    But when I see this in spring application its different, We are creating camel context in XML file with below which will allow spring to search the Spring ApplicationContext for route builder instances i.e. Camel pickup any RouteBuilder instances which was created by Spring in its scan process.

     <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>
    

    So above is the linking of RouteBuilder with camel context.

  2. Now for the producer Template, as in the application we are just creating the object in @Service class like below and sending the request to Route class

     @Autowired
     private ProducerTemplate template;
    
     UpsellResponse upsellResponse = template.requestBodyAndHeaders("{{upsell.route}}", upsellRequest, headers, UpsellResponse.class);
    

    But I am not able to understand how the template is linked with Camel Context and Route here. And also want to know whether the first point is correct or not.

I also noticed in the Route Class, {{}} are used to in from({{}}) and to({{}}) for the property file variable. Is this any specific rule related to Camel since in spring we use to use ${} for the variable from property file with @PropertySource and @Value

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

Upvotes: 0

Views: 2367

Answers (1)

Claus Ibsen
Claus Ibsen

Reputation: 55540

When using Spring XML files and @Autowire etc then camel-spring has factory beans that create ProducerTemplate and link to CamelContext. Also it has a namespace parser to parse <camelContext> and create Camel context.

For using {{ }} then its the syntax Camel uses for property placeholders. See more here: http://camel.apache.org/how-do-i-use-spring-property-placeholder-with-camel-xml.html and http://camel.apache.org/using-propertyplaceholder.html

Upvotes: 1

Related Questions