GLMills
GLMills

Reputation: 638

how to find all routes on a camel context

I'm trying to find all routes associated with a camel context? I can't find the camel exchange getContext() data sheets so I know what methods can be called?

I have a dynamic route builder, drop a config file and the route gets created. I need to create the routes in registry, as not started, and use a JGroups/Controlbus route that controls who is the active route. But I can't figure out how to get all routes associated with a camel context? if you can shed some light on this, I'd really be in your dept. thanks in advance.

This is what I have but I can't get to work, found on stacktrace.

@Override
public void process(Exchange exchange) throws Exception {

    List<ProcessorDefinition<?>> outputProcessorDefs = exchange.getContext().getRouteDefinition("[routeId]").getOutputs();
    for ( ProcessorDefinition rte : outputProcessorDefs ) {

    log.info("ROUTES: " + rte);

    }

}

Upvotes: 1

Views: 4535

Answers (2)

GLMills
GLMills

Reputation: 638

This is my test for JGroups ControlBus management made possible by getRoutes() for dynamic route creation.

public class EndpointControlBusFileRouteBuilder extends RouteBuilder {

    private static final Logger log = LoggerFactory.getLogger(EndpointControlBusFileRouteBuilder.class);

    private String routeId;
    private String ClusterId;

    public EndpointControlBusFileRouteBuilder(String routeId) {

        this.routeId = routeId;

    }

    @Override
    public void configure() throws Exception {

        log.info("*** JGroups routeCluster - RouteId : " + routeId + " ***");
        ClusterId = routeId + ".JGroups";

        from("jgroups:" + ClusterId + "?enableViewMessages=true&channelProperties=etc/jgroups.xml")
        .autoStartup(true)
        .routeId(ClusterId)
        .filter(dropNonCoordinatorViews())
        .threads().delay(delayIfContextNotStarted(SECONDS.toMillis(5))) // run in separated and delayed thread. Delay only if the context hasn't been started already. 
        .log("Starting JGroups JChannel Routes Consumer!!!!!!!!!!!!!!!!!!!!!")
        .to("controlbus:route?routeId=" + routeId + "&action=start&async=true");

    }

}

public class EndpointControlBusProcessor implements Processor {

    private String routeId = "";

    private static final Logger log = LoggerFactory.getLogger(EndpointControlBusProcessor.class);

    @Override
    public void process(Exchange exchange) throws Exception {

        List<Route> routeList = exchange.getContext().getRoutes();
        ProducerTemplate template = exchange.getContext().createProducerTemplate();

        for ( Route rte : routeList ) {

            routeId = rte.getId();

//          log.info("ROUTES: " + routeId);
//          ServiceStatus routeStatus = exchange.getContext().getRouteStatus(routeId);
//          log.info("Route " + routeId + " Status: " + routeStatus);

            String status = template.requestBody("controlbus:route?routeId=" + routeId + "&action=status", null, String.class);
            log.info("Controlbus Route Status: " + status + " for route: " + routeId);

            if ( (null == status) || status.equalsIgnoreCase("Stopped") ) {

                exchange.getContext().addRoutes(new EndpointControlBusFileRouteBuilder(routeId));
//              status = template.requestBody("controlbus:route?routeId=" + routeId + "&action=status", null, String.class);
//              log.info("Controlbus Route Status: " + status + " for route: " + routeId);

            } else {

                log.info("Route " + routeId + " already started");

            }
        }
        template.stop();
    }
}

Upvotes: 1

GLMills
GLMills

Reputation: 638

thanks to a question answered by Claus How to find all Endpoints of route (Apache Camel, Java)

I was able to locate some info and found another simpler way to do this.

public void process(Exchange exchange) throws Exception {

    List<Route> routeList = exchange.getContext().getRoutes();
    for ( Route rte : routeList ) {

    log.info("ROUTES: " + rte.getId());

    }

Upvotes: 3

Related Questions