Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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:cloudAdministrationRoute.createTenant")
- //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();
- }
- }
- Building main route!
- Context: SpringCamelContext(camel-1) with spring id org.springframework.web.context.WebApplicationContext:/sample-route
- Building nested route!
- ContextSpringCamelContext(camel-1) with spring id org.springframework.web.context.WebApplicationContext:/sample-route
- {"status":"SUCCESS"}
- org.apache.camel.component.direct.DirectConsumerNotAvailableException: No consumers available on endpoint: Endpoint[direct://myRoute.2].
- 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:cloudAdministrationRoute.createTenant")
- .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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement