Load apache camel routes at runtime and add to existing CamelContext

Viewed 284

I am trying to load an Apache Camel routes at runtime and add to existing CamelContext. I have route defined like below ,

<?xml version="1.0" encoding="UTF-8" ?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

    <routeContext id="message" xmlns="http://camel.apache.org/schema/spring">
        <route id="abcRoute" autoStartup="false">
            <from uri="activemq:input"/>
            <delay>
                <constant>1000</constant>
            </delay>
            <to uri="activemq:output"/>
        </route>
    </routeContext>

</beans>

I see , it was passible to load camel routes at runtime using loadRoutesDefinition in camel2 as below ,

InputStream inputStream = getClass().getResourceAsStream("MyRoute.xml");
    RoutesDefinition routesDefinition = camelContext.loadRoutesDefinition(is);
camelContext.addRouteDefinitions(routesDefinition.getRoutes());

I am looking for possible way to load camel routes in camel3 at runtime.

1 Answers

You can load route definitions from files by using ExtendedCamelContext, adapting your Camel context:

    ExtendedCamelContext ecc = camelContext.adapt(ExtendedCamelContext.class);
    Resource resource = ResourceHelper.fromBytes("resource.xml", bytes);
    ecc.getRoutesLoader().loadRoutes(resource);

where bytes is the byte array that stores the content of your file.

Related