How in Camel to add and start routes dynamically?

前端 未结 1 1122
Happy的楠姐
Happy的楠姐 2020-12-30 11:37

I\'m trying to remove some boilerplate from routes in Camel.

For example, let\'s consider the two routes, which are similar and most of their inner stuff could be ge

相关标签:
1条回答
  • 2020-12-30 12:35

    I would create a dynamic route builder e.g.

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

    and add it on some event e.g

    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"));
    
    ..
    
    0 讨论(0)
提交回复
热议问题