Akhilesh Pandey
Akhilesh Pandey

Reputation: 211

Load apache camel routes at runtime

In the project , I've been dynamically loading Apache Camel routes during runtime. Current environment includes Spring 2.7.4, Apache Camel 3.18.0, and JDK 17

private void fromFile(final Path filepath) throws Exception {
    final byte[] routeDefinitionBytes = Files.readAllBytes(filepath);
    final Resource resource = ResourceHelper.fromBytes(filepath.toString(), routeDefinitionBytes);
    final ExtendedCamelContext extendedCamelContext = camelContext.adapt(ExtendedCamelContext.class);
    extendedCamelContext.getRoutesLoader().loadRoutes(resource);
}

Now migrating project to utilize Apache Camel 4.0.0-RC1, Spring Boot 3.1.1, and continue to use JDK 17. The issue facing is that the adapt() method isn't available in the newer version of Apache Camel, rendering your existing code non-functional.

I'm seeking a viable solution that can allow me to continue reading route definitions from a file and inject them into an Apache Camel context at runtime in Apache Camel 4.0.0-RC1. Any recommendations would be much appreciated.

Upvotes: 1

Views: 1071

Answers (3)

krishna
krishna

Reputation: 1

I had to add the following maven dependency also along with the above mentioned code to load xml dynamically on run time.

    <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-xml-jaxb-dsl-starter</artifactId>
            <version>4.7.0</version>
        </dependency>

Upvotes: 0

Gary Motts
Gary Motts

Reputation: 1

Extending the existing answer https://stackoverflow.com/a/76809559/7733418 let me point out that after the PluginHelp.getResourceLoader, the camelContext needs to be started started, e.g.,
camelContext.start();.

Upvotes: 0

Nicolas Filotto
Nicolas Filotto

Reputation: 44985

For such a need, you can rely on the utility method PluginHelper#getRoutesLoader(CamelContext) but behind the scenes, the new logic is to call getCamelContextExtension() on the camel context to get the ExtendedCamelContext, then get the plugin that you want to use thanks to the method ExtendedCamelContext#getContextPlugin(Class).

So your code would then be:

private void fromFile(final Path filepath) throws Exception {
    final byte[] routeDefinitionBytes = Files.readAllBytes(filepath);
    final Resource resource = ResourceHelper.fromBytes(
        filepath.toString(), routeDefinitionBytes
    );
    PluginHelper.getRoutesLoader(camelContext).loadRoutes(resource);
}

Upvotes: 1

Related Questions