14

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?

abbasdgr8
  • 555
  • 1
  • 5
  • 15

2 Answers2

27

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

Matthew Wilson
  • 2,045
  • 14
  • 27
  • Thanks for the answer @matthew-wilson. Trying to push this a little further, how can I do it if I want to save the output of this bean call to a header? I'm expecting something like `setHeader("anotherHeader", simple("bean:myBean?method=beanMethod(${header.someHeader})"))` – abbasdgr8 May 28 '14 at 13:24
  • 2
    the method's return type is void so there will be no output from the bean. If you were to change its return type to be `String` for example then after calling the `.to(..` bean call the messages body will be the output of the method call. then you would be able to do `setHeader("anotherHeader",simple("${body}"))` – Matthew Wilson May 28 '14 at 13:54
  • 1
    Worth mentioning that this solution also works with properties `.to("bean:myBean?method=beanMethod(${property.propKey})"`. (tested on Camel 2.10) – Alessandro Da Rugna Apr 29 '16 at 11:52
  • 1
    @abbasdgr8, you may also use `.setHeader("anotherHeader", method(MyBean.class))`. This will call the method in `MyBean` (you can specify which method if you want) and set the header value; it will not modify the Exchange body. – DavidS Jan 15 '19 at 19:16
  • What does the URL look like if `myBean.beanMethod(Object o)` not just `(String ...)`, e.g. `myBean.beanMethod(org.apache.camel.Message m)`? – Gerold Broser Dec 18 '20 at 21:22
  • Or you can use .bean(MyBean.class, "beanMethod") and then MyBean.beanMethod(MyObject, Exchange) where MyObject is the message object at the time the bean is invoked. Then of course use Exchange to access header and properties. – Gerry Nov 01 '21 at 15:34
2

The answers seem to be a bit outdated. Here is how I do, the modern Camel way. You can retrieve the header value within a bean with the @Headers annotation, and you can call the bean method with passing the class and the method name;

Route Class

public class MyRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("direct:myRoute")
            .setHeader("myHeader", simple("my header value"))
            .bean(MyBean.class, "handle");
    }

}

Bean Class

public class MyBean {
    
    public static void handle(@Header("myHeader") String headerVal) {
        // do something with header
        System.out.println("myHeader: " + headerVal);
    }

}
Levent Divilioglu
  • 11,198
  • 5
  • 59
  • 106