Versions Compared

Key

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

...

You can use Java Code to define your RouteBuilder implementations, then in your spring.xml you can specify the Java package names to search for (recursively) to find your routes such as in the following example.

Wiki Markup
{snippet:id=example1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/camelContextFactoryBean.xml}

Or if you prefer you can use the Spring 2.0 XML Namespaces approach. These can be defined as beans in spring and then referenced in your camel context e.g.

Wiki Markup
{snippet:id=example3example5|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/camelContextFactoryBeancamelContextRouteBuilderRef.xml}

Or if you prefer to reference the route builder instance in the spring context

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, specifing 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 http://activemq.apache.org/camel/schema/spring">
    <package>org.apache.camel.spring.config.scan.route</package>
  </camelContext>
Wiki Markup
{snippet:id=example5|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/camelContextRouteBuilderRef.xml}
Warning

Use caution when specifying the package name as org.apache.camel or a sub package of this. This causes Camel to search in its own packages for your routes which could cause problems.

In 2.X 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 'packages' 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>
      <packages>org.example.routes</packages>
      <excludes>**.*Excluded*</excludes>
      <includes>**.*</includes>
    </packageScan>
  </camelContext>

Exclude patterns are applied before the include patterns. If no include or exclude patterns are defined then all the Route classes discovered in the packages will be returned.

In the above example, camel will scan all the 'org.example.routes' package and any subpackages for RouteBuilder classes. Say the scan finds two RouteBuilders, one in org.example.routes called 'MyRoute" and another 'MyExcludedRoute' in a subpackage 'excluded'. The fully qualified names of each of the classes are extracted (org.example.routes.MyRoute, org.example.routes.excluded.MyExcludedRoute) and the include and exclude patterns are applied.

The exclude pattern **.*Excluded* is going to match the fqcn 'org.example.routes.excluded.MyExcludedRoute' and veto camel from initializing it.

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

For example:

**.*Excluded* would match org.simple.Excluded, org.apache.camel.SomeExcludedRoute or org.example.RouteWhichIsExcluded

**.??cluded* would match org.simple.IncludedRoute, org.simple.Excluded but not match org.simple.PrecludedRoute

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};
}

Using Spring XML

You can use Spring 2.0 XML configuration to specify your Xml Configuration for Routes such as in the following example.

...