title: How in Camel to add and start routes dynamically?
body: <p>I'm trying to remove some boilerplate from routes in Camel.</p>
  
  <p>For example, let's consider the two routes, which are similar and most of their inner stuff could be generated. I've created a component "template", which creates <code>TemplateEndpoint</code>, and modifyed an XML config to use the template component.</p>
  
  <p>A custom method <code>TemplateEndpoint.generateRoute</code> (adding route definitions) is being called from <code>StartupListener</code> (defined in <code>TemplateEndpoint.setSuffix</code>).
  So while Camel context starting, route definitions appear in the context, but the framework doesn't create route services for them and hence they don't get started.</p>
  
  <p>How to start routes added in <code>StartupListener</code>?</p>
  
  <p><em>Probably I'm facing an XY problem, and you can advice me a better approach to do the trick (i.e. adding and starting routes on the fly)</em>.</p>
  
  <p><strong>Similar two routes</strong></p>
  
  <pre><code>&lt;route id="route_A"&gt;
    &lt;from uri="restlet:/another1?restletMethods=GET" /&gt;
    &lt;to uri="first:run_A" /&gt;
    &lt;to uri="second:jump_A" /&gt;     
    &lt;to uri="third:fly_A" /&gt;
  &lt;/route&gt;
  
  &lt;route id="route_B"&gt;
    &lt;from uri="restlet:/another2?restletMethods=GET" /&gt;
    &lt;to uri="first:run_B" /&gt;
    &lt;to uri="second:jump_B" /&gt;     
    &lt;to uri="third:fly_B" /&gt;
  &lt;/route&gt;
  </code></pre>
  
  <p><strong>Modified XML</strong></p>
  
  <pre><code>&lt;route id="route_A"&gt;
    &lt;from uri="restlet:/another1?restletMethods=GET" /&gt;
    &lt;to uri="template://?suffix=A" /&gt;
  &lt;/route&gt;
  
  &lt;route id="route_B"&gt;
    &lt;from uri="restlet:/another2?restletMethods=GET" /&gt;
    &lt;to uri="template://?suffix=B" /&gt;
  &lt;/route&gt;
  </code></pre>
  
  <p><strong>Endpoint</strong></p>
  
  <pre><code>public class TemplateEndpoint extends DefaultEndpoint {
  
    /* some methods omitted */ 
    // this endpoint creates a DefaultProducer, which does nothing
  
    public void setSuffix(final String suffix) throws Exception {
  
      this.getCamelContext().addStartupListener(new StartupListener() {
        @Override
        public void onCamelContextStarted(CamelContext context, boolean alreadyStarted) throws Exception {       
          generateRoute(suffix)
          .addRoutesToCamelContext(context);
        }
      });
    }
  
    private RouteBuilder generateRoute(final String suffix){ 
       final Endpoint endpoint = this;
  
       return new RouteBuilder() {
          @Override
          public void configure() throws Exception {
            from(endpoint)
             .to("first:run_" + suffix)
             .to("second:jump_" + suffix)
             .to("third:fly_" + suffix);
         }
    }
  }
  </code></pre>