camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henryk Konsek (Confluence)" <conflue...@apache.org>
Subject [CONF] Apache Camel > Spring Java Config
Date Fri, 25 Oct 2013 19:38:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/en/2176/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/Spring+Java+Config">Spring
Java Config</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~hekonsek">Henryk
Konsek</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        Extended documentation with common use cases.<br />
    </div>
        <br/>
                         <h4>Changes (3)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >Note that this library is totally
optional; you could just wire Camel together yourself with Java Config. <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">h3.
Example <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h3.
Common cases <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The
most common case of using JavaConfig with Camel would be to create configuration with defined
list of routes to be used by router. <br> <br>{code:java} <br>@Configuration
<br>public class MyRouteConfiguration extends CamelConfiguration { <br> <br>
   @Autowire <br>    private MyRouteBuilder myRouteBuilder; <br> <br>  
 @Autowire <br>    private MyAnotherRouteBuilder myAnotherRouteBuilder; <br> <br>
   @Override <br>    public List&lt;RouteBuilder&gt; routes() { <br> 
      return Arrays.asList(myRouteBuilder, myAnotherRouteBuilder); <br>    }  <br>
<br>} <br>{code} <br> <br>Starting from Camel 2.13.0 you can skip
the *routes()* definition, and fall back to the *RouteBuilder* instances located in the Spring
context. <br> <br>{code:java} <br>@Configuration <br>@ComponentScan(&quot;com.example.routes&quot;)
<br>public class MyRouteConfiguration extends CamelConfiguration { <br>} <br>{code}
<br> <br>h3. Other examples <br> <br></td></tr>
            <tr><td class="diff-unchanged" >The following [example using Java
Config|http://svn.apache.org/repos/asf/camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/patterns/FilterTest.java]
is actually a [Spring Testing] based unit test. <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="SpringJavaConfig-SpringJavaConfig"></a>Spring Java
Config</h2>

<p><a href="/confluence/display/CAMEL/Spring" title="Spring">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 <a href="/confluence/display/CAMEL/Guice"
title="Guice">Guice</a> along with the <a href="http://www.springsource.org/javaconfig"
class="external-link" rel="nofollow">Spring JavaConfig</a> project.</p>

<p>You can use either the XML or Java config approachs with Camel; its your choice really
on which you prefer.</p>

<p><b>NOTE:</b> From Camel 2.2.0 camel-spring-javaconfig is moved to Spring
3.x, which means you can't run it with spring 2.x.</p>

<h3><a name="SpringJavaConfig-UsingSpringJavaConfig"></a>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" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
  &lt;artifactId&gt;camel-spring-javaconfig&lt;/artifactId&gt;
  &lt;version&gt;${camel-version}&lt;/version&gt;
&lt;/dependency&gt;
</pre>
</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><a name="SpringJavaConfig-Commoncases"></a>Common cases</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" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
@Configuration
public class MyRouteConfiguration extends CamelConfiguration {

    @Autowire
    private MyRouteBuilder myRouteBuilder;

    @Autowire
    private MyAnotherRouteBuilder myAnotherRouteBuilder;

    @Override
    public List&lt;RouteBuilder&gt; routes() {
        return Arrays.asList(myRouteBuilder, myAnotherRouteBuilder);
    } 

}
</pre>
</div></div>

<p>Starting from Camel 2.13.0 you can skip the <b>routes()</b> definition,
and fall back to the <b>RouteBuilder</b> instances located in the Spring context.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
@Configuration
@ComponentScan("com.example.routes")
public class MyRouteConfiguration extends CamelConfiguration {
}
</pre>
</div></div>

<h3><a name="SpringJavaConfig-Otherexamples"></a>Other examples</h3>

<p>The following <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/patterns/FilterTest.java"
class="external-link" rel="nofollow">example using Java Config</a> is actually a
<a href="/confluence/display/CAMEL/Spring+Testing" title="Spring Testing">Spring Testing</a>
based unit test.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
@ContextConfiguration(
        locations = "org.apache.camel.spring.javaconfig.patterns.FilterTest$ContextConfig",
        loader = JavaConfigContextLoader.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 = "&lt;matched/&gt;";

        resultEndpoint.expectedBodiesReceived(expectedBody);

        template.sendBodyAndHeader(expectedBody, "foo", "bar");

        resultEndpoint.assertIsSatisfied();
    }

    @DirtiesContext
    @Test
    public void testSendNotMatchingMessage() throws Exception {
        resultEndpoint.expectedMessageCount(0);

        template.sendBodyAndHeader("&lt;notMatched/&gt;", "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");
                }
            };
        }
    }
}
]]></script>
</div></div>

<p>The <b>@ContextConfiguration</b> annotation tells the <a href="/confluence/display/CAMEL/Spring+Testing"
title="Spring Testing">Spring Testing</a> framework to load the <b>ContextConfig</b>
class as the configuration to use. This class derives from <b>SingleRouteCamelConfiguration</b>
which is a helper Spring Java Config class which will configure the CamelContext for us and
then register the RouteBuilder we create.</p>

<p>If you wish to create a collection of <b>RouteBuilder</b> instances then
derive from the <b>CamelConfiguration</b> helper class and implement the <b>routes()</b>
method. Keep in mind that (starting from the Camel 2.13.0) if you don't override <b>routes()</b>
method, then <b>CamelConfiguration</b> will use all <b>RouteBuilder</b>
instances available in the Spring context.</p>

<p>Since <b>Camel 2.11.0</b> you can use the CamelSpringJUnit4ClassRunner
with CamelSpringDelegatingTestContextLoader like <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/CamelSpringDelegatingTestContextLoaderTest.java"
class="external-link" rel="nofollow">example using Java Config with CamelSpringJUnit4ClassRunner</a>.<br/>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
@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("&lt;name&gt;test&lt;/name&gt;");

        endEndpoint.assertIsSatisfied();
        errorEndpoint.assertIsSatisfied();
    }
}
]]></script>
</div></div>.</p>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;" class="grey">
                        <a href="https://cwiki.apache.org/confluence/users/removespacenotification.action?spaceKey=CAMEL">Stop
watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://cwiki.apache.org/confluence/users/editmyemailsettings.action">Change
email notification preferences</a>
</div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Spring+Java+Config">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=104069&revisedVersion=9&originalVersion=8">View
Changes</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message