【问题标题】:How in Camel to add and start routes dynamically?Camel如何动态添加和启动路由?
【发布时间】:2016-03-22 13:32:22
【问题描述】:

我正在尝试从 Camel 的路线中删除一些样板。

例如,让我们考虑两条路线,它们相似并且可以生成它们的大部分内部内容。我创建了一个组件“模板”,它创建了TemplateEndpoint,并修改了一个 XML 配置以使用模板组件。

正在从StartupListener(在TemplateEndpoint.setSuffix 中定义)调用自定义方法TemplateEndpoint.generateRoute(添加路由定义)。 因此,当 Camel 上下文启动时,路由定义出现在上下文中,但框架不会为它们创建路由服务,因此它们不会启动。

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>

端点

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);
       }
  }
}

【问题讨论】:

    标签: java templates routes apache-camel boilerplate


    【解决方案1】:

    我会创建一个动态路由构建器,例如

    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"));
    
    ..
    

    【讨论】:

    • 嘿,例子真的很好。我在实施相同的过程中遇到了一些问题。
    猜你喜欢
    • 1970-01-01
    • 2018-07-01
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多