Mapping the message to method parameters

Camel provides a capability called Parameter Binding, which allows your route to explicitly map parts of the message to your method parameters when Camel invokes a Java method. This can be used anywhere you can use the Bean or Method Expression Languages, such as in a predicate, processor step, or expression.

This recipe will show you how to specify the mapping of the exchange values to your method parameters right within the DSL.

Getting ready

The Java code for this recipe is located in the org.camelcookbook.extend.binding package. The Spring XML files are located under src/main/resources/META-INF/spring and prefixed with binding.

This recipe assumes the existence of a Java class with the following definition, though the techniques explained in this recipe can be applied to any Java method call:

public class MyBean {
  public String sayHello(String name, boolean hipster) {
    return (hipster) ? ("Yo " + name) : ("Hello " + name);
  }
}

How to do it...

Within your Bean Expression Language calls, you can specify the parameter value mapping to use when calling out to a Java method using either literals or the Simple Expression Language.

In the XML DSL, this is written as:

<route>
  <from uri="direct:hipster"/>
  <bean ref="myBean"
        method="sayHello(${body}, true)"/>
</route>
<route>
  <from uri="direct:undecided"/>
  <bean ref="myBean"
        method="sayHello(${body}, ${header.hipster})"/>
</route>

In the Java DSL, the same routing logic is expressed as:

from("direct:hipster")
  .bean(MyBean.class, "sayHello(${body}, true)");

from("direct:undecided")
  .bean(MyBean.class, "sayHello(${body}, ${header.hipster})");

How it works...

Camel has a well-documented algorithm for how it maps exchange and Camel context data to the method parameters when calling out to a Java object (see http://camel.apache.org/bean-binding#BeanBinding-Parameterbinding).

In summary, it assumes that the first parameter is the message body, and automatically attempts to convert the body to the data type of that parameter, unless the first parameter is of the type org.apache.camel.Exchange, in which case it is bound directly. Parameter Binding allows the developer to specify how to map literals and message parts to the method parameters from within the Camel DSL.

Camel Parameter Binding allows you to specify literals (true or false, numbers, Strings, and null) as well as Simple Expressions as part of the binding string. You can also specify a wildcard asterisk(*) that tells Camel to use its normal parameter binding algorithm for that portion of the parameters.

For example, if you have a Java method that takes a String value and a boolean value:

public String myMethod(String name, boolean doDifferent)

You could call that from a Camel route in various ways, such as if you wanted to hard-code a value for the boolean parameter and use the default parameter mapping to pass the message body as a String for the first parameter:

.bean(MyBean.class, "myMethod(*, true)");

Alternatively, if you wanted to map a header value to the first parameter using a Simple Expression:

.bean(MyBean.class, "myMethod(${header.username}, false)");

This will also work within a .to() block, as part of a bean: endpoint URI:

.to("bean:myBean?method=myMethod(${header.username}, false)");

There's more...

Camel also provides a way for you to use annotations to tell it how to map the message to parameters. This approach allows the method creator to specify the mapping as opposed to the use of Parameter Binding, which is specified by the route developer:

public String myMethod(@Header("JMSCorrelationID") String id,
                       @Body String message) {
  //...
}

Parameter Binding Annotations also allow you to use other Expression Languages to provide more sophisticated mappings, such as using an XPath expression to map a value from the message to a parameter as follows:

public String myMethod(@XPath("/myData/people/@id") String id,
                       @Body String message) {
   //...
}

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.239.214