Versions Compared

Key

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

...

Note

For Camel 1.x you need to use the following namespace:

Code Block

http://activemq.apache.org/camel/schema/spring

with the following schema location:

Code Block

http://activemq.apache.org/camel/schema/spring/camel-spring.xsd

You need to add Camel to the schemaLocation declaration

Code Block

http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd

So the XML file looks like this:

Code Block
xml
xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

...

Or you can refer to camel XSD in the XML declaration:

Code Block

xmlns:camel="http://camel.apache.org/schema/spring"

...

Camel also provides a powerful feature that allows for the automatic discovery and initialization of routes in given packages. This is configured by adding tags to the camel context in your spring context definition, specifying the packages to be recursively searched for RouteBuilder implementations. To use this feature in 1.X, requires a <package></package> tag specifying a comma separated list of packages that should be searched e.g.

Code Block
xml
xml

  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <package>org.apache.camel.spring.config.scan.route</package>
  </camelContext>

...

In Camel 2.0 this has been extended to allow selective inclusion and exclusion of discovered route classes using Ant like path matching. In spring this is specified by adding a <packageScan/> tag. The tag must contain one or more 'package' elements (similar to 1.x), and optionally one or more 'includes' or 'excludes' elements specifying patterns to be applied to the fully qualified names of the discovered classes. e.g.

Code Block
xml
xml

  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <packageScan>
      <package>org.example.routes</package>
      <excludes>**.*Excluded*</excludes>
      <includes>**.*</includes>
    </packageScan>
  </camelContext>

...

Under the covers, this is using Spring's AntPatternMatcher implementation, which matches as follows

Code Block

? matches one character
* matches zero or more characters
** matches zero or more segments of a fully qualified name

...

This allows you to just annotate your routes using the Spring @Component and have those routes included by Camel

Code Block

@Component
public class MyRoute extends SpringRouteBuilder {

    @Override
    public void configure() throws Exception {
        from("direct:start").to("mock:result");
    }
}

You can also use the ANT style for inclusion and exclusion, as mentioned above in the <packageScan> documentation.

Include Page
how do i import routes from other xml files
how do i import routes from other xml files

Test time exclusion.

At test time it is often desirable to be able to selectively exclude matching routes from being initalized that are not applicable or useful to the test scenario. For instance you might a spring context file routes-context.xml and three Route builders RouteA, RouteB and RouteC in the 'org.example.routes' package. The packageScan definition would discover all three of these routes and initialize them.

Say RouteC is not applicable to our test scenario and generates a lot of noise during test. It would be nice to be able to exclude this route from this specific test. The SpringTestSupport class has been modified to allow this. It provides two methods (excludedRoute and excludedRoutes) that may be overridden to exclude a single class or an array of classes.

Code Block
java
java

public class RouteAandRouteBOnlyTest extends SpringTestSupport {
    @Override      
    protected Class excludeRoute() {
        return RouteC.class;
    }
}

In order to hook into the camelContext initialization by spring to exclude the MyExcludedRouteBuilder.class we need to intercept the spring context creation. When overriding createApplicationContext to create the spring context, we call the getRouteExcludingApplicationContext() method to provide a special parent spring context that takes care of the exclusion.

Code Block
java
java

@Override
protected AbstractXmlApplicationContext createApplicationContext() {
    return new ClassPathXmlApplicationContext(new String[] {"routes-context.xml"}, getRouteExcludingApplicationContext());
}

RouteC will now be excluded from initialization. Similarly, in another test that is testing only RouteC, we could exclude RouteB and RouteA by overriding

Code Block

@Override
protected Class[] excludeRoutes() {
    return new Class[]{RouteA.class, RouteB.class};
}

...

If you want to be injected with the CamelContext in your POJO just implement the CamelContextAware interface; then when Spring creates your POJO the CamelContext will be injected into your POJO. Also see the Bean Integration for further injections.

Integration Testing

To avoid a hung route when testing using Spring Transactions see the note about Spring Integration Testing under Transactional Client.

See also