Hello Camel community!

Am currently using camel version 2.24.1.

I have a code which starts a camel context by passing an instance of 
RouteBuilder to it. It looks as follows.

newCamelContext.addRoutes(new MySmartRouteBuilder(flow));
newCamelContext.start();


Problem : When the application reboots initially, all the routes are 
successfully started and works great. But once the application is in running 
state, any updated to existing flows (I stop the old camel context and spin up 
an updated one with fresh instance of MySmartRouteBuilder), camel context 
starts successfully but looks like none of those routes which are created get 
added to the camel context when .addRoutes() method is executed. Hence the log 
says 0 of 0 routes started when newCamelContext starts.


The implementation of MySmartRouteBuilder is as follows

package com.reji.camel.core.route;

public class MySmartRouteBuilder extends RouteBuilder {

    private static Logger logger = 
LoggerFactory.getLogger(MySmartRouteBuilder.class);
    private MyIntegrationDefinition myIntegrationDefinition;
    private ObjectMapper mapper = new ObjectMapper();
    private RouteDefinition routeDef = new RouteDefinition();

    public MySmartRouteBuilder(MyIntegrationDefinition myIntegrationDefinition) 
{
        super();
        this.myIntegrationDefinition = myIntegrationDefinition;
    }

    @Override
    public void configure() throws Exception {
        for(Step step : myIntegrationDefinition.steps) {
            NodeHandler nodeHandler = NodeHandlerFactory.get(step.stepType);
            routeDef = nodeHandler.handleNode(routeDef, step);
            routeDef.id(flow.id);
        }
        logger.info("Final route definition : {}", routeDef.toString());    // 
This one prints the routeDefinition successfully everytime and it looks fine
    }
}



NodeHandler is an interface with implementations for each type of step in 
MyIntegrationDefinition which analyses steps inside MyIntegrationDefinition and 
incrementally builds routeDef object. Sample of handler looks something like 
follows

package com.reji.camel.core.handlers.node;

import com.reji.camel.core.handlers.NodeHandler;
import com.reji.camel.core.handlers.NodeHandlerFactory;
import com.reji.camel.core.models.Step;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.RouteDefinition;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
@Component
public class QuartzNodeHandler extends RouteBuilder implements NodeHandler {
    private static final String NAME = "quartz";
    private static final String CRON = "cron";

    @PostConstruct
    void init() {
        NodeHandlerFactory.register(NAME, this);
    }

    @Override
    public RouteDefinition handleNode(RouteDefinition routeDefinition, Step 
step) throws Exception {
        String uri = String.format("quartz://apic/quartz?cron=%s", 
flowStep.configurations.get(CRON));
        return from(uri);
    }


    @Override
    public void configure() throws Exception {

    }
}

I can see that routeDef gets printed correctly at the logger statement inside 
MySmartRouteBuilder -> logger.info("Final route definition : {}", 
routeDef.toString()); (which says all my handlers work great!! )

While running in debug mode, I see that when the object of MySmartRouteBuilder 
gets returned back to newCamelContext.addRoutes(new MySmartRouteBuilder(flow)); 
all routeDefs are lost and there are 0 routes in there.

Any help to identify the dynamic behavior of RouteBuilder class would be very 
helpful! Thanks..

Reply via email to