简体   繁体   中英

Unable to add Route in Apache Camel dynamically at Runtime

I wanted to add new camel route dynamically on runtime. ie say on user request i need to add new route and need to start consuming data from the newly added route as well, which i couldnt.

import java.util.ArrayList;
import java.util.List;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.SpringRouteBuilder;
import org.apache.camel.model.RouteDefinition;
import org.springframework.stereotype.Component;
@Component
public class EndNodeConsumer extends SpringRouteBuilder {

    public List<String> routes_list = new ArrayList<String>();

    public EndNodeConsumer(){
        if(routes_list!=null && routes_list.size() == 0){
            routes_list.add("ddsi:EndNode1:0/Sensor.Msg?reliabilityKind=RELIABLE&Partition=EndNode");
            routes_list.add("ddsi:EndNode2:0/Sensor.Msg?reliabilityKind=RELIABLE&Partition=EndNode");           
        }
    }

    @Override   
    public void configure() throws Exception {  
        for(int i =0 ;i <routes_list.size(); i++){  
            System.out.println("inside configure "+i+" "+routes_list.get(i));
            from(routes_list.get(i))
            .unmarshal("cdr")
            .process(new Processor() {    
                public void process(Exchange e) {
                    System.out.println("DDS Consumer "+((Sensor.Msg) e.getIn().getBody()).sensorId);                    
                }                   
            }); 
        }
        List<RouteDefinition> str = getRouteCollection().getRoutes();
        for(int j = 0 ;j <str.size();j++){
            System.out.println("routes "+str.get(j));
        }

    }
}

On request new route will be added to the array list "routes_list". then a function call to method "configure " will be made.

When i see the camel RouteCollection by calling getRouteCollection().getRoutes() i am able to see the new routes aswell in the list but on sending a message to new route am not able to receive . Could you please have look at it and please provide your input

Use the API on CamelContext to add routes at runtime. It has a addRouteBuilder method among others. But check its api.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM