5
votes

Camel Spring Boot scans the Spring context for RouteBuilders. From the documentation:

Camel auto-configuration collects all the RouteBuilder instances from the Spring context and automatically injects them into the provided CamelContext. That means that creating new Camel route with the Spring Boot starter is as simple as adding the @Component annotated class to your classpath

Is there a way to control this: include/exclude packages or classes.

I want to annotate certain RouteBuilders and have Camel exclude those. My intent is to add them to the CamelContext dynamically, later.

1
Why wouldn't you just inject CamelContext (Spring Boot creates it for you) and addRoutes() when you need them?Andriy Slobodyanyk
The Routes are Spring components. Since I am using Camel Spring Boot, it does this magical thing: it examines the beans in the Spring Context, looking for RouteBuilders.Darius X.
Well, you might create Routes not as Spring components. I am not Camel expert but from my point of view all that tricks with dynamically creating routes, starting-stopping them etc smell bad and follow to unexpected errors. I think that the route should be defined at startup and lives and polls till the end. But if you need more control over it there is an option. Define the route starting from "direct:someLabel" and send messages to it with producerTemplate (which could or coundn't be Spring component, it does not matter).Andriy Slobodyanyk
Why don't you just remove the @Component annotation from your RouteBuilder? Without the annotation the RouteBuilder won't be discovered`mgyongyosi
You might to add .autostart(false) to the route definition and then start it later via ControlBus.Andriy Slobodyanyk

1 Answers

6
votes

You can use these properties, that are set to the SpringBootTest Annotation.

  • java-routes-inlcude-pattern - Pattern to include routes in your test
  • java-routes-exclude-pattern - Pattern to exclude routes in your test

These are ant style properties that search through your src folder for matching RouteBuilders

Example with pattern:

// Fix test runner
@RunWith(CamelSpringBootRunner.class)
// Your spring boot application with the main method
@SpringBootTest(classes = {SpringBootTestRunner.class},  
    // Finds all routes in your whole src folder that start with 'SomeRoute', e.g. SomeRouteOne
    properties = {"camel.springboot.java-routes-include-pattern=**/SomeRoute*"})
public class SomeRouteOneTestCase {
    ...   // Unit Tests
}

Example for fix class:

// Fix test runner
@RunWith(CamelSpringBootRunner.class)
// Your spring boot application with the main method
@SpringBootTest(classes = {SpringBootTestRunner.class},
    // Find the RouteBuilder 'SomeRouteTwo' in the package 'com.foo.bar' and nothing more
    properties = {"camel.springboot.java-routes-include-pattern=com/foo/bar/TestRouteTwo"})
public class TestRouteTwoTestCase {
    ...   // Tests
}

java-routes-exclude-pattern works in the same way, just exluding the listed route builders.

Hope this helps.

Greets
Chris