Camel difference between route properties and exchange properties

11,686

The Exchange is only created upon the reception of the request in the client side. And this means that your Camel Processor will have access to the Message only and not the property from external resource.

The Exchange's properties are the Message's meta-information. As per the doc,

The Exchange also holds meta-data during its entire lifetime stored as properties accessible using the various getProperty(String) methods. The setProperty(String, Object) is used to store a property. For example you can use this to store security, SLA related data or any other information deemed useful throughout processing.

Share:
11,686
SLG
Author by

SLG

Updated on June 05, 2022

Comments

  • SLG
    SLG almost 2 years

    Can someone explain me the differences between the two properties?

    @Override
    public void setUp() throws Exception {
        context = new DefaultCamelContext(new SimpleRegistry());        
        template = context.createProducerTemplate();
    
        context.addRoutes(new RouteBuilder() {
    
            public void configure() throws Exception {              
                PropertiesComponent prop = context.getComponent(
                                          "properties", PropertiesComponent.class);
                prop.setLocation("classpath:test.properties");
    
                from("direct:start")
                .log("Property: ${properties:a}")
                .process(new Processor() {
    
                    @Override
                    public void process(Exchange ex) throws Exception {
                        String a = ex.getProperty("a", String.class);
                        LOG.info("Property: " + a);
                    }
                })
                ;
            }
        });
    
        context.getShutdownStrategy().setTimeout(1);
        context.start();
    }
    
    @Test
    public void testRoute() throws Exception {
        template.sendBody("direct:start", null);
    }
    

    The properties-file (test.properties):

    a = a
    

    Output:

    2015-09-03 14:38:01,740 | INFO  | route1           | Property: a
    2015-09-03 14:38:01,743 | INFO  | CamelTest2       | Property: null
    

    The first line is from .log("${properties:a}"), so it can be found. However, String a = ex.getProperty("a", String.class); cannot. Both are properties and point to the same property, right?

    What is the difference, and how can I find the property in the processor?

  • J2B
    J2B over 8 years
    And additional - The properties.a refers to the property in the properties file. While exchangeProperty (priviously property which is deprecated) referes to the property of the exchange. camel.apache.org/simple.html
  • SLG
    SLG over 8 years
    Ah, I see. Thanks! But is there a possibility to add the whole property file to the exchange?
  • Karthik R
    Karthik R over 8 years
    Not exactly sure. But if you are using a Java DSL, you have the most flexibility isn't it? write a method that can load property file using a simple java Properties : Properties properties = ..... and add that property to the exchange. It should be simple :)