javaspringparametersapache-camelspring-bean

Camel - Passing specific parameters from routes to a generic bean method


Let's say I have a Camel route that looks like this :

from("direct:myRoute")
        .setHeader("someHeader", simple("some header value"))
        .beanRef("myBean", "beanMethod");

And I have a bean that I cannot change that looks like this :

public class MyBean {
    public void beanMethod(String headerExpected) {
        // do something with the value here.
    }
}

Basically, I want to pass the value of someHeader from myRoute to beanMethod within MyBean.

Knowing that beanMethod can accept a String, how can I pass the value of the header someHeader from the route so that it is accepted as a String within beanMethod?


Solution

  • You can pass parameters in the way you described like this:

    from("direct:myRoute")
    .setHeader("someHeader", simple("some header value"))
    .to("bean:myBean?method=beanMethod(${header.someHeader})")
    

    More info, including other methods for bean binding can be found here http://camel.apache.org/bean-binding.html