title: Stop/Remove Route Dynamically/Programmatically Doesn't Remove the Corresponding Thread
body: <p>During the processing of an Exchange received from JMS I'm creating dynamically a route that fetches a file from FTP to the file system and when the batch is done I need to remove that same route. The following code fragment shows how I do this:</p>
  
  <pre class="lang-java prettyprint-override"><code>public void execute() {
      try {
          context.addRoutes(createFetchIndexRoute(routeId()));
      } catch (Exception e) {
          throw Throwables.propagate(e);
      }
  }
  
  private RouteBuilder createFetchIndexRoute(final String routeId) {
      return new RouteBuilder() {
          @Override
          public void configure() throws Exception {
              from("ftp://" + getRemoteQuarterDirectory() +
                  "?fileName=" + location.getFileName() +
                  "&amp;binary=true" +
                  "&amp;localWorkDirectory=" + localWorkDirectory)
                  .to("file://" + getLocalQuarterDirectory())
                  .process(new Processor() {
                      RouteTerminator terminator;
  
                      @Override
                      public void process(Exchange exchange) throws Exception {
                          if (camelBatchComplete(exchange)) {
                              terminator = new RouteTerminator(routeId, 
                                      exchange.getContext());
                              terminator.start();
                          }
                      }
                  })
                  .routeId(routeId);
              }
          };
  }
  </code></pre>
  
  <p>I'm <strong>Using a thread to stop a route from a route</strong>, which is an approach recommended in the Camel Documentation - <a href="http://camel.apache.org/how-can-i-stop-a-route-from-a-route.html" rel="noreferrer">How can I stop a route from a route</a></p>
  
  <pre class="lang-java prettyprint-override"><code>public class RouteTerminator extends Thread {
      private String routeId;
      private CamelContext camelContext;
  
      public RouteTerminator(String routeId, CamelContext camelContext) {
          this.routeId = routeId;
          this.camelContext = camelContext;
      }
  
      @Override
      public void run() {
          try {
              camelContext.stopRoute(routeId);
              camelContext.removeRoute(routeId);
          } catch (Exception e) {
              throw Throwables.propagate(e);
          }
      }
  }
  </code></pre>
  
  <p>In result the route does stop. But what I see in the <strong>jconsole</strong> is that the thread that corresponds to the route isn't removed. Thus in time these <em>abandoned</em> threads just keep accumulating.</p>
  
  <p>Is there a way to properly stop/remove a route dynamically/programmatically and also to release the route's thread, so that they don't accumulate through time? </p>