Reputation: 165
I have a RouteBuilder
class that has grown quite large with many Direct Routes: from("direct:...")
.
Each Route is performing a specific type of enrichment of the body.
The RouteBuilder
works fine but it would be nice to break it up into a few separate classes where each class is specific to the type of enrichment being performed.
The classes would be a part of the same workflow, just defined in separate classes.
Is this possible? If so, can anyone point me to examples?
Upvotes: 1
Views: 283
Reputation: 134
I wanted to know the answer to this too, but I haven't been able to find any examples either - so I tried doing this as a proof-of-concept (using Camel 3):
RouteBuilderA.java
public class RouteBuilderA extends RouteBuilder {
@Override
public void configure() {
from("timer:myTimer?fixedRate=true&period=1000")
.setBody().constant("ROUTE A")
.log("${body}")
.to(RouteBuilderB.INPUT);
}
}
RouteBuilderB.java
public class RouteBuilderB extends RouteBuilder {
public static final String INPUT = "direct:input";
@Override
public void configure() {
from(INPUT)
.setBody().constant("ROUTE B")
.log("${body}");
}
}
MainRouteBuilder.java
public class MainRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
getContext().addRoutes(new RouteBuilderA());
getContext().addRoutes(new RouteBuilderB());
}
}
I've not looked into the drawbacks of using this setup, i.e. what it means for error handling, Message Exchange Patterns etc. but this seems to work for a start.
Upvotes: 0
Reputation: 7025
As Screwtape already commented, you can have as many RouteBuilder
classes as you want to build 1 CamelContext
. Because you use Direct Routes, they need to be in the same CamelContext
what is normally true if they are in the same deployment unit.
If you use Spring-Boot and the Camel-Starter the RouteBuilder
s are even auto-discovered if you declare them as @Component
.
A simple example with multiple Direct Routes in different RouteBuilder
s
Upvotes: 2