You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Tomatito <my...@gmail.com> on 2016/02/06 13:06:07 UTC
Failure to add routes dynamically
I'm using Apache-Camel 2.15.2.
I am trying to add routes to a CamelContext dynamically, but I came across a
problem that baffles me.
As far as I can tell, I do add the routes to the correct CamelContext, and
it seems like their configure() is called without throwing exceptions.
However when I try to execute the main route, I get a run time Exception
telling me that the route I added dynamically does not exist.
Here is a simplified version of my code:
public class MainRouteBuilder extends RouteBuilder{ public static
CamelContext camelContext; public static boolean returnLabel = true;
public static RouteBuilder nestedRouteBuilder; @Override public void
configure() throws Exception { System.out.println("Building main
route!"); System.out.println("Context: " + getContext());
camelContext = getContext(); from("direct:mainRoute") //3. I
do not actually want to instantiate RouteContainer like this each time I
call this route. //I would simply want to reuse a reference to an
instance I created outside of configure()... .to(new
RouteContainer().getMyRoute(2)) ; returnLabel = false;
//configure direct:myRoute.2 includeRoutes(nestedRouteBuilder);
}}public class RouteContainer extends RouteBuilder{ public Route route;
RouteContainer() { super(MainRouteBuilder.camelContext); }
String getMyRoute(final int n) { if (MainRouteBuilder.returnLabel
&& route == null) { route = new Route() {
@Override public void configure() {
System.out.println("Building nested route!");
System.out.println("Context" + getContext());
from("direct:myRoute." + n) .transform()
.simple("number: " + n) .to("stream:out")
.process(new Processor() { @Override
public void process(Exchange exchange) throws Exception {
Response response = new Response();
response.setStatus(Status.SUCCESS);
exchange.getOut().setBody(response); }
}); } }; }//1. works:
MainRouteBuilder.nestedRouteBuilder = this;//2. does not work://
RouteContainer routeContainer = new RouteContainer();//
routeContainer.route = this.route;//
MainRouteBuilder.nestedRouteBuilder = routeContainer; return
"direct:myRoute." + n; } @Override public void configure() throws
Exception { if (route != null) { route.configure();
} } public abstract static class Route { abstract
public void configure(); }}
Requests that are sent to direct:mainRoute work.During Camel startup I see
in the console:
Building main route!Context: SpringCamelContext(camel-1) with spring id
org.springframework.web.context.WebApplicationContext:/sample-routeBuilding
nested route!ContextSpringCamelContext(camel-1) with spring id
org.springframework.web.context.WebApplicationContext:/sample-route
and when I send a request to direct:mainRoute, the output is:
{"status":"SUCCESS"}
HOWEVER, if I comment out (1) above, and uncomment (2), Camel starts up with
the same output to the console, but when I send a request to
direct:mainRoute, the execution of the route fails with the exception:
org.apache.camel.component.direct.DirectConsumerNotAvailableException: No
consumers available on endpoint: Endpoint[direct://myRoute.2].
To Clarify: my problem is because I would actually like NOT to instantiate
RouteContainer each time I call its route, as I do in (3). This is why I
instantiate them at point (2) and plug the Route instance into it...
So I would like MainRouteBuilder to look like this:
public class MainRouteBuilder extends RouteBuilder{ public static
CamelContext camelContext; public static boolean returnLabel = true;
public static RouteBuilder nestedRouteBuilder; RouteContainer
routeContainer = new RouteContainer(); @Override public void
configure() throws Exception { System.out.println("Building main
route!"); System.out.println("Context: " + getContext());
camelContext = getContext(); from("direct:mainRoute")
.to(routeContainer.getMyRoute(2)) //I may want to call it again like
this: //.to(routeContainer.getMyRoute(3)) ; returnLabel
= false; //configure direct:myRoute.2
includeRoutes(nestedRouteBuilder); }}
My assumption is that maybe the nested route direct:myRoute.2 is created in
the wrong CamelContext, but the console output tells me it is not so.
Any idea what I am doing wrong here?
--
View this message in context: http://camel.465427.n5.nabble.com/Failure-to-add-routes-dynamically-tp5777328.html
Sent from the Camel - Users mailing list archive at Nabble.com.