Modified: websites/production/camel/content/spring-java-config.html
==============================================================================
--- websites/production/camel/content/spring-java-config.html (original)
+++ websites/production/camel/content/spring-java-config.html Fri Aug 25
09:20:43 2017
@@ -36,17 +36,6 @@
<![endif]-->
- <link href='//camel.apache.org/styles/highlighter/styles/shCoreCamel.css'
rel='stylesheet' type='text/css' />
- <link href='//camel.apache.org/styles/highlighter/styles/shThemeCamel.css'
rel='stylesheet' type='text/css' />
- <script src='//camel.apache.org/styles/highlighter/scripts/shCore.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushJava.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushXml.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushPlain.js'
type='text/javascript'></script>
-
- <script type="text/javascript">
- SyntaxHighlighter.defaults['toolbar'] = false;
- SyntaxHighlighter.all();
- </script>
<title>
Apache Camel: Spring Java Config
@@ -86,15 +75,12 @@
<tbody>
<tr>
<td valign="top" width="100%">
-<div class="wiki-content maincontent"><h2
id="SpringJavaConfig-SpringJavaConfig">Spring Java Config</h2><p><a
shape="rect" href="spring.html">Spring</a> started life using XML Config to
wire beans together. However some folks don't like using XML and would rather
use Java code which led to the creation of Guice along with the Spring
JavaConfig project.</p><p>You can use either the XML or Java config approaches
with Camel; its your choice really on which you prefer.</p><h3
id="SpringJavaConfig-UsingSpringJavaConfig">Using Spring Java Config</h3><p>To
use Spring Java Config in your Camel project the easiest thing to do is add the
following to your pom.xml</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<dependency>
+<div class="wiki-content maincontent"><h2
id="SpringJavaConfig-SpringJavaConfig">Spring Java Config</h2><p><a
shape="rect" href="spring.html">Spring</a> started life using XML Config to
wire beans together. However some folks don't like using XML and would rather
use Java code which led to the creation of Guice along with the Spring
JavaConfig project.</p><p>You can use either the XML or Java config approaches
with Camel; its your choice really on which you prefer.</p><h3
id="SpringJavaConfig-UsingSpringJavaConfig">Using Spring Java Config</h3><p>To
use Spring Java Config in your Camel project the easiest thing to do is add the
following to your pom.xml</p><parameter
ac:name="">xml</parameter><plain-text-body><dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-javaconfig</artifactId>
<version>${camel-version}</version>
</dependency>
-]]></script>
-</div></div><p>This will then add the dependencies on the Spring JavaConfig
library along with some helper classes for configuring Camel inside
Spring.</p><p>Note that this library is totally optional; you could just wire
Camel together yourself with Java Config.</p><h3
id="SpringJavaConfig-Configuration">Configuration</h3><p>The most common case
of using JavaConfig with Camel would be to create configuration with defined
list of routes to be used by router.</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[@Configuration
+</plain-text-body><p>This will then add the dependencies on the Spring
JavaConfig library along with some helper classes for configuring Camel inside
Spring.</p><p>Note that this library is totally optional; you could just wire
Camel together yourself with Java Config.</p><h3
id="SpringJavaConfig-Configuration">Configuration</h3><p>The most common case
of using JavaConfig with Camel would be to create configuration with defined
list of routes to be used by router.</p><parameter
ac:name="">java</parameter><plain-text-body>@Configuration
public class MyRouteConfiguration extends CamelConfiguration {
@Autowire
@@ -109,114 +95,11 @@ public class MyRouteConfiguration extend
}
}
-]]></script>
-</div></div><p>Starting from Camel 2.13.0 you can skip the
<strong>routes()</strong> definition, and fall back to the
<strong>RouteBuilder</strong> instances located in the Spring context.</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[@Configuration
-@ComponentScan("com.example.routes")
+</plain-text-body><p>Starting from Camel 2.13.0 you can skip the
<strong>routes()</strong> definition, and fall back to the
<strong>RouteBuilder</strong> instances located in the Spring
context.</p><parameter
ac:name="">java</parameter><plain-text-body>@Configuration
+@ComponentScan("com.example.routes")
public class MyRouteConfiguration extends CamelConfiguration {
}
-]]></script>
-</div></div><h3 id="SpringJavaConfig-Testing">Testing</h3><p>Since
<strong>Camel 2.11.0</strong> you can use the
<code>CamelSpringJUnit4ClassRunner</code> with
<code>CamelSpringDelegatingTestContextLoader</code>. This is the recommended
way to test Java Config and Camel integration.</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-// tag::example[]
-@RunWith(CamelSpringJUnit4ClassRunner.class)
-@ContextConfiguration(
- classes =
{CamelSpringDelegatingTestContextLoaderTest.TestConfig.class},
- // Since Camel 2.11.0
- loader = CamelSpringDelegatingTestContextLoader.class
- )
-@MockEndpoints
-public class CamelSpringDelegatingTestContextLoaderTest {
- @EndpointInject(uri = "mock:direct:end")
- protected MockEndpoint endEndpoint;
-
- @EndpointInject(uri = "mock:direct:error")
- protected MockEndpoint errorEndpoint;
-
- @Produce(uri = "direct:test")
- protected ProducerTemplate testProducer;
-
- @Configuration
- public static class TestConfig extends SingleRouteCamelConfiguration {
- @Bean
- @Override
- public RouteBuilder route() {
- return new RouteBuilder() {
- @Override
- public void configure() throws Exception {
-
from("direct:test").errorHandler(deadLetterChannel("direct:error")).to("direct:end");
-
- from("direct:error").log("Received message
on direct:error endpoint.");
-
- from("direct:end").log("Received message on
direct:end endpoint.");
- }
- };
- }
- }
-
- @Test
- public void testRoute() throws InterruptedException {
- endEndpoint.expectedMessageCount(1);
- errorEndpoint.expectedMessageCount(0);
-
- testProducer.sendBody("<name>test</name>");
-
- endEndpoint.assertIsSatisfied();
- errorEndpoint.assertIsSatisfied();
- }
-}
-// end::example[]
-]]></script>
-</div></div>If you wish to create a collection of
<strong>RouteBuilder</strong> instances then derive from the
<strong>CamelConfiguration</strong> helper class and implement the
<strong>routes()</strong> method. Keep in mind that (starting from the Camel
2.13.0) if you don't override <strong>routes()</strong> method, then
<strong>CamelConfiguration</strong> will use all <strong>RouteBuilder</strong>
instances available in the Spring context.<p>The following <a shape="rect"
class="external-link"
href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/patterns/FilterTest.java">example
using Java Config</a> demonstrates how to test Java Config integration with
Camel 2.10 and lower. Keep in mind that <code>JavaConfigContextLoader</code> is
deprecated and could be removed in the future versions of Camel on the behalf
of the <code>CamelSpringDelegatingTestContextLoader</code>.</p><div class="code
panel pdl" styl
e="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-// tag::example[]
-@RunWith(CamelSpringJUnit4ClassRunner.class)
-@ContextConfiguration(classes = {FilterTest.ContextConfig.class}, loader =
CamelSpringDelegatingTestContextLoader.class)
-public class FilterTest extends AbstractJUnit4SpringContextTests {
-
- @EndpointInject(uri = "mock:result")
- protected MockEndpoint resultEndpoint;
-
- @Produce(uri = "direct:start")
- protected ProducerTemplate template;
-
- @DirtiesContext
- @Test
- public void testSendMatchingMessage() throws Exception {
- String expectedBody = "<matched/>";
-
- resultEndpoint.expectedBodiesReceived(expectedBody);
-
- template.sendBodyAndHeader(expectedBody, "foo",
"bar");
-
- resultEndpoint.assertIsSatisfied();
- }
-
- @DirtiesContext
- @Test
- public void testSendNotMatchingMessage() throws Exception {
- resultEndpoint.expectedMessageCount(0);
-
- template.sendBodyAndHeader("<notMatched/>",
"foo", "notMatchedHeaderValue");
-
- resultEndpoint.assertIsSatisfied();
- }
-
- @Configuration
- public static class ContextConfig extends SingleRouteCamelConfiguration {
- @Bean
- public RouteBuilder route() {
- return new RouteBuilder() {
- public void configure() {
-
from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");
- }
- };
- }
- }
-}
-// end::example[]
-]]></script>
-</div></div>The <strong>@ContextConfiguration</strong> annotation tells the <a
shape="rect" href="spring-testing.html">Spring Testing</a> framework to load
the <strong>ContextConfig</strong> class as the configuration to use. This
class derives from <strong>SingleRouteCamelConfiguration</strong> which is a
helper Spring Java Config class which will configure the CamelContext for us
and then register the RouteBuilder we create.</div>
+</plain-text-body><h3 id="SpringJavaConfig-Testing">Testing</h3><p>Since
<strong>Camel 2.11.0</strong> you can use the
<code>CamelSpringJUnit4ClassRunner</code> with
<code>CamelSpringDelegatingTestContextLoader</code>. This is the recommended
way to test Java Config and Camel
integration.<plain-text-body>{snippet:lang=java|id=example|url=camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/CamelSpringDelegatingTestContextLoaderTest.java}</plain-text-body>If
you wish to create a collection of <strong>RouteBuilder</strong> instances
then derive from the <strong>CamelConfiguration</strong> helper class and
implement the <strong>routes()</strong> method. Keep in mind that (starting
from the Camel 2.13.0) if you don't override <strong>routes()</strong> method,
then <strong>CamelConfiguration</strong> will use all
<strong>RouteBuilder</strong> instances available in the Spring
context.</p><p>The following <a shape="rect" class="external-link
"
href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/patterns/FilterTest.java">example
using Java Config</a> demonstrates how to test Java Config integration with
Camel 2.10 and lower. Keep in mind that <code>JavaConfigContextLoader</code> is
deprecated and could be removed in the future versions of Camel on the behalf
of the
<code>CamelSpringDelegatingTestContextLoader</code>.<plain-text-body>{snippet:lang=java|id=example|url=camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/patterns/FilterTest.java}</plain-text-body>The
<strong>@ContextConfiguration</strong> annotation tells the <a shape="rect"
href="spring-testing.html">Spring Testing</a> framework to load the
<strong>ContextConfig</strong> class as the configuration to use. This class
derives from <strong>SingleRouteCamelConfiguration</strong> which is a helper
Spring Java Config class which will confi
gure the CamelContext for us and then register the RouteBuilder we
create.</p></div>
</td>
<td valign="top">
<div class="navigation">
Modified: websites/production/camel/content/spring-remoting.html
==============================================================================
--- websites/production/camel/content/spring-remoting.html (original)
+++ websites/production/camel/content/spring-remoting.html Fri Aug 25 09:20:43
2017
@@ -164,148 +164,47 @@
<p>For more details see <a shape="rect"
href="using-exchange-pattern-annotations.html">Using Exchange Pattern
Annotations</a></p>
-<h2 id="SpringRemoting-BeanBinding">Bean Binding</h2><p>Bean Binding in Camel
defines both which methods are invoked and also how the <a shape="rect"
href="message.html">Message</a> is converted into the parameters of the method
when it is invoked.</p><h3
id="SpringRemoting-Choosingthemethodtoinvoke">Choosing the method to
invoke</h3><p>The binding of a Camel <a shape="rect"
href="message.html">Message</a> to a bean method call can occur in different
ways, in the following order of importance:</p><ul><li>if the message contains
the header <strong>CamelBeanMethodName</strong> then that method is invoked,
converting the body to the type of the method's argument.<ul><li>From
<strong>Camel 2.8</strong> onwards you can qualify parameter types to select
exactly which method to use among overloads with the same name (see below for
more details).</li><li>From <strong>Camel 2.9</strong> onwards you can specify
parameter values directly in the method option (see below for more
details).</li><
/ul></li><li>you can explicitly specify the method name in the <a shape="rect"
href="dsl.html">DSL</a> or when using <a shape="rect"
href="pojo-consuming.html">POJO Consuming</a> or <a shape="rect"
href="pojo-producing.html">POJO Producing</a></li><li>if the bean has a method
marked with the <code>@Handler</code> annotation, then that method is
selected</li><li>if the bean can be converted to a <a shape="rect"
href="processor.html">Processor</a> using the <a shape="rect"
href="type-converter.html">Type Converter</a> mechanism, then this is used to
process the message. The <a shape="rect" href="activemq.html">ActiveMQ</a>
component uses this mechanism to allow any JMS MessageListener to be invoked
directly by Camel without having to write any integration glue code. You can
use the same mechanism to integrate Camel into any other messaging/remoting
frameworks.</li><li>if the body of the message can be converted to a <a
shape="rect" class="external-link" href="http://camel.apache.org/m
aven/current/camel-core/apidocs/org/apache/camel/component/bean/BeanInvocation.html">BeanInvocation</a>
(the default payload used by the <a shape="rect" class="external-link"
href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/component/bean/ProxyHelper.html">ProxyHelper</a>)
component - then that is used to invoke the method and pass its
arguments</li><li>otherwise the type of the body is used to find a matching
method; an error is thrown if a single method cannot be chosen
unambiguously.</li><li>you can also use Exchange as the parameter itself, but
then the return type must be void.</li><li>if the bean class is private (or
package-private), interface methods will be preferred (from <strong>Camel
2.9</strong> onwards) since Camel can't invoke class methods on such
beans</li></ul><p>In cases where Camel cannot choose a method to invoke, an
<code>AmbiguousMethodCallException</code> is thrown.</p><p>By default the
return value is set on the outbound messag
e body. </p><h3 id="SpringRemoting-Asynchronousprocessing">Asynchronous
processing</h3><p>From <strong>Camel 2.18</strong> onwards you can
return a CompletionStage implementation (e.g. a CompletableFuture) to implement
asynchronous processing.</p><p>Please be sure to properly complete the
CompletionStage with the result or exception, including any timeout handling.
Exchange processing would wait for completion and would not impose any timeouts
automatically. It's extremely useful to monitor <a shape="rect"
class="external-link"
href="https://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/spi/InflightRepository.html">Inflight
repository</a> for any hanging messages.</p><p>Note that completing with
"null" won't set outbody message body to null, but would keep message intact.
This is useful to support methods that don't modify exchange and return
CompletableFuture<Void>. To set body to null, just add Exchange method
parameter and directly m
odify exchange messages.</p><p>Examples:</p><p>Simple asynchronous processor,
modifying message body.</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[public CompletableFuture<String>
doSomethingAsync(String body)]]></script>
-</div></div><p>Composite processor that do not modify exchange</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[public CompletableFuture<Void>
doSomethingAsync(String body) {
+<h2 id="SpringRemoting-BeanBinding">Bean Binding</h2><p>Bean Binding in Camel
defines both which methods are invoked and also how the <a shape="rect"
href="message.html">Message</a> is converted into the parameters of the method
when it is invoked.</p><h3
id="SpringRemoting-Choosingthemethodtoinvoke">Choosing the method to
invoke</h3><p>The binding of a Camel <a shape="rect"
href="message.html">Message</a> to a bean method call can occur in different
ways, in the following order of importance:</p><ul><li>if the message contains
the header <strong>CamelBeanMethodName</strong> then that method is invoked,
converting the body to the type of the method's argument.<ul><li>From
<strong>Camel 2.8</strong> onwards you can qualify parameter types to select
exactly which method to use among overloads with the same name (see below for
more details).</li><li>From <strong>Camel 2.9</strong> onwards you can specify
parameter values directly in the method option (see below for more
details).</li><
/ul></li><li>you can explicitly specify the method name in the <a shape="rect"
href="dsl.html">DSL</a> or when using <a shape="rect"
href="pojo-consuming.html">POJO Consuming</a> or <a shape="rect"
href="pojo-producing.html">POJO Producing</a></li><li>if the bean has a method
marked with the <code>@Handler</code> annotation, then that method is
selected</li><li>if the bean can be converted to a <a shape="rect"
href="processor.html">Processor</a> using the <a shape="rect"
href="type-converter.html">Type Converter</a> mechanism, then this is used to
process the message. The <a shape="rect" href="activemq.html">ActiveMQ</a>
component uses this mechanism to allow any JMS MessageListener to be invoked
directly by Camel without having to write any integration glue code. You can
use the same mechanism to integrate Camel into any other messaging/remoting
frameworks.</li><li>if the body of the message can be converted to a <a
shape="rect" class="external-link" href="http://camel.apache.org/m
aven/current/camel-core/apidocs/org/apache/camel/component/bean/BeanInvocation.html">BeanInvocation</a>
(the default payload used by the <a shape="rect" class="external-link"
href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/component/bean/ProxyHelper.html">ProxyHelper</a>)
component - then that is used to invoke the method and pass its
arguments</li><li>otherwise the type of the body is used to find a matching
method; an error is thrown if a single method cannot be chosen
unambiguously.</li><li>you can also use Exchange as the parameter itself, but
then the return type must be void.</li><li>if the bean class is private (or
package-private), interface methods will be preferred (from <strong>Camel
2.9</strong> onwards) since Camel can't invoke class methods on such
beans</li></ul><p>In cases where Camel cannot choose a method to invoke, an
<code>AmbiguousMethodCallException</code> is thrown.</p><p>By default the
return value is set on the outbound messag
e body. </p><h3 id="SpringRemoting-Asynchronousprocessing">Asynchronous
processing</h3><p>From <strong>Camel 2.18</strong> onwards you can
return a CompletionStage implementation (e.g. a CompletableFuture) to implement
asynchronous processing.</p><p>Please be sure to properly complete the
CompletionStage with the result or exception, including any timeout handling.
Exchange processing would wait for completion and would not impose any timeouts
automatically. It's extremely useful to monitor <a shape="rect"
class="external-link"
href="https://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/spi/InflightRepository.html">Inflight
repository</a> for any hanging messages.</p><p>Note that completing with
"null" won't set outbody message body to null, but would keep message intact.
This is useful to support methods that don't modify exchange and return
CompletableFuture<Void>. To set body to null, just add Exchange method
parameter and directly m
odify exchange messages.</p><p>Examples:</p><p>Simple asynchronous processor,
modifying message body.</p><parameter
ac:name="language">java</parameter><plain-text-body>public
CompletableFuture<String> doSomethingAsync(String
body)</plain-text-body><p>Composite processor that do not modify
exchange</p><parameter
ac:name="language">java</parameter><plain-text-body>public
CompletableFuture<Void> doSomethingAsync(String body) {
return CompletableFuture.allOf(doA(body), doB(body), doC());
-}]]></script>
-</div></div><h3 id="SpringRemoting-Parameterbinding">Parameter
binding</h3><p>When a method has been chosen for invocation, Camel will bind to
the parameters of the method.</p><p>The following Camel-specific types are
automatically bound:</p><ul
class="alternate"><li><code>org.apache.camel.Exchange</code></li><li><code>org.apache.camel.Message</code></li><li><code>org.apache.camel.CamelContext</code></li><li><code>org.apache.camel.TypeConverter</code></li><li><code>org.apache.camel.spi.Registry</code></li><li><code>java.lang.Exception</code></li></ul><p>So,
if you declare any of these types, they will be provided by Camel.
<strong>Note that <code>Exception</code> will bind to the caught exception of
the <a shape="rect" href="exchange.html">Exchange</a></strong> - so it's often
usable if you employ a <a shape="rect" href="pojo.html">Pojo</a> to handle,
e.g., an <code>onException</code> route.</p><p>What is most interesting is that
Camel will also try to bind the body of the <a shape=
"rect" href="exchange.html">Exchange</a> to the first parameter of the method
signature (albeit not of any of the types above). So if, for instance, we
declare a parameter as <code>String body</code>, then Camel will bind the IN
body to this type. Camel will also automatically convert to the type declared
in the method signature.</p><p>Let's review some examples:</p><p>Below is a
simple method with a body binding. Camel will bind the IN body to the
<code>body</code> parameter and convert it to a <code>String</code>.</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[public String doSomething(String body)
-]]></script>
-</div></div><p>In the following sample we got one of the automatically-bound
types as well - for instance, a <code>Registry</code> that we can use to lookup
beans.</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[public String doSomething(String body,
Registry registry)
-]]></script>
-</div></div><p>We can use <a shape="rect" href="exchange.html">Exchange</a> as
well:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[public String doSomething(String body,
Exchange exchange)
-]]></script>
-</div></div><p>You can also have multiple types:</p><div class="code panel
pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[public String doSomething(String body,
Exchange exchange, TypeConverter converter)
-]]></script>
-</div></div><p>And imagine you use a <a shape="rect" href="pojo.html">Pojo</a>
to handle a given custom exception <code>InvalidOrderException</code> - we can
then bind that as well:</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[public String badOrder(String body,
InvalidOrderException invalid)
-]]></script>
-</div></div><p>Notice that we can bind to it even if we use a sub type of
<code>java.lang.Exception</code> as Camel still knows it's an exception and can
bind the cause (if any exists).</p><p>So what about headers and other stuff?
Well now it gets a bit tricky - so we can use annotations to help us, or
specify the binding in the method name option.<br clear="none"> See the
following sections for more detail.</p><h3
id="SpringRemoting-BindingAnnotations">Binding Annotations</h3><p>You can use
the <a shape="rect" href="parameter-binding-annotations.html">Parameter Binding
Annotations</a> to customize how parameter values are created from the <a
shape="rect" href="message.html">Message</a></p><h4
id="SpringRemoting-Examples">Examples</h4><p>For example, a <a shape="rect"
href="bean.html">Bean</a> such as:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[public class Bar {
+}</plain-text-body><h3 id="SpringRemoting-Parameterbinding">Parameter
binding</h3><p>When a method has been chosen for invocation, Camel will bind to
the parameters of the method.</p><p>The following Camel-specific types are
automatically bound:</p><ul
class="alternate"><li><code>org.apache.camel.Exchange</code></li><li><code>org.apache.camel.Message</code></li><li><code>org.apache.camel.CamelContext</code></li><li><code>org.apache.camel.TypeConverter</code></li><li><code>org.apache.camel.spi.Registry</code></li><li><code>java.lang.Exception</code></li></ul><p>So,
if you declare any of these types, they will be provided by Camel.
<strong>Note that <code>Exception</code> will bind to the caught exception of
the <a shape="rect" href="exchange.html">Exchange</a></strong> - so it's often
usable if you employ a <a shape="rect" href="pojo.html">Pojo</a> to handle,
e.g., an <code>onException</code> route.</p><p>What is most interesting is that
Camel will also try to bind the body of the <a
shape="rect" href="exchange.html">Exchange</a> to the first parameter of the
method signature (albeit not of any of the types above). So if, for instance,
we declare a parameter as <code>String body</code>, then Camel will bind the IN
body to this type. Camel will also automatically convert to the type declared
in the method signature.</p><p>Let's review some examples:</p><p>Below is a
simple method with a body binding. Camel will bind the IN body to the
<code>body</code> parameter and convert it to a
<code>String</code>.</p><plain-text-body>public String doSomething(String body)
+</plain-text-body><p>In the following sample we got one of the
automatically-bound types as well - for instance, a <code>Registry</code> that
we can use to lookup beans.</p><plain-text-body>public String
doSomething(String body, Registry registry)
+</plain-text-body><p>We can use <a shape="rect"
href="exchange.html">Exchange</a> as well:</p><plain-text-body>public String
doSomething(String body, Exchange exchange)
+</plain-text-body><p>You can also have multiple
types:</p><plain-text-body>public String doSomething(String body, Exchange
exchange, TypeConverter converter)
+</plain-text-body><p>And imagine you use a <a shape="rect"
href="pojo.html">Pojo</a> to handle a given custom exception
<code>InvalidOrderException</code> - we can then bind that as
well:</p><plain-text-body>public String badOrder(String body,
InvalidOrderException invalid)
+</plain-text-body><p>Notice that we can bind to it even if we use a sub type
of <code>java.lang.Exception</code> as Camel still knows it's an exception and
can bind the cause (if any exists).</p><p>So what about headers and other
stuff? Well now it gets a bit tricky - so we can use annotations to help us, or
specify the binding in the method name option.<br clear="none"> See the
following sections for more detail.</p><h3
id="SpringRemoting-BindingAnnotations">Binding Annotations</h3><p>You can use
the <a shape="rect" href="parameter-binding-annotations.html">Parameter Binding
Annotations</a> to customize how parameter values are created from the <a
shape="rect" href="message.html">Message</a></p><h4
id="SpringRemoting-Examples">Examples</h4><p>For example, a <a shape="rect"
href="bean.html">Bean</a> such as:</p><plain-text-body>public class Bar {
public String doSomething(String body) {
// process the in body and return whatever you want
- return "Bye World";
+ return "Bye World";
}
-]]></script>
-</div></div><p>Or the Exchange example. Notice that the return type must be
<strong>void</strong> when there is only a single parameter of the type
<code>org.apache.camel.Exchange</code>:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[public class Bar {
+</plain-text-body><p>Or the Exchange example. Notice that the return type must
be <strong>void</strong> when there is only a single parameter of the type
<code>org.apache.camel.Exchange</code>:</p><plain-text-body>public class Bar {
public void doSomething(Exchange exchange) {
// process the exchange
- exchange.getIn().setBody("Bye World");
+ exchange.getIn().setBody("Bye World");
}
-]]></script>
-</div></div><h4 id="SpringRemoting-@Handler">@Handler</h4><p>You can mark a
method in your bean with the @Handler annotation to indicate that this method
should be used for <a shape="rect" href="bean-binding.html">Bean
Binding</a>.<br clear="none"> This has an advantage as you need not specify a
method name in the Camel route, and therefore do not run into problems after
renaming the method in an IDE that can't find all its references.</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[public class Bar {
+</plain-text-body><h4 id="SpringRemoting-@Handler">@Handler</h4><p>You can
mark a method in your bean with the @Handler annotation to indicate that this
method should be used for <a shape="rect" href="bean-binding.html">Bean
Binding</a>.<br clear="none"> This has an advantage as you need not specify a
method name in the Camel route, and therefore do not run into problems after
renaming the method in an IDE that can't find all its references.</p><parameter
ac:name="">java</parameter><plain-text-body>public class Bar {
@Handler
public String doSomething(String body) {
// process the in body and return whatever you want
- return "Bye World";
+ return "Bye World";
}
-]]></script>
-</div></div><h3
id="SpringRemoting-Parameterbindingusingmethodoption">Parameter binding using
method option</h3><p><strong>Available as of Camel 2.9</strong></p><p>Camel
uses the following rules to determine if it's a parameter value in the method
option</p><ul class="alternate"><li>The value is either <code>true</code> or
<code>false</code> which denotes a boolean value</li><li>The value is a numeric
value such as <code>123</code> or <code>7</code></li><li>The value is a String
enclosed with either single or double quotes</li><li>The value is null which
denotes a <code>null</code> value</li><li>It can be evaluated using the <a
shape="rect" href="simple.html">Simple</a> language, which means you can use,
e.g., body, header.foo and other <a shape="rect" href="simple.html">Simple</a>
tokens. Notice the tokens must be enclosed with ${ }.</li></ul><p>Any other
value is consider to be a type declaration instead - see the next section about
specifying types for overloaded methods.</p><p>W
hen invoking a <a shape="rect" href="bean.html">Bean</a> you can instruct
Camel to invoke a specific method by providing the method name:</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ .bean(OrderService.class,
"doSomething")
-]]></script>
-</div></div><p>Here we tell Camel to invoke the doSomething method - Camel
handles the parameters' binding. Now suppose the method has 2 parameters, and
the 2nd parameter is a boolean where we want to pass in a true value:</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[public void doSomething(String payload,
boolean highPriority) {
+</plain-text-body><h3
id="SpringRemoting-Parameterbindingusingmethodoption">Parameter binding using
method option</h3><p><strong>Available as of Camel 2.9</strong></p><p>Camel
uses the following rules to determine if it's a parameter value in the method
option</p><ul class="alternate"><li>The value is either <code>true</code> or
<code>false</code> which denotes a boolean value</li><li>The value is a numeric
value such as <code>123</code> or <code>7</code></li><li>The value is a String
enclosed with either single or double quotes</li><li>The value is null which
denotes a <code>null</code> value</li><li>It can be evaluated using the <a
shape="rect" href="simple.html">Simple</a> language, which means you can use,
e.g., body, header.foo and other <a shape="rect" href="simple.html">Simple</a>
tokens. Notice the tokens must be enclosed with ${ }.</li></ul><p>Any other
value is consider to be a type declaration instead - see the next section about
specifying types for overloaded methods.</
p><p>When invoking a <a shape="rect" href="bean.html">Bean</a> you can
instruct Camel to invoke a specific method by providing the method
name:</p><plain-text-body> .bean(OrderService.class, "doSomething")
+</plain-text-body><p>Here we tell Camel to invoke the doSomething method -
Camel handles the parameters' binding. Now suppose the method has 2 parameters,
and the 2nd parameter is a boolean where we want to pass in a true
value:</p><plain-text-body>public void doSomething(String payload, boolean
highPriority) {
...
}
-]]></script>
-</div></div><p>This is now possible in <strong>Camel 2.9</strong>
onwards:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ .bean(OrderService.class,
"doSomething(*, true)")
-]]></script>
-</div></div><p>In the example above, we defined the first parameter using the
wild card symbol *, which tells Camel to bind this parameter to any type, and
let Camel figure this out. The 2nd parameter has a fixed value of
<code>true</code>. Instead of the wildcard symbol we can instruct Camel to use
the message body as shown:</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ .bean(OrderService.class,
"doSomething(${body}, true)")
-]]></script>
-</div></div><p>The syntax of the parameters is using the <a shape="rect"
href="simple.html">Simple</a> expression language so we have to use ${ }
placeholders in the body to refer to the message body.</p><p>If you want to
pass in a <code>null</code> value, then you can explicit define this in the
method option as shown below:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
.to("bean:orderService?method=doSomething(null, true)")
-]]></script>
-</div></div><p>Specifying <code>null</code> as a parameter value instructs
Camel to force passing a <code>null</code> value.</p><p>Besides the message
body, you can pass in the message headers as a
<code>java.util.Map</code>:</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ .bean(OrderService.class,
"doSomethingWithHeaders(${body}, ${headers})")
-]]></script>
-</div></div><p>You can also pass in other fixed values besides booleans. For
example, you can pass in a String and an integer:</p><div class="code panel
pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ .bean(MyBean.class,
"echo('World', 5)")
-]]></script>
-</div></div><p>In the example above, we invoke the echo method with two
parameters. The first has the content 'World' (without quotes), and the 2nd has
the value of 5.<br clear="none"> Camel will automatically convert these values
to the parameters' types.</p><p>Having the power of the <a shape="rect"
href="simple.html">Simple</a> language allows us to bind to message headers and
other values such as:</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ .bean(OrderService.class,
"doSomething(${body}, ${header.high})")
-]]></script>
-</div></div><p>You can also use the OGNL support of the <a shape="rect"
href="simple.html">Simple</a> expression language. Now suppose the message body
is an object which has a method named <code>asXml</code>. To invoke the
<code>asXml</code> method we can do as follows:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ .bean(OrderService.class,
"doSomething(${body.asXml}, ${header.high})")
-]]></script>
-</div></div><p>Instead of using <code>.bean</code> as shown in the examples
above, you may want to use <code>.to</code> instead as shown:</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
.to("bean:orderService?method=doSomething(${body.asXml},
${header.high})")
-]]></script>
-</div></div><h3
id="SpringRemoting-Usingtypequalifierstoselectamongoverloadedmethods">Using
type qualifiers to select among overloaded methods</h3><p><strong>Available as
of Camel 2.8</strong></p><p>If you have a <a shape="rect"
href="bean.html">Bean</a> with overloaded methods, you can now specify
parameter types in the method name so Camel can match the method you intend to
use.<br clear="none"> Given the following bean:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeHeader panelHeader pdl"
style="border-bottom-width: 1px;"><b>MyBean</b></div><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-public static final class MyBean {
-
- public String hello(String name) {
- return "Hello " + name;
- }
-
- public String hello(String name, @Header("country") String
country) {
- return "Hello " + name + " you are from " +
country;
- }
-
- public String times(String name, @Header("times") int times) {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < times; i++) {
- sb.append(name);
- }
- return sb.toString();
- }
-
- public String times(byte[] data, @Header("times") int times) {
- String s = new String(data);
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < times; i++) {
- sb.append(s);
- if (i < times - 1) {
- sb.append(",");
- }
- }
- return sb.toString();
- }
-
- public String times(String name, int times, char separator) {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < times; i++) {
- sb.append(name);
- if (i < times - 1) {
- sb.append(separator);
- }
- }
- return sb.toString();
- }
-
-}
-]]></script>
-</div></div>Then the <code>MyBean</code> has 2 overloaded methods with the
names <code>hello</code> and <code>times</code>. So if we want to use the
method which has 2 parameters we can do as follows in the Camel route:<div
class="code panel pdl" style="border-width: 1px;"><div class="codeHeader
panelHeader pdl" style="border-bottom-width: 1px;"><b>Invoke 2 parameter
method</b></div><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-from("direct:start")
- .bean(MyBean.class, "hello(String,String)")
- .to("mock:result");
-]]></script>
-</div></div>We can also use a <code>*</code> as wildcard so we can just say we
want to execute the method with 2 parameters we do<div class="code panel pdl"
style="border-width: 1px;"><div class="codeHeader panelHeader pdl"
style="border-bottom-width: 1px;"><b>Invoke 2 parameter method using
wildcard</b></div><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-from("direct:start")
- .bean(MyBean.class, "hello(*,*)")
- .to("mock:result");
-]]></script>
-</div></div>By default Camel will match the type name using the simple name,
e.g. any leading package name will be disregarded. However if you want to match
using the FQN, then specify the FQN type and Camel will leverage that. So if
you have a <code>com.foo.MyOrder</code> and you want to match against the FQN,
and <strong>not</strong> the simple name "MyOrder", then follow this
example:<div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ .bean(OrderService.class,
"doSomething(com.foo.MyOrder)")
-]]></script>
-</div></div><div class="confluence-information-macro
confluence-information-macro-information"><span class="aui-icon aui-icon-small
aui-iconfont-info confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><p>Camel currently only supports
either specifying parameter binding or type per parameter in the method name
option. You <strong>cannot</strong> specify both at the same time, such
as</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[doSomething(com.foo.MyOrder ${body}, boolean
${header.high})
-]]></script>
-</div></div><p>This may change in the future.</p></div></div></div>
+</plain-text-body><p>This is now possible in <strong>Camel 2.9</strong>
onwards:</p><plain-text-body> .bean(OrderService.class, "doSomething(*,
true)")
+</plain-text-body><p>In the example above, we defined the first parameter
using the wild card symbol *, which tells Camel to bind this parameter to any
type, and let Camel figure this out. The 2nd parameter has a fixed value of
<code>true</code>. Instead of the wildcard symbol we can instruct Camel to use
the message body as shown:</p><plain-text-body> .bean(OrderService.class,
"doSomething(${body}, true)")
+</plain-text-body><p>The syntax of the parameters is using the <a shape="rect"
href="simple.html">Simple</a> expression language so we have to use ${ }
placeholders in the body to refer to the message body.</p><p>If you want to
pass in a <code>null</code> value, then you can explicit define this in the
method option as shown below:</p><plain-text-body>
.to("bean:orderService?method=doSomething(null, true)")
+</plain-text-body><p>Specifying <code>null</code> as a parameter value
instructs Camel to force passing a <code>null</code> value.</p><p>Besides the
message body, you can pass in the message headers as a
<code>java.util.Map</code>:</p><plain-text-body> .bean(OrderService.class,
"doSomethingWithHeaders(${body}, ${headers})")
+</plain-text-body><p>You can also pass in other fixed values besides booleans.
For example, you can pass in a String and an integer:</p><plain-text-body>
.bean(MyBean.class, "echo('World', 5)")
+</plain-text-body><p>In the example above, we invoke the echo method with two
parameters. The first has the content 'World' (without quotes), and the 2nd has
the value of 5.<br clear="none"> Camel will automatically convert these values
to the parameters' types.</p><p>Having the power of the <a shape="rect"
href="simple.html">Simple</a> language allows us to bind to message headers and
other values such as:</p><plain-text-body> .bean(OrderService.class,
"doSomething(${body}, ${header.high})")
+</plain-text-body><p>You can also use the OGNL support of the <a shape="rect"
href="simple.html">Simple</a> expression language. Now suppose the message body
is an object which has a method named <code>asXml</code>. To invoke the
<code>asXml</code> method we can do as follows:</p><plain-text-body>
.bean(OrderService.class, "doSomething(${body.asXml}, ${header.high})")
+</plain-text-body><p>Instead of using <code>.bean</code> as shown in the
examples above, you may want to use <code>.to</code> instead as
shown:</p><plain-text-body>
.to("bean:orderService?method=doSomething(${body.asXml}, ${header.high})")
+</plain-text-body><h3
id="SpringRemoting-Usingtypequalifierstoselectamongoverloadedmethods">Using
type qualifiers to select among overloaded methods</h3><p><strong>Available as
of Camel 2.8</strong></p><p>If you have a <a shape="rect"
href="bean.html">Bean</a> with overloaded methods, you can now specify
parameter types in the method name so Camel can match the method you intend to
use.<br clear="none"> Given the following
bean:<plain-text-body>{snippet:id=e1|lang=java|title=MyBean|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodTest.java}</plain-text-body>Then
the <code>MyBean</code> has 2 overloaded methods with the names
<code>hello</code> and <code>times</code>. So if we want to use the method
which has 2 parameters we can do as follows in the Camel
route:<plain-text-body>{snippet:id=e2|lang=java|title=Invoke 2 parameter
method|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodTest.java}</pla
in-text-body>We can also use a <code>*</code> as wildcard so we can just say
we want to execute the method with 2 parameters we
do<plain-text-body>{snippet:id=e3|lang=java|title=Invoke 2 parameter method
using
wildcard|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodTest.java}</plain-text-body>By
default Camel will match the type name using the simple name, e.g. any leading
package name will be disregarded. However if you want to match using the FQN,
then specify the FQN type and Camel will leverage that. So if you have a
<code>com.foo.MyOrder</code> and you want to match against the FQN, and
<strong>not</strong> the simple name "MyOrder", then follow this
example:</p><plain-text-body> .bean(OrderService.class,
"doSomething(com.foo.MyOrder)")
+</plain-text-body><rich-text-body><p>Camel currently only supports either
specifying parameter binding or type per parameter in the method name option.
You <strong>cannot</strong> specify both at the same time, such
as</p><plain-text-body>doSomething(com.foo.MyOrder ${body}, boolean
${header.high})
+</plain-text-body><p>This may change in the future.</p></rich-text-body></div>
</td>
<td valign="top">
<div class="navigation">