Search code examples
apache-camelspring-camel

How to get headers from within Exchange object in the body object


I have headers i'm setting within a function and then saving the Exchange objects to a list. newExchange.`in`.setHeader("aggregationCount", 3)

Im then trying to retrieve this header later on and I can see it, in the object its in the path of Exchange.in.body.in.headers - How would I access this variable, I've tried (e.`in`.getHeader("aggregationCount") however this doesn't work. All i need to is to pass a variable/property through on the Exchange object, if there is other ways to do this, tell me and i can just use this instead.


Solution

  • Within the Camel route, you can simply use .header("HeaderName") to return the header value. In this case, something like:

    .header("aggregationCount")
    

    Or if you need to use the value in a conditional:

    .when(header("aggregationCount").isGreaterThan(0))
    

    https://camel.apache.org/components/latest/languages/header-language.html

    To answer your other question, you could also use exchangeProperty to store and retrieve values.

    https://camel.apache.org/components/3.4.x/languages/exchangeProperty-language.html