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

abbasdgr8 picture abbasdgr8 · May 28, 2014 · Viewed 36.8k times · Source

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?

Answer

Matthew Wilson picture Matthew Wilson · May 28, 2014

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