Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Using Bean Expressions from the Java DSL

Code Block
languagejava

from("activemq:topic:OrdersTopic").
  filter().method("myBean", "isGoldCustomer").
    to("activemq:BigSpendersQueue");

Using Bean Expressions from XML

Code Block
langxml

<route>
  <from uri="activemq:topic:OrdersTopic"/>
  <filter>
    <method ref="myBean" method="isGoldCustomer"/>
    <to uri="activemq:BigSpendersQueue"/>
  </filter>
</route>

...

So we could implement it like this...

Code Block
languagejava

public class MyBean {
  public boolean isGoldCustomer(Exchange exchange) {
  	...
  }
}

We can also use the Bean Integration annotations. For example you could do...

Code Block
languagejava

public boolean isGoldCustomer(String body) {...}

or

Code Block
languagejava

public boolean isGoldCustomer(@Header(name = "foo") Integer fooHeader) {...}

...

Camel can instantiate the bean and invoke the method if given a class or invoke an already existing instance. This is illustrated from the example below:

Code Block
java
java

        from("activemq:topic:OrdersTopic").
                filter().expression(BeanLanguage(MyBean.class, "isGoldCustomer")).
                to("activemq:BigSpendersQueue");

The 2nd parameter isGoldCustomer is an optional parameter to explicit set the method name to invoke. If not provided Camel will try to invoke the best suited method. If case of ambiguity Camel will thrown an Exception. In these situations the 2nd parameter can solve this problem. Also the code is more readable if the method name is provided. The 1st parameter can also be an existing instance of a Bean such as:

Code Block
java
java

   private MyBean my;

        from("activemq:topic:OrdersTopic").
                filter().expression(BeanLanguage.bean(my, "isGoldCustomer")).
                to("activemq:BigSpendersQueue");

In Camel 2.2 onwards you can avoid the BeanLanguage and have it just as:

Code Block
java
java

   private MyBean my;

        from("activemq:topic:OrdersTopic").
                filter().expression(bean(my, "isGoldCustomer")).
                to("activemq:BigSpendersQueue");

Which also can be done in a bit shorter and nice way:

Code Block
java
java

   private MyBean my;

        from("activemq:topic:OrdersTopic").
                filter().method(my, "isGoldCustomer").
                to("activemq:BigSpendersQueue");

...