Best way to load dynamically routes in Apache Camel

后端 未结 3 605
渐次进展
渐次进展 2021-01-13 04:41

we have developped application based on Karaf and Apache Camel. While our application is entirely based on bundles ( OSGI ) we are also loading the Camel context ( and its\'

3条回答
  •  广开言路
    2021-01-13 05:35

    Adding/Removing routes dynamically does not restart/reset camelContext.

    Please find the sample.

    DynamicAddRouteProcessor.java

    public class DynamicAddRouteProcessor implements Processor {
    
    @Override
    public void process(Exchange paramExchange) throws Exception {
    
        final String routeId = "DYNANMIC.ROUTE.1";
        Route route = paramExchange.getContext().getRoute(routeId);
        if (null == route) {
            System.out.println("No route exist, creating one with name "); 
            paramExchange.getContext().addRoutes(new RouteBuilder() {
                public void configure() throws Exception {
                    from("direct:DYNANMIC.ROUTE.1").routeId(routeId).to("direct:myloggerRoute");
                }
            });
        } else {
            System.out.println("Route already exist, no action"+ route.getId());
        }
    
    }
    

    }

    DynamicRemoveRouteProcessor.java

    public class DynamicRemoveRouteProcessor implements Processor {

    @Override
    public void process(Exchange paramExchange) throws Exception {
    
        final String routeId = "DYNANMIC.ROUTE.1";
        Route route = paramExchange.getContext().getRoute(routeId);
        if (null != route) {
            System.out.println("Route already exist, deleting it!!!"    + route.getId());
            paramExchange.getContext().stopRoute(routeId);
            paramExchange.getContext().removeRoute(routeId);
    
        } else {
            System.out.println("No sucn route exist, no action done "
                    + routeId);
        }
    
    }
    

    }

    blueprint.xml

    
        
            
            
                
            
            
            
        
        
            
            
            
            5000
            
            
        
    
        
            
            
            
        
    
    

提交回复
热议问题