Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camel difference between route properties and exchange properties

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?

like image 799
SLG Avatar asked Sep 13 '25 06:09

SLG


1 Answers

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.

like image 78
Karthik R Avatar answered Sep 14 '25 21:09

Karthik R