首页 > 解决方案 > 如何在运行时在 Spring Boot 应用程序下将添加/删除路由更新到 Apache Camel 中?

问题描述

我需要在应用程序Apache Camel的运行时添加/删除路由。Spring Boot这意味着应该在应用程序运行时更新路由。这也意味着在应用程序的第一次运行期间我没有所有路由,并且我无法重新启动它以获取它们。

我找到了旧答案,但仍然无法理解如何使用xml-defined 路由来做到这一点。你能解释一下怎么做吗?

标签: javaspring-bootroutesapache-camel

解决方案


将您的 xml 放在 src/main/resources 下并调用以下组件:



    @Component 
    public class CamelRoutesLoader {

        @Autowired
        private CamelContext camelContext;

        @Value("${camel-routes-filename:#{null}}")
        private String routesFilename;

        @PostConstruct
        private void loadRoutes() {
            if(routesFilename == null) {
                //error no file name
            }
            try {
                InputStream is = this.getClass().getClassLoader().getResource(routesFilename).openStream();
                RoutesDefinition routes = ModelHelper.loadRoutesDefinition(camelContext, is);
                camelContext.addRouteDefinitions(routes.getRoutes());
            } catch(Exception e) {
                //Impossible to load routes
            }
        }
    }

我在 Spring 初始化后调用我的组件,但您可以删除 @PostConstruct,将其公开并传递文件名


推荐阅读