2015-12-16 4 views
2

Я пытаюсь удалить некоторые шаблоны с маршрутов в Camel.Как в Camel добавлять и запускать маршруты динамически?

Например, у нас есть два маршрута, которые похожи, и большая часть их внутреннего материала может быть сгенерирована. Я создал компонент «шаблон», который создает TemplateEndpoint и изменяет конфигурацию XML.

Когда контекст верблюда, начинающийся с моего StartupListener, добавляет определения маршрута (созданные в TemplateEndpoint.generateRoute) в контекст, но не создает для них службы маршрута, и, следовательно, они не запускаются.

Как начать маршруты, добавленные в StartupListener?

Возможно, я столкнулся с проблемой XY, и вы можете посоветовать мне лучший подход к трюку (т. Е. Добавление и запуск маршрутов на лету).

Похожие два маршрута

<route id="route_A"> 
    <from uri="restlet:/another1?restletMethods=GET" /> 
    <to uri="first:run_A" /> 
    <to uri="second:jump_A" />  
    <to uri="third:fly_A" /> 
</route> 

<route id="route_B"> 
    <from uri="restlet:/another2?restletMethods=GET" /> 
    <to uri="first:run_B" /> 
    <to uri="second:jump_B" />  
    <to uri="third:fly_B" /> 
</route> 

Модифицированный XML

<route id="route_A"> 
    <from uri="restlet:/another1?restletMethods=GET" /> 
    <to uri="template://?suffix=A" /> 
</route> 

<route id="route_B"> 
    <from uri="restlet:/another2?restletMethods=GET" /> 
    <to uri="template://?suffix=B" /> 
</route> 

Endpoint

public class TemplateEndpoint extends DefaultEndpoint { 

    /* some methods omitted */ 
    // this endpoint creates a DefaultProducer, which doing 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); 
     } 
    } 
} 

ответ

1

Я хотел бы создать д ynamic route builder, например.

public class MyRouteBuilder extends RouteBuilder { 

      private String another; 
      private String letter; 

      public MyRouteBuilder(CamelContext context,String another, String letter) { 
       super(context); 
       this.another=another; 
       this.letter=letter; 
      } 

      @Override 
      public void configure() throws Exception { 
       super.configure(); 
       from("restlet:/"+another+"?restletMethods=GET") 
       .to("first:run_"+letter) 
       .to("second:jump_"+letter) 
       .to("third:fly_"+letter); 
      } 
    } 

и добавить его на какое-то событие

например
public class ApplicationContextProvider implements ApplicationContextAware { 

    @Override 
    public void setApplicationContext(ApplicationContext context) 
      throws BeansException { 

     camelContext=(CamelContext)context.getBean("mainCamelContext"); 
     camelContext.addRoutes(new MyRouteBuilder(camelContext, "another1","A")); 
     camelContext.addRoutes(new MyRouteBuilder(camelContext, "another2","B")); 

.. 
Смежные вопросы