apache-cameljava-17spring-boot-3

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.


Solution

  • 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);
    }