Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. public class MainRouteBuilder extends RouteBuilder
  2. {
  3. public static CamelContext camelContext;
  4.  
  5. public static boolean returnLabel = true;
  6.  
  7. public static RouteBuilder nestedRouteBuilder;
  8.  
  9. @Override
  10. public void configure() throws Exception
  11. {
  12. System.out.println("Building main route!");
  13. System.out.println("Context: " + getContext());
  14.  
  15. camelContext = getContext();
  16.  
  17. from("direct:cloudAdministrationRoute.createTenant")
  18. //3. I do not actually want to instantiate RouteContainer like this each time I call this route.
  19. //I would simply want to reuse a reference to an instance I created outside of configure()...
  20. .to(new RouteContainer().getMyRoute(2))
  21. ;
  22.  
  23. returnLabel = false;
  24.  
  25. //configure direct:myRoute.2
  26.  
  27. includeRoutes(nestedRouteBuilder);
  28. }
  29.  
  30. }
  31.  
  32. public class RouteContainer extends RouteBuilder
  33. {
  34. public Route route;
  35.  
  36. RouteContainer() {
  37. super(MainRouteBuilder.camelContext);
  38. }
  39.  
  40. String getMyRoute(final int n) {
  41.  
  42. if (MainRouteBuilder.returnLabel && route == null) {
  43. route = new Route() {
  44.  
  45. @Override
  46. public void configure()
  47. {
  48.  
  49. System.out.println("Building nested route!");
  50. System.out.println("Context" + getContext());
  51.  
  52. from("direct:myRoute." + n)
  53. .transform()
  54. .simple("number: " + n)
  55. .to("stream:out")
  56. .process(new Processor() {
  57. @Override
  58. public void process(Exchange exchange) throws Exception {
  59. Response response = new Response();
  60. response.setStatus(Status.SUCCESS);
  61.  
  62. exchange.getOut().setBody(response);
  63. }
  64. });
  65. }
  66. };
  67. }
  68.  
  69. //1. works:
  70. MainRouteBuilder.nestedRouteBuilder = this;
  71.  
  72. //2. does not work:
  73. // RouteContainer routeContainer = new RouteContainer();
  74. // routeContainer.route = this.route;
  75. // MainRouteBuilder.nestedRouteBuilder = routeContainer;
  76.  
  77. return "direct:myRoute." + n;
  78. }
  79.  
  80.  
  81. @Override
  82. public void configure() throws Exception {
  83. if (route != null) {
  84. route.configure();
  85. }
  86. }
  87.  
  88. public abstract static class Route {
  89. abstract public void configure();
  90. }
  91.  
  92. }
  93.  
  94. Building main route!
  95. Context: SpringCamelContext(camel-1) with spring id org.springframework.web.context.WebApplicationContext:/sample-route
  96. Building nested route!
  97. ContextSpringCamelContext(camel-1) with spring id org.springframework.web.context.WebApplicationContext:/sample-route
  98.  
  99. {"status":"SUCCESS"}
  100.  
  101. org.apache.camel.component.direct.DirectConsumerNotAvailableException: No consumers available on endpoint: Endpoint[direct://myRoute.2].
  102.  
  103. public class MainRouteBuilder extends RouteBuilder
  104. {
  105. public static CamelContext camelContext;
  106.  
  107. public static boolean returnLabel = true;
  108.  
  109. public static RouteBuilder nestedRouteBuilder;
  110.  
  111. RouteContainer routeContainer = new RouteContainer();
  112.  
  113. @Override
  114. public void configure() throws Exception
  115. {
  116. System.out.println("Building main route!");
  117. System.out.println("Context: " + getContext());
  118.  
  119. camelContext = getContext();
  120.  
  121. from("direct:cloudAdministrationRoute.createTenant")
  122. .to(routeContainer.getMyRoute(2))
  123. //I may want to call it again like this:
  124. //.to(routeContainer.getMyRoute(3))
  125. ;
  126.  
  127. returnLabel = false;
  128.  
  129. //configure direct:myRoute.2
  130.  
  131. includeRoutes(nestedRouteBuilder);
  132. }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement